30 Most Useful Linux Commands

30 Most Frequently Used Linux Commands With Examples

This blogpost gives a list of 30 Most Frequently Used Linux/Unix Commands With Examples. I tried to list out the most frequent commands, which will be useful for any Linux user in day to day life. I have also given examples with each command so that it will be easy for you to understand.

30 Most Useful Linux Commands


1. ls Command Examples : 

This is the most frequently used linux command and it list the the current directory.

ls without any option list the current working directory

# ls

ODBCDataSources         gai.conf             mail            rc4.d
X11                     gconf                mail.rc         rc5.d
adduser.conf            gnome                mailcap         rc6.d

By default ls without option won’t show the hidden files, to view the hidden files type

# ls -a
.                       fstab.d              magic.mime      rc4.d
..                      ftpusers             mail            rc5.d
.pwd.lock               gai.conf             mail.rc         rc6.d
ODBCDataSources         gconf                mailcap         rcS.d

to list files in Human Readable Format use the option -lh

# ls -lh

drwxr-xr-x   2 root root  20K Jul  2 15:56 bin
drwxr-xr-x   2 root root 4.0K Oct 23  2013 games
drwxr-xr-x  35 root root 4.0K Jul  2 15:55 include
drwxr-xr-x  51 root root 4.0K Jun 28 13:26 lib
drwxr-xr-x  10 root root 4.0K Oct 23  2013 local

to list files ordered by Last Modified Time use option -ltr

# ls -ltr

drwxr-xr-x   2 root root  4096 Oct 23  2013 src
drwxr-xr-x  10 root root  4096 Oct 23  2013 local
drwxr-xr-x   2 root root  4096 Oct 23  2013 games
drwxr-xr-x  51 root root  4096 Jun 28 13:26 lib

2. cd Command Examples

to change the directory in linux  we use cd command

[email protected]:/usr# cd bin
[email protected]:/usr/bin#

to goto one level up use cd .. command

[email protected]:/usr/bin# cd ..
[email protected]:/usr#

3. man Command Examples

sometimes you remember the command but forgot the options available with it, man command helps you here. It will show the manual of the command and list out all the options available with that command. Lets check ls command manual

[email protected]:/# man ls

       ls [OPTION]... [FILE]...

       List  information  about the FILEs (the current directory by default).  Sort entries alphabetically if none of -cftuvSUX
       nor --sort is specified.

       Mandatory arguments to long options are mandatory for short options too.

       -a, --all
              do not ignore entries starting with .

       -A, --almost-all
              do not list implied . and ..

              with -l, print the author of each file

       -b, --escape

it will show you all the options and their uses, to exit from man press q.

4. cp Command Examples

The cp command is  used to copy one file to other

# cp file1 file2

using cp with -p option will preserve the mode, ownership and time. using cp with -i option will prompt for overwriting the file.

5. mv Command Example

The mv command is used for move operation from one location to other.

to move the file from one location to other use command

# mv /usr/file1 /usr/bin/file1

it also acts as a rename command and to do that type

# mv file1 file2

above command will rename the file1 to file2

6. mkdir Command Example

In Linux/Unix we use mkdir command to create new directory, but to do that you must have write permission.

# ls
bin  file1  games  include  lib  local  sbin  share  src
# mkdir temp
# ls
bin  file1  games  include  lib  local  sbin  share  src  temp

if you need to create nested directory then you can use mkdir -p command

# mkdir -p temp1/temp2/temp3
# ls
bin  file1  games  include  lib  local  sbin  share  src  temp  temp1
# cd temp1
temp1# ls

 7. chmod Command Example

chmod command is used to set the permission on files or directory or alter previously set permission. Permission can be set using symbolic or octal codes.

u  user      +  to add a permission                   r  read
g  group     -  to remove a permission                w  write
o  other     =  to assign a permission explicitly     x  execute

to add read, write and execute permission for file we can do this by

chmod u=rwx tempfile

to add read and write permission to any directory we can do this by

chmod u+rw dir1

 8. date Command Example

date returns the current date and time of the system

# date
Tue Jul 29 13:42:51 EDT 2014

We can also format the output of date command like this

# date '+The date is %d/%m/%y, and the time is %H:%M:%S.'
The date is 29/07/14, and the time is 13:43:43.

 9. file Command Example

Some times you need to know the type of any file or what type of data it contains then you can use file command.

# file file1
file1: ASCII text

 10. tar Command Example

To compress a file you can use tar command.

# tar cf demo.tar temp
# ls
bin  demo.tar  file1  games  include  lib  local  sbin  share  src  temp

above command will create a archive demo.tar with contents of temp directory. To see the content of tar archive use this  option

# tar tvf demo.tar
drwxr-xr-x root/root         0 2014-07-30 13:03 temp/
-rw-r--r-- root/root        14 2014-07-29 14:04 temp/file1

to extract the content of archived file use xf option. Here x means extract and f means file. It will copy the content of archive in current directory.

# tar xf demo.tar

 11. grep Command Example

Some time you need to search with a pattern because you forgot the file name but partially you remember, in that case you can use grep command.

# grep is file1
  this is file1

In above example output will come with is highlighted in red. With grep you can search in multiple file for the same pattern

# grep is file1 file2 file3 file4

Also if you need to search for word codingbyte in all the available files which name is starting with a and are present in current directory type this.

# grep codingbyte a*

 12. ssh Command Example

ssh command is used to login to remote host securely. It provides encrypted communication between client and host.

ssh -l jsmith host.abc.com

 13. rmdir Command Example

To remove a directory you can use rmdir command. The directory you are removing must be empty.

# rmdir temp

If you need to deleted a directory which is not empty you can use option -r with rmdir

# rmdir -r demo

 14. rm Command Example

rm Command is used to remove a file or directory. To remove a file you must have write permission on the directory where file reside.

# rm file1

Before deleting if you want a confirmation then you can use -i option

# rm -i file1
rm: remove regular file 'file1'? y

to remove a directory and all the contents of it, you can use -r option

rm -r temp

Above command will first recursively delete the all the files from directory and sub directory and then delete the temp directory itself.

15.pwd Command Example

pwd command will show the current working directory of logged in user

# pwd

 16. ps Command Example

ps command is used to check the current running processes on the system. It will list out the process id and other detail of the process.

# ps
  PID TTY          TIME CMD
17488 pts/0    00:00:00 bash
19364 pts/0    00:00:00 ps

To see the full detail of processes use the option fu with ps

# ps fu
root     17488  0.0  0.2  18120  2068 pts/0    Ss   12:48   0:00 -bash
root     19407  0.0  0.1  15520  1152 pts/0    R+   14:17   0:00  \_ ps fu

17. passwd Command Example

If you need to change the password then you can use passwd command.

# passwd
Enter new UNIX password:

 18. more Command Example

Suppose you want to open a big file and read it page by page, this you cannot do with cat command. more command allow you to read a big file one page at a time. To quit before reaching end of file press q

# more file1

19. kill Command Example

To kill a running process you can use kill command, this command requires process id and that you can get from ps command.

# kill 19633

 20. lpr Command Example

To send the content of a file to print use lpr command.

# lpr -Pprinter1 file1

Here printer1 is the name of printer and file1 is the file you are going to print.

21. gzip Command Example

gzip command is used to create .gzip compressed file.

# gzip demo.txt

To uncompress the above file use below command

# gzip -d demo.txt.gz

 22. unzip Command Example

To uncompress the .zip file use unzip command

$ unzip demo.zip

It is also possible to see the content of zip file without uncompressing the file

unzip -l demo.zip

 23. shutdown Command Example

To turn off your system of schedule the turnoff you can use shutdown command

# shutdown -h now

Above command will turn of the system instantly. To schedule the turnoff after 20 minutes use below command

# shutdown -h +20

to reboot the system you can use below command

# shutdown -r now

 24. free Command Example

To check the usage of memory you can use free command. It will show the free memory, used memory and swap memory .

# free
             total       used       free     shared    buffers     cached
Mem:        786432     371760     414672          0          0     199080
-/+ buffers/cache:     172680     613752
Swap:            0          0          0

You can format the memory display using  -b, -k, -m and -g .

# free -m
             total       used       free     shared    buffers     cached
Mem:           768        348        419          0          0        194
-/+ buffers/cache:        154        613
Swap:            0          0          0

 25. top Command Example

To see the top processes from the system use top command. It will show the list sorted by cpu usage

top - 15:03:48 up 31 days,  3:10,  1 user,  load average: 0.18, 0.19, 0.10
Tasks:  27 total,   1 running,  26 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem:    786432 total,   397332 used,   389100 free,        0 buffers
KiB Swap:        0 total,        0 used,        0 free,   199900 cached

    1 root      20   0 26488 1064  288 S   0.0  0.1   0:05.47 init
    2 root      20   0     0    0    0 S   0.0  0.0   0:00.00 kthreadd/3722
    3 root      20   0     0    0    0 S   0.0  0.0   0:00.00 khelper/3722
   50 root      20   0 15224    8    4 S   0.0  0.0   0:00.00 upstart-file-br

 26. df Command Example

df command is used to see the disk usage of filesystem.

# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/simfs      52428800 2320664  50108136   5% /
none              393216       4    393212   1% /dev
none                   4       0         4   0% /sys/fs/cgroup
none               78644    1056     77588   2% /run
none                5120       0      5120   0% /run/lock

We can format the output  using -h option, it will show the output in human readable format.

# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/simfs       50G  2.3G   48G   5% /
none            384M  4.0K  384M   1% /dev
none            4.0K     0  4.0K   0% /sys/fs/cgroup
none             77M  1.1M   76M   2% /run
none            5.0M     0  5.0M   0% /run/lock

 27. whereis Command Example

To search the location of a specific linux command or a specific name  you can use whereis command.

# whereis cat
cat: /bin/cat /usr/share/man/man1/cat.1.gz
# whereis apache2
apache2: /usr/sbin/apache2 /etc/apache2 /usr/lib/apache2 /usr/share/apache2 /usr/share/man/man8/apache2.8.gz

 28. whatis Command Example

As name suggest whatis command will show one line description of any command .

# whatis cat
cat (1)              - concatenate files and print on the standard output
# whatis ls
ls (1)               - list directory contents

 29. tail Command Example

Suppose you want to see the last few lines of the file then you can use tail command. By default it will show 10 lines.

# tail file1

you can also define how many lines you want to see by using -n option. In below command it will show the N lines from the tail of the file1.

tail -n N file1

 30. wget Command Example

wget is used to download the software and other files from the internet. In my last post How To Install WordPress on Ubuntu VPS i have used this command to download the wordpress setup file from wordpress server.

wget http://wordpress.org/latest.tar.gz

I have tried my best to list out the 30 Most Frequently Used Linux Commands and given their example. Please give your suggestion in the comment and also share the post.


Leave a Reply

Your email address will not be published. Required fields are marked *