Friday, October 9, 2015

Getting word count from file in unix/linux

syntax: wc filename

WC command will display the total number of lines, words and characters from file.

Example: $ wc filename

2 30 100 filename

First column - Total number of lines (2 lines in example file)
Second column - number of words (30 words in example file)
Third column - number of characters (100 characters in example file)

To get only line count from file

$ wc -l filename

2 filename

To get only word count from file

$ wc -w filename

30 filename

To get only character count from file

$ wc -c filename

100 filename

Display last few lines from file in unix/linux using tail command

$ tail file.txt

It will display last 10 lines from file.txt

$ tail file.txt -n 100

It will display last 100 lines from file.txt

Display first/top few lines from file in unix/linux

head -lines filename

Example: $ head -3 file.txt

It will display first 3 lines form file.txt. Here lines is an optional parameter. If you don't specify lines it will display 10 lines by default.

Example: $ head file.txt

It will display first 10 lines from file.txt

Thursday, October 8, 2015

Last reboot time in Linux/Unix server

$ last reboot

The above command will display last 2 reboot time

$ last reboot

It will display last system reboot time

$ who -b

Result:        system boot 2015-02-22 04:40

It will display the date and time of last reboot

$ uptime

It will also show the total time since the last reboot