Most Useful Linux Commands
One of the most enthralling features of the Linux operating system is its command line. It provides for you one of the most versatile command line among all operating systems. With a plethora of commands, you can perform each and every operation without even using the GUI once. Such is not the case in other operating system like Windows which treat the command line as secondary line of action.
Linux, as you also know, is available in different flavors known as the distributions. With the command line showing variations, it further increases the range of the commands that you need to be comfortable with. Although, it can take you some time to master all the commands in the command line, there are some basic commands that you should know before getting started. After getting a hang of these commands, you can move further into more complex ones. Here are some of the most useful commands used in Linux.
When you need help with any command and its usage/ options, always keep this one in mind:
1) man <command> : It provides you information on the command and its usage
User Commands
2) pwd (Present working directory) : Displays the directory you are currently accessing.
3) whoami: Shows current user
4) ls: This command is used for listing the contents of a directory. It comes with multiple options that fulfill different functions. You can use ‘man ls’ command to know more about the options available to use with ‘ls’.
Some of them are given below.
Syntax : ls <options>
Options:
-l create long list
-a include hidden directories and files
-d list for a specific directory or file
-R show complete tree structure
-h human readable form
Here is a sample for your reference:
[root@localhost /]# ls -l
total 60
lrwxrwxrwx. 1 root root 7 Jul 14 2016 bin -> usr/bin
dr-xr-xr-x. 4 root root 4096 Mar 13 2016 boot
drwxr-xr-x. 19 root root 3000 Mar 20 11:17 dev
drwxr-xr-x. 74 root root 4096 Mar 20 13:22 etc
drwxr-xr-x. 5 root root 4096 Mar 21 03:36 home
lrwxrwxrwx. 1 root root 7 Jul 14 2016 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Jul 14 2016 lib64 -> usr/lib64
drwx------. 2 root root 16384 Jul 14 2016 lost+found
drwxr-xr-x. 2 root root 4096 Jun 9 2016 media
drwxr-xr-x. 2 root root 4096 Jun 9 2014 mnt
drwxr-xr-x. 2 root root 4096 Jun 9 2016 opt
dr-xr-xr-x. 96 root root 0 Mar 20 11:16 proc
dr-xr-x---. 4 root root 4096 Mar 20 13:29 root
drwxr-xr-x. 23 root root 680 Mar 20 13:22 run
lrwxrwxrwx. 1 root root 8 Jul 14 2016 sbin -> usr/sbin
drwxr-xr-x. 2 root root 4096 Jun 9 2016 srv
dr-xr-xr-x. 13 root root 0 Mar 20 11:16 sys
drwxrwxrwt. 7 root root 4096 Mar 21 04:34 tmp
drwxr-xr-x. 13 root root 4096 Jul 14 2014 usr
drwxr-xr-x. 20 root root 4096 Mar 20 11:16 var
[root@localhost /]#
5) cd (Change Directory) : This command is used to navigate to a different directory.
Syntax: cd <path to new directory>
Variations:
cd navigate to home directory
cd .. navigate back one level
cd ../.. navigate back two levels
cd - navigate to last used directory
cd ~ Change to root directory
6) mkdir (make directory) : This command is used to create a new directory.
Syntax : mkdir <directory name>
Variations:
mkdir <directory1> <directory2> <directory3> creates multiple directories at once
mkdir <directory1>/<directory2>/<directory3> creates nested directories
7) rmdir (remove directory) : This command is used to remove a directory.
Syntax : rmdir <directory name>
Variations:
rmdir -p <directory1>/ <directory2> /<directory3>
(remove directory with its subsequent directories)
8) touch The touch command is used to create a empty file in a directory.
Syntax: touch <filename>
9) clear The ‘clear’ command is used to clear the screen and bring the cursor to the top of the page.
10) cat This command is used to view a file. It can also be used to concatenate multiple files
Syntax : cat <filename>
Variations:
head <filename> view the first ten lines of a file
tail <filename> view the last ten lines of a file
Combine two files into a new, third file: cat <file1> <file2> >> <file3>
11) cp This command is used to Copy a file or directory.
Syntax : cp <options> <source> <destination>
Options:
-r copy a directory and its contents (recursive)
-p copy with associated permissions
-f force operation
12) mv This command is used to Copy a file or directory or rename it.
Syntax : mv <source> <destination>
Rename a directory or file: mv <old name> <new name>
13) rm This command is used to delete a file or directory.
Syntax : rm <filename>
Variations:
rm -r delete a directory with its contents (recursive)
rm -f forced operation
14) find This command is used to search files and directories for a string. There are numerous variations for this command. We can refer them with the “man” command.
The most common are:
> “find / -iname <filename>” which searches all directories (not case-sensitive) for a file
> “find / -iname <*text*>” which searches all directories (not case-sensitive) for specific text
>replace “/” with the initial search directory such as /joesmith to limit the search
>use “-name” instead of “-iname” for case-sensitive searches
15) grep The grep command is used to find certain text within a file . Here is an example for your reference:
[root@localhost b]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 | grep -i bootproto
Output : BOOTPROTO=dhcp
variations :
grep -i ignores cases
grep -v inverts the match
16) vi It is a text editor used to edit files .When we give the command “vi” with the file name, it opens the file in the editor. It is a very versatile editor with multiple options to copy, paste or format a text. The editor is in the command mode by default. You can enter the insert mode simply by pressing “Esc i”.
17) shutdown As the name suggests, the ‘shutdown’ command is used to shutdown the system. There are certain variations that you can use while using the shutdown command. You can shutdown the system immediately or put a timer to it. System can also be rebooted with the shutdown command.
• Shutdown the system immediately
shutdown -h now
• Shutdown the system after 5 minutes
shutdown -h +5
• Reboot the system
shutdown -r now
18) More The more command is used to read files with large number of lines by providing an option to scroll. It can be used instead of the “cat” command with the similar syntax.
For example :
more <filename>
19) passwd This command is used to change the password of the current user.
Here is a sample for your reference.
[root@localhost network-scripts]# passwd
Changing password for user root.
New password:
20) exit This command is used to exit from the system.
Summary
Linux operating system contains a plethora of commands for numerous activities that it performs. The range is quite substantial. Through this article we were able to cover some of the most basic commands used in Linux common to most distributions.