Useful Unix Commands and Tips - Part 5

Let us learn about some miscellaneous commands in this part:

1) To find Linux version of OS:
lsb_release -a
2) To find kernel version:
uname -a
3) To find number cpu cores:
lscpu
cat /proc/cpuinfo
4)  more: cat command can be used to view the file contents. But if the file is more than few pages, reading through the buffer is difficult. The commands more and less are more useful in these cases. We can go to next page by pressing space bar.  By pressing b we can go back by one page. We can search for a string by typing /, followed by the string similar to vi. We can find the next occurrence of the string by typing n.
5) less: less command has similar functionality as more but it is faster.
6) which  command can be used to find the location of a program.
which perlwhich java
7) To read a file backwards, we can use tac (reverse cat) and less together:
tac nohup.out | less
8) To find packages installed using rpm:
rpm -qa | egrep "java-1.7.0-openjdk|java-1.8.0-openjdk
9) fuser is used to find users currently using files and sockets.
fuser <filename>
10) To create directories including child directories:
mkdir -p test1/test2
11) To print Unix environment variables we can use either printenv or env
 
12) touch can be used to create empty file. If file already exists, it updates modified timestamp.
touch <filename>
13) split can be used to split a file in unix. My default it splits into 1000 line files.:
split log.txt  # Assuming log.txt has 4500 lines, it will spit it into 5 files (4x1000 + 500)
split -l 2000  log.txt  part # This will split log.txt into 3 files: parta (2000 lines), partb (2000 lines), partc (500 lines)
split -b 100k log.txt  part # Assuming file size is 250 KB, it splits file into parta (100 KB), partb (100 KB), partc (100 KB)
14) To see a list of all shell variables, type set. The output of set includes the environment variables, because those are available to the shell as well. Environment and shell variables are different. We need to export a shell variable to be available as environment variable. If you want to remove a variable, simply type unset and name of the variable.
15) To see all the aliases set in your shell, just type alias (with no arguments). If you want to remove an alias, just type unalias and the alias’s name.
unalias ll # To remove aliasls  #To ignore an alias run it prepended with alias ll # To get an alias definition just call it with alias.
16) To find number of occurrences of a string in a file:
grep -o -n "searchString" filename  | cut -d : -f 1 | uniq -c
17) Useful netstat commands:
netstat -tunapl    # To list listen ports.netstat -nr        # Show routing table
18) To trace routes and hops to a target ip:
traceroute 200.200.200.23
19) Input Output Redirection:
i) With 2>&1, the stderr stream with file descriptor 2 is redirected to &1. This effectively merges stderr into stdout.
ii) If you want to ignore errors, use 2>/dev/null
20) As a system administrator, we need to monitor live logs lot of times. We can use tail command to check live updates to log file.
tail -f access.log # f stands for follow. This command continuously writes access.log updates.
tail -f follows the file descriptor. So, whenever logs are rotated, tail stops giving contents of current log files as it keeps on following the contents of file which has be moved to some other file name. To continuing to tail a rotating file, we can ask tail to follow a file by its name but not by its file descriptor. F flag can be used to follow the file name instead of file descriptor.
tail -F access.log
21)  Applications write logs to multiple log files. For example, weblogic has 3 main log files: server.log, diagnostic log and out log. We can tail all the logs by passing their names as arguments. It is very useful while troubleshooting an issue.
tail -f log1.txt log2.txt log3.txt
22) Comparing Files:
i) diff: The diff command compares two files, reporting on differences between them.
diff file1 file2diff -rq dir1 dir2 # To find difference between two directories.
ii) comm: comm produces three columns of output – lines only in file1 (column 1), lines only in file2 (column 2), and lines common to both files (column 3). Option flags can be used to suppress columns of output.
comm file1 file2
iii) cmp: The cmp command also compares two files. However, different than comm or diff, the cmp command (by default) reports the byte and line number where the two files first are different.
cmp file1 file2
iv) sdiff – This command compares two file and gives results side by side. Personally I prefer sdiff.
sdiff file1 file2
23) We can follow any of the following methods to empty files in linux:
cat /dev/null > <filename>truncate --size 0 soa_server2.out* # I prefer this method as we can empty multiple files.> <filename>
24) JDK jar utility:
jar tvf test.jar  # View contents of JAR filejar xvf test.jar  # Extract contents of JAR file
25) nslookup can be used to convert hostname to ip and vice-versa. It is very useful to in troubleshooting DSN/network related issues.
nslookup <hostname>
26) DIG (Domain Information Groper)  also queries DSN server but has more advanced features:
dig <hostname>
27) cp is used for copying files and directories.
cp file1 file2 # creates a file copy file2 from file1.
cp file1 file2 dir1 # Copies file1 and file2 to dir1
cp -R dir1 dir2 # Copies dir1 to dir2
cp -p file1 file2 # Preserves file permissions. p stands for preserve.

Comments

Popular posts from this blog

HOW WE REDUCED SOA OSB PROVISIONING FROM 4 DAYS TO 4 HOURS

NOT ABLE TO START RABBITMQ CLUSTER: CANNOT DECLARE A QUEUE ‘~S’ ON NODE ‘~S’: ~255P

SOA SUITE 12.2.1.4 INSTALLATION: GOT EXCEPTION WHEN AUTO CONFIGURING THE SCHEMA COMPONENT(S) WITH DATA OBTAINED FROM SHADOW TABLE