(Last Updated On: January 10, 2018)

Are you coming from Windows environment to Linux and in need of basic Linux commands to start with?, here I present to you top most frequently used Linux commands with examples. Since most Linux users coming from Windows environment may find it hard and stressful to work on the terminal, my aim here is to make your transition interesting and enjoyable.

The list of commands given here is good for starters, experienced Linux users may find most commands basic. But it’s a good resource for reference for any Linux user in general.

Let’s get started on our top most frequently used Linux commands with examples:

1) ls:  Used to list directory contents. ls will list information about the files, the current directory by default. Most used options are:
-a, –all –> do not ignore entries starting with. ( i.e list hidden files)
-h, –human-readable –> with -l, print sizes in human readable format (e.g., 1K  234M 2G)
-l –> use a long listing, this shows extra file attributes like the owner, size, permissions and extended attributes e.g sticky bit.
-r, –reverse –> reverse order while sorting, the list from last to first.

$ mkdir /tmp/test && cd /tmp/test
$ touch file1 file2 file3 
$ ls -lh 
total 0
-rw-r--r-- 1 jmutai jmutai 0 Jan 10 12:29 file1
-rw-r--r-- 1 jmutai jmutai 0 Jan 10 12:29 file2
-rw-r--r-- 1 jmutai jmutai 0 Jan 10 12:29 file3 

$  ls -lhr
total 0
-rw-r--r-- 1 jmutai jmutai 0 Jan 10 12:29 file3 
-rw-r--r-- 1 jmutai jmutai 0 Jan 10 12:29 file2
-rw-r--r-- 1 jmutai jmutai 0 Jan 10 12:29 file1

2) cd:    change to specific directory e.g

To move to a directory we create on 1 above, we’ll use:

$ cd /tmp/test

On which we can confirm added files,

$ ls -lh .ls -lh .
total 0
-rw-r--r-- 1 jmutai jmutai 0 Jan 10 12:29 file1
-rw-r--r-- 1 jmutai jmutai 0 Jan 10 12:29 file2
-rw-r--r-- 1 jmutai jmutai 0 Jan 10 12:29 file3

3) rm:    The rm command is used to remove files or directories on Linux file system.

Common options are:

-f, –force –> ignore nonexistent files and arguments, never prompt
-i –> prompt before every removal
-r, -R, –recursive –> remove directories and their contents recursively
-d, –dir –> remove empty directories
-v, –verbose –> explain what is being done
–help –> display this help and exit

To remove the three files we created previously, we’ll use:

$ cd /tmp/test

To remove single file,  this will remove file1

$ rm file1
$ ls

file2 file3

You can see that we now have two files, one deleted. To remove more than one file, separate them with whitespaces like below:

$ rm file2 file3
$ ls                  # now resturns empty list

A better way to remove all files on a folder is by using wildcard *. Look at example below

$ cd /tmp/test

Let’s create ten files:

$ touch file{1..10}.txt
$ ls
file10.txt file2.txt file4.txt file6.txt file8.txt
file1.txt file3.txt file5.txt file7.txt file9.txt

To delete all these files at once, use:

$ cd /tmp/test
$ rm *

If asked sure you want to delete all 10 files in /tmp/test [yn]? then press y.

You can also remove directory /tmp/test and its contents using -r flag:

$ rm -rf /tmp/test

If you want a prompt before file removal, maybe for double-checking, use -i flag. Will look something like below:

$ rm -i file1
rm: remove regular empty file 'file1'? y

4) pwd: This is used to print the name of the current working directory. It is essential if you’re not sure of your current working directory. The common option is :

-P, –physical –> Used to avoid all symlinks

$ cd /tmp/test
$ pwd
/tmp/test

5) chmod:    This is used to change file mode bits

Common options:

-R, –recursive –> change files and directories recursively
–preserve-root –> fail to operate recursively on ‘/’
–no-preserve-root –> do not treat ‘/’ specially (the default)
-f, –silent, –quiet –> suppress most error messages

Examples:

Make file script.sh, executable

$ chmod +x script.sh

Make the file script.sh, writable and executable by everybody. Rem read=4, write=2 and execute=1

$ chmod 775  script.sh

Change bits of folder /tmp/test and its files to read, write and execute for owner and group, but only read and execute for any other user.

$ chmod -R 775 /tmp/test 
$ ls -ldh /tmp/test
drwxrwxr-x 2 jmutai jmutai 40 Jan 10 12:59 /tmp/test

u,g and can be used to represent a user, group, and other users respectively. Examples:

$ chmod u=rx file  --> Grant the owner rx permissions, not w
$ chmod go-rwx file --> Deny rwx permission for group, others
$ chmod g+w file  --> Give write permission to the group
$ chmod a+x file1 file2 --> Give execute permission to everybody
$ chmod g+rx,o+x file --> OK to combine like this with a comma

6) chown: Used for changing ownership of a file. Common options being:

-f, –silent, — quiet:  Suppress most error messages
-R, — recursive: Operate on files and directories recursively
— no-preserve-root: Do not treat ‘/’ specially (the default)
–preserve-root:  Fail to operate recursively on ‘/’

Examples:

$ chown jmutai /tmp/test/file1 --> Change the owner of/tmp/test/file1 to "jmutai".
$ chown jmutai:admins /tmp/test/file1 --> Likewise, but also change its group to "admins".
$ chown -R jmutai /tmp/test -->  Change the owner of /tmp/test and subfiles to "root".
$ chown -R jmutai:admins /tmp/test --> Also group of /tmp/test and subfiles to admins

change owner to root and group to jose of file love.txt

7) chgrp:    change group ownership. Common options are:

-f, –silent, — quiet:  Suppress most error messages
-R, –recursive: Operate on files and directories recursively
–no-preserve-root: Do not treat ‘/’ specially (the default)
–preserve-root:  Fail to operate recursively on ‘/’

Examples:

$ chgrp admins /tmp/test/file1 --> Change the group of/tmp/test/file1 to "staff".
$ chgrp -hR admins /tmp/test --> Change the group of /tmp/test/ and subfiles to "staff".


8) ssh: a secure shell for remote connection.  It is OpenSSH SSH client for remote login: For a detailed guide on working with ssh, refer to:

ssh cheatsheet for Linux SysAdmins


9) man:    view command manual/ seek help
Example:

$ man ls 

will display ls command man page, paged through more command.

10) cd ..    moves up one directory
11) cd ../..  Moves up two directories.
12) mkdir:    create a new directory
Example: Typing the following command creates three directories, paul, copy and project. The parent directory being paul.

 $ mkdir -p  paul/copy/project

13) rmdir:    remove a directory, it will remove empty directories only. To remove a directory with its contents rm:

Example: The following command will remove an empty directory named project.

$ rmdir project

This will remove directory project and all of its contents:

$ rm -r project

14) locate:    Locate is used to find files by name in the file system.

# cd /root
# touch myfile1.txt
# updatedb  --> Update a mlocate database.
# locate myfile1.txt
/root/myfile1.txt

15) whereis: locate the binary, source, and manual page files for a specified command. Example is

$ whereis ping
ping: /usr/bin/ping /usr/share/man/man8/ping.8.gz

$ where su --> only binary
/usr/bin/su

16) cp:  cp is used to copy files/directories from one location to another. Common options

-i, –interactive –> prompt before overwrite
-l, –link –> hard link files instead of copying
-R, -r, –recursive –> copy directories recursively
-s, –symbolic-link –> make symbolic links instead of copying
-u, –update –> copy only when the SOURCE file is newer than the destination file
f, –force –> if an existing destination file cannot be opened, remove it and try again

$ cp myfile1.txt  ~/
$ cp -r /tmp/test ~/

# Copy only when the SOURCE file is newer than the destination file

$ echo "check 1" > myfile1.txt
$ cp myfile1.txt myfile2.txt
$ cp -u myfile1.txt myfile2.txt
$ cat myfile2.txt 
check 1
check2
$ cat myfile1.txt
check 1

You can see on use of -u that the file was not copied since the destination is newer than the source.

17) mv: mv command is used to move and rename files. Most common options are:

-i, –interactive –> prompt before overwrite
-v, –verbose –> explain what is being done
-u, –update –> copy only when the SOURCE file is newer than the destination file
f, –force –> if an existing destination file cannot be opened, remove it and try again

Examples:

$ mv file1 file2 > rename file1 to file2
$ mv -u file1 file2 > move file1 to file2 only if newer than file2
$ move file1 /home/ > move file1 from its current directory to /home directory

18) mount: Used to mount a filesystem in Linux. Most used options:

-a, –all –> mount all filesystems mentioned in fstab
-T,-fstab <path> –> Specify alternative file to /etc/fstab
-t, –types <list> –-> limit the set of filesystem types
-w, –rw, –read-write –> mount the filesystem read-write (default)
-v, –verbose –> say what is being done

Examples:

$ sudo mount /dev/sda6 /mnt --> mount /dev/sda6 partition to /mnt directory
$ sudo mount -t ext3 /dev/sda5 /mnt --> mount filesystem type of ext3 to /mnt

19) zip: Zip command is used to package, compress and archive files.It is analogous to a combination of the Unix commands tar and compress and is compatible with PKZIP:  Examples:

$ cd /tmp/test
$ zip test.zip *
 adding: myfile1.txt (stored 0%)
 adding: myfile2.txt (deflated 7%)
 adding: script.sh (stored 0%)
 adding: test (stored 0%)

20) unzip: list, test and extract compressed files in a ZIP archive. Examples:

List contents of a zipped file only:

$ unzip -l test.zip 
Archive: test.zip
 Length Date Time Name
--------- ---------- ----- ----
 8 2018-01-10 18:18 myfile1.txt
 15 2018-01-10 18:18 myfile2.txt
 0 2018-01-10 13:23 script.sh
 0 2018-01-10 13:23 test
--------- -------
 23 4 files

Extract zipped file:

$ unzip test.zip

unzip to a specific directory:

$ unzip -d /tmp/dir1

Test integrity of archive:

$ unzip -tq test.zip 
No errors detected in compressed data of test.zip.

21) umountUnmount a filesystem that is already mounted in Linux Unix systems.

-a, –all –> unmount all filesystems
-f, –force –> force unmount (in case of an unreachable NFS system)
-R, –recursive –> recursively unmount a target with all its children
-r, –read-only –> in case unmounting fails, try to remount read-only

Example:

$ sudo umount /mnt
$ sudo umount -R /many-mounts --> recursively unmount /many-mounts

22) df   Report file system disk space usage. The most commonly used options:

-h, –human-readable –> print sizes in powers of 1024 (e.g., 1023M)
-H, –si –> print sizes in powers of 1000 (e.g., 1.1G)
-t, –type=TYPE –> limit listing to file systems of type TYPE
-T, –print-type –> print file system type

Example:

$  df -hT -t ext4
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/arch-root ext4 117G 107G 4.8G 96% /
/dev/nvme0n1p2 ext4 727M 66M 608M 10% /boo

23) cat:    concatenate files and print on the standard output. This command can also be used to direct output to a file or append to a file.

$ cat myfile1.txt 
check 1

$  cat myfile2.txt
check 1
check2

$  cat myfile1.txt >> myfile2.txt 

$ cat myfile2.txt 
check 1
check2
check 1

23 ) ifconfig & ip : Configure and view IP related configurations. The complete guide is here:

24) top: top program provides a dynamic real-time view of a running system. It can display system summary information as well as a list of processes or threads currently being managed by the Linux kernel. To use top program just type top

$ top

25) netstat: Netstat prints network connections, routing tables, interface statistics, masquerade connections, and multicast memberships. Most used options are:

-a, –all –> Show both listening and non-listening sockets
-l, –listening –> Show only listening sockets
— numeric, -n –> Show numerical addresses instead of trying to determine symbolic host, port
–statistics , -s –> Display summary statistics for each protocol
–route, -r –-> Display the kernel routing tables

Examples:

Print routing table (use -n flag to disable DNS lookups):

$ netstat -rn
netstat -rn
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 10.87.0.1 0.0.0.0 UG 0 0 0 tun0
0.0.0.0 192.168.0.1 0.0.0.0 UG 0 0 0 wlp1s0

Which process is listening to port <port>

$ netstat -pln | grep <port> | awk '{print $NF}'

Fast display of ipv4 TCP listening programs

$ sudo netstat -vtlnp --listening -4

WARNING! netstat is deprecated. It has been replaced by ss command line tool. For comprehensive usage of netstat and ss, use:

26) nslookup: Nslookup is a program| utility used to query Internet domain name servers.  Nslookup operates in two modes: interactive and non-interactive. Interactive mode allows the user to query name servers for information about various hosts and domains or to print a list of hosts in a domain. The non-interactive mod is used to print just the name and requested information for a host or domain.

$ nslookup google.com
Server: 8.8.8.8
Address: 8.8.8.8#53

Non-authoritative answer:
Name: google.com
Address: 216.58.223.110
Name: google.com
Address: 2a00:1450:401a:805::200e

27) dig: This is a DNS lookup utility, dig stands for domain information groper. It is a flexible tool for interrogating DNS name servers.

Dig performs DNS lookups and displays the answers that are returned from the name server(s) that were queried. It is a great tool to troubleshoot DNS, flexibility and very easy to use. Examples:

Get IPv4 address for a domain, only answer section:

$ dig A google.com +short
216.58.223.110

Get IPv6:

$ dig AAAA google.com +short
2a00:1450:401a:805::200e

More examples can are on:

28) touch:    Touch command is used to change file timestamps, It mainly update the access and modification times of each file to the current time. You can also create an empty file with touch command. Usage:

$ touch file.txt --> create empty file if doesn't exist

29) free: Display amount of free and used memory in the system: common option is

h, –human –-> show human-readable output

$ free -h
 total used free shared buff/cache available
Mem: 7.4G 4.9G 1.5G 622M 1.0G 1.8G
Swap: 0B 0B 0B

 

Wrapping Up

We’ve covered top most frequently used Linux commands with examples. There are many more Linux commands but this list only covered those commands used by every Linux user – be it starter, average or advanced. In our next tutorial, we’ll cover more advanced commands for experienced Linux users and look at those commands crucial for troubleshooting Linux issues.