Saturday, July 7, 2012

Frequently used linux commands!!

1.Delete all trailing blank lines at end of file using sed
Blank lines at end of a text file are mostly useless, you can remove them easily using sed:
# sed -e :a -e '/^\n*$/{$d;N;ba' -e '}'

 2.Delete trailing spaces or tabs from end of each line
To delete trailing spaces or tabs, i.e. whitespaces from the end of each line, is easy with the sed command.
# sed 's/[ \t]*$//'

 3.Count the total number of logged in user
To count the total number of logged in user(s), use the following commands:
# who | wc -l

4.Check remote hostname using nslookup
You can find the hostname of remote machine using the nslookup command.
# nslookup | grep name     name =

 5.List all installed package in Ubuntu
To list all the installed packages in Ubuntu, so you try the "dpkg -l" command:
# dpkg -l

 6.Mount a CDROM in Linux
You can mount a CDROM using this command:
# mount /dev/cdrom /mnt/cdrom
Make sure you have created the mount point /mnt/cdrom beforehand.

 7.Add a user to group in Linux
To add a user to a group (supplementary), you can use the command below:
# useradd -G admin john
If you want to change the user's primary group, you can use
# useradd -g admin john

 8.Make a file executable in Linux
Assume you have create a shell script,, you need to make this file executable, you can use the chmod command.
# chmod +x
Then you can execute the script directly
# ./

9.Print the web site's content using wget
To show the web site's content in command line, just one command with wget.
# wget -q -O-

 10.Compare the difference between two directories
You have two directories, say /foo and /bar, and you want to compare the difference between this two directories, you can use
# diff -r /foo /bar
If you only want to know which files difference, you can use
# diff -qr /foo /bar

 11.Test if local SMTP server is running
To test if a local SMTP server is running, you can try to ping the port 25
# echo -e "quit" | nc localhost 25
220 ubuntu ESMTP Postfix (Ubuntu)
221 2.0.0 Bye
If no SMTP server is listening
# echo -e "quit" | nc localhost 25localhost.localdomain [] 25 (?) : Connection refused

 12.Open two files side by side using vim
If you have two files, you want to open both of them in vi/vim and edit side by side, you can do the following.
# vim -O foo.txt bar.txt

 13.How to check if a file is locked in Linux?
Suppose a file test.txt is being locked by a program, e.g. using the flock system call, how can we know if this file is really
being locked?
# lsof test.txt
perl    5654 john 3uW  REG    8,1    1 983057 test.txt
The W means the file is currently held by an exclusive lock

 14.Count the number of lines contain a specific string using grep
Usually, when you want to count the number of lines contain a specific string, it is easy with the grep command,
# cat input.txt | grep foo | wc -l
But actually a more direct approach would be:
# cat input.txt | grep -c foo

15.How to compress a directory to a zip file
To compress a directory and all the files under this directory to a zip file, you can use
# zip -r directory

 16.Edit a remote file with vim over SSH
If you have a remote file and you can have a quick edit, if you use the following command:
# ssh -t [email protected] vim /data/test.txt
You will be prompted to login if you don't have proper ssh key in the remote server, and after finishing editing the file, you
will quit the ssh session automatically.

 17.Find the process ID of a running program
The easiest way to get all the process IDs of a running program in Linux, see the following example:
#  pidof apache2
29747 29745 29700 29698 29696 29695 29687 29678 29677 29664 23931

 18.What are the difference between tail -f and tail -F?
Basically these two commands are very similar, except tail -F do one more thing: the retry.
This is useful when you are tailing a web server log, e.g. apache log. When the log file got rotated, the tail -F will
continue to work since it will retry for the rotated file, while tail -f can't.

 19.How to kill and logout a logged in user
Assume john is logged in and you want to terminate his session, you can use the skill command.
# sudo skill -KILL -u john

 20.Extract a bzipped tar file
Normally tar is combined with gzip as the most common usage, however, sometimes people use bzip2 instead for maximum
compression rate. File in such format usually have the filename like program.tar.bz2
To extract it using a single command:
# tar -jxvf program.tar.bz2
The key is the argument - j

21.Monitor a directory for file modification
You have a directory to monitor, you want to know which files inside is currently changing.
Try the following magic command:
# watch -d -n 2 'df; ls -FlAt;'
When a file is changed, it will be highlighted.

22.Replace text in vi with confirmation
Similar to this article, you want to replace text, but you want to vi/vim to ask for confirmation before the actual replacements.

23.Replace text in vi
To replace a text in a file using vi/vim
Enter the command mode by typing:, and use the %s/foo/bar/g for global replacement(s).
All foo in the file will be replaced by bar

24.Case conversion of string in Linux
To convert a string to uppercase letter, you can use the following command.
# echo 'Test' | tr '[:lower:]' '[:upper:]'
On the other hand, if you want to convert from upper case to lower case, you can use
#echo 'Test' | tr '[:upper:]' '[:lower:]'

25.Specify a port with the scp command
If you want to use scp to copy the file to the remote machine, but that ssh server is listening at a port other than 22, you can use the following command syntax.
# scp -P 6000 test.txt [email protected]:/home/john/
Please, note it is -P, not -p.

26.List 10 most often used commands
You can list the top 10 most often used commands in your Linux, by the following method.
# history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head 
135 ls
63 cd
48 exit
46 sudo
34 tail
30 cat
29 df
18 date
17 vi
12 pwd

27.How to combine the output of multiple commands into a single file?
You can combine multiple commands by a bracket, and do the file redirection.
# (echo "A"; echo "B"; echo "C") > output.txt

28.How to add a line number to a text file?
It is very easy with the nl command, when you want to add a line number to a text file.
# nl file.txt

29.How to save a man page to a text file?
To save a man page to a text file, you can use the following command:
# man ls| col -b > ls.txt

30.How to extract a particular file from a tar file?
Assume you have a lot of files in a tar file, how do you extract only a particular file without extract all the files?
Just use the following command:
# tar xvf files.tar file.txt
Now only file.txt is extracted.

31.Quickly clear the screen
Previously we discussed how to clear the screen using the clear command. But the easiest way is to type Ctrl+l key in your terminal.
# [Ctrl+l]

32.Move a long-running job to background
Assume you have a job running for a very long time, and you might want it to run in the background, even you have logged out from the SSH session.
1. Suspend it
# [Ctrl-Z]
2. Make it run in background
# bg && disown
You can now safely logout from the SSH session.

33.Kill all processes listening on a particular port
To kill all processes listening on a particular port, e.g. port 80
# kill -9 $( lsof -i:80 -t )
Replace 80 by the port you want.

34.Find out which programs are listening TCP port
Use the command: netstat -tlnp
# netstat -tlnp
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0    *               LISTEN     
tcp        0      0   *               LISTEN     
tcp        0      0    *               LISTEN     

35.Display current system hardware architecture
arch can display the current system hardware architecture name (same as uname -m)
# arch

36.wget with rate limiting
When you are downloading a large file, and you want to limit the bandwidth used, it is easy with the wget command
wget --limit-rate=100k
The above command will limit the rate to be used not to exceed 100kbps.

37.Create a temp file in Linux
To create a temp file in Linux, you can use the mktemp command
# mktemp foo.XXXXXXXXXXX
mktemp will replace XXXXX... by random characters and create that temp file for you.

38.Create an empty file in Linux
Besides using the touch command, any easy tricks is to use the > redirection, e.g.
# > test.txt
Easy enough?

39.Difference between more and less command
Both the less and more help you to display the contents of a given file one screen at a time.
The differences:
more is available on all UNIX system, while less is not
less is more flexible, such as allow to scroll backward
If you are using modern Linux, you can always use less to replace more.

40.How to send message to a syslog
Basically, you need to use the logger command, e.g.
logger "have fun with logger"
You will find the log went to /var/log/messages
Jan 15 23:08:34 www john: have fun with logger

41.How to create a command shortcut
Sometimes, it is useful to set a command short but, by using the alias command
# alias hello='echo world'
# hello

42.Check how long server has been running
Use the uptime command
$ uptime
22:36:10 up 12 min,  1 user,  load average: 0.00, 0.04, 0.06

43.How to clear the screen in Linux
Use the clear command
# clear

44.Remove package with Yum
You can use the following command to remove packages using yum,
e.g. remove Firefox
yum remove firefox
To remove multiple packages, try
yum remove firefox gedit

45.How to remove installed package using RPM?
To remove installed package using rpm, use the following command
e.g. remove Firefox
rpm -e firefox
To remove multiple packages, try
rpm -e firefox gedit

46.Why sometimes kill does not work
Sometimes you might notice that kill statement does not work in some cases, e.g.
kill program
The reason is that the kill statement send a SIGTERM signal to the target process by default and tell the process to terminate itself. However, this signal 

can be catch up by the process and therefore can be ignored if the program's writer chooses to ignore.
To kill a process at all cost, use
kill -9 program
SIGKILL will be sent instead of SIGTERM, which cannot be caught or ignored.

No comments:

Post a Comment