Monday, May 10, 2010

Practically Useful UNIX Commands

Friends,

Last weekend my FireNookz’ (desktop) HDD crashed and I had a chance to install RHEL 5 in my system. After a long time I was trying to mess with some of the UNIX commands. I am not that expert in SED and AWK however I can survive and I am still learning though… I shall also share a “Practically Useful GREP Commands” as soon as possible but for now… Here are few commands which every UNIX professional may find useful.

______________________________

1. Display specific lines (based on line number) of a file using sed command

Syntax: $ sed -n -e Xp -e Yp FILENAME

sed : sed command, which will print all the lines by default.
-n : Suppresses output.
-e CMD : Command to be executed
Xp: Print line number X
Yp: Print line number Y
FILENAME : name of the file to be processed.

Example: $ sed -n -e 120p -e 145p -e 1050p /var/log/syslog

2. Viewing the content of var/log/cron from line number 101 to 110.

Syntax: sed -n M,Np FILENAME

M – Starting line number
N – Ending line number

Example: $ sed -n 101,110p /var/log/cron

3. Display first N lines of a file using head command

Syntax: head -n N FILENAME

Example: $ head -n 15 /var/log/maillog


4. Ignore last N lines of a file using head command and show only the remaining lines from the top of file.

Syntax: head -n -N FILENAME

Example: $ head -n -250 /var/log/secure

5. Display last N lines of the file using tail command

Syntax: tail -n N FILENAME

Example: $ tail -n 50 /var/log/messages

6. Ignore first N-1 lines of the file using tail command and show only the remaining of the lines.

Syntax: tail -n +N FILENAME

Example: $ tail -n +5 /etc/xinetd.conf

7. View growing log file in real time using tail command

Syntax: tail -f FILENAME

Example: $ tail -f /var/log/syslog

8. Display specific lines (based on line number) of a file using head and tail command

The example below will display line numbers 101 – 110 of /var/log/anaconda.log file

M – Starting line number
N – Ending line number

Syntax: cat file | tail -n +N | head -n (M-N+1)

Example: $ cat /var/log/anaconda.log | tail -n +101 | head -n 10

cat : prints the whole file to the stdout.
tail -n +101 : ignores lines upto the given line number, and then start printing lines after the given number.
head -n 10 : prints the first 10 line, that is 101 to 110 and ignores the remaining lines.

9. Display lines matching a pattern, and few lines following the match.
The following example displays the line that matches “Initializing CPU” from the /var/log/dmesg and 5 lines immediately after this match.
# grep "Initializing CPU#1" /var/log/dmesg
Initializing CPU#1
[Note: The above shows only the line matching the pattern]
 
# grep -A 5 "Initializing CPU#1" dmesg
Initializing CPU#1
Calibrating delay using timer specific routine.. 3989.96 BogoMIPS (lpj=1994982)
CPU: After generic identify, caps: bfebfbff 20100000 00000000 00000000
CPU: After vendor identify, caps:  bfebfbff 20100000 00000000 00000000
monitor/mwait feature present.
CPU: L1 I cache: 32K, L1 D cache: 32K
[Note: The above shows the line and 5 lines after the pattern matching]
PS:
Viewing N lines after the match with -A option.
Viewing N lines before the match with -B option.
Viewing N lines around the match with -C option.

10. Displaying specific bytes from a file.

Display first 40 bytes from syslog.
$ head -c40 /var/log/syslog

Display last 30 bytes from syslog.
$ tail -c30 /var/log/syslog

11. Viewing compressed log files

The following example explains how to display either the top 40 or the last 30 bytes of a file.

# Display the first N lines of a compressed file.
$ zcat file.gz | head -250

# Display the last N lines of a compressed file.
$ zcat file.gz | tail -250

# Ignoring the last N lines of a compressed file.
$ zcat file.gz | head -n -250

# Ignoring the first N lines of a compressed file.
$ zcat file.gz | tail -n +250

# Viewing the lines matching the pattern
$ zcat file.gz | grep -A2 'error'

# Viewing particular range of lines identified by line number.
$ zcat file.gz | sed -n -e 45p -e 52p

Happy Learning!

0 comments: