Top 20 Linux Commands
1) clear command
clear is a standard operating system command which is used to clear the terminal screen. This command brings the command line on the top of the terminal. By pressing the ‘ctrl+L' shortcut key, you can also clear the terminal screen.
Syntax:
$ clear
Before using the clear command
After using the command, the terminal screen will look like this.
2) exit command
This command is used to close the terminal window or exit the shell. It takes one more parameter which exits the terminal or a shell.
Also, use the shortcut key ‘ctrl+shift+w’ to close the shell or terminal window and ‘ctrl+shift+q’ for close all the terminal tabs.
Syntax:
$ exit [n]
3) echo command
The echo command is used to print the line of text or a string or a number. It prints the string that passes as an argument in the command.
Syntax:
$ echo [option] [string]
Different options used in an echo command
(a) ‘/b’: ‘/b’ is a backspace option used with a backslash interpreter
‘-e.’
(b) ‘\t’: This option is used in a string for the horizontal tab spaces using the backslash interpreter ‘-e.’
(c) ‘\n’: ‘\n’ option is used to break a string or a line of text by using the backslash interpreter ‘-e.’
(d) ‘\v’: This option is used for the vertical tab space.
(e) ‘\c’: It is also used with the backslash interpreter ‘-e’ and produce no further string after it.
(f) ‘echo *’: This option ‘*’ displays the files and directories as similar to the ‘ls’ command.
Syntax:
$ echo*
4) history command
The history command displays all previously used commands since the session was started.
Syntax:
$ history
5) pwd command
pwd stands for present working directory which shows the path of the working directory starting from the root.
Syntax:
$ pwd
6) cd command
The 'cd' command is used to change the current working directory into its child or parent directory or any other directory (home or root directory).
Syntax:
$cd [directory_name]
Different types of cd commands in Linux
(a) ‘cd ..’: This command is used to change the current directory to the parent directory.
Syntax:
$ cd ..
(b) ‘cd/’: ‘cd/’ is used to move the user to the root directory.
Syntax:
$ cd/
(c) ‘cd~’: this command is used to change the current directory to home directory.
Syntax:
$ cd~
(d) cd dir1/dir2/dir3
This command is used to change a directory from the parent directory.
7) ls Command
The ls command shows the list of all the files and folders in the current working directory.
Syntax:
$ ls [option]
Options used with ls command
(a) ‘ls --version’: this option shows the version of ls.
Syntax:
$ ls --version
(b) ‘ls~’: This option ‘ls~' shows the list of files and directories of the home directory.
Syntax:
$ ls~
(c) ‘-a’: It shows the list of all the files and directories with the hidden files started with the .dot symbol in the current directory.
Syntax:
$ ls -a
(d) ‘-d */’: It is used to display only the list of directories.
Syntax:
$ ls -d */
(e) ‘-R’: This option of ls command displays the file of subdirectories also.
Syntax:
$ ls -R
8) mkdir command
mkdir stands for make directory and it is used to create a new directory in a file system. It takes one or more argument as a directory name. It also creates multiple directories by passing their directory names in the command. By using this command, you can also set permission to the directory.
Syntax1:
$ mkdir [option] directory_name
Syntax2:
$ mkdir dir1 dir2 dir3 ... dirN
- By using the ‘ls' command, you can verify that the directory is successfully created or not.
Different options used in mkdir command
(a) ‘--version’: This option shows the version of mkdir and its brief information about the license.
Syntax:
$ mkdir--version
(b) ‘-v’: This option of mkdir gives the message for each created directory.
Syntax:
$ mkdir -v directory_name
(c) ‘-p’: This option creates the parent directory as necessary.
Syntax:
$ mkdir -p dir1/dir2/..../dirN
(d) ‘--help’: This option shows the help related information.
Syntax:
$ mkdir --help
9) rmdir command
It stands for remove directory.
This command is used to remove the existing empty directory from the file system and takes one more argument as a directory name. It removes each directory specified in the rmdir command.
Syntax1:
$ rmdir [option] directory_name
Syntax2:
$ rmdir dir1 dir2 dir3 ... dirN
Different options used in rmdir command
(a) ‘--version’: This option displays the version of rmdir.
Syntax:
$ rmdir–version
(b) ‘-v’: This option of mkdir gives the message for each deleted directory.
Syntax:
$ rmdir -v directory_name
(c) ‘-p’: This option deletes or removes the child directory then its parent directory from the file system.
Syntax:
$ rmdir-p dir1/dir2/..../dirN
(d) ‘--help’: This option shows the general information or syntax of the command.
Syntax:
$ rmdir --help
10) cp command
The 'cp' command is used to copy the file or directory from one location (source) to another location (destination). This command takes at least two arguments as a file name. It can also copy more than one file in a single directory.
Syntax:
$ cp [option] Source Destination
- Copy the content of one file into the second file
Syntax:
$ cp first_file_name second_file_name
- Copy multiple files to a single directory
Syntax:
$ cp source_file1 Source_file2 ... Source_fileN Destination_directory
- Copy all the files(not directories) of source Directory to Destination Directory (*)
Syntax:
$ cp * Destination_directory
Different options used in the cp command
(a) ‘-i or -interactive’: This option warns you before overwriting the destination file. If you print ‘y,’ destination file is overwritten by the source file.
Syntax:
$ cp -i sourec_file Destination_file
(b) ‘-b’: This option creates a backup of each destination file in the same folder or directory with the different name and format.
Syntax:
$ cp --backup source_file destination_file
(c) ‘-R’: This option copies all the files of source directory to the destination directory.
Syntax:
$ cp -R source_directory_name path_of_dest_directory
(d) ‘-p’: This option preserves the attributes of source file like mode, timestamp, and ownership.
Syntax:
cp -p source_file Destination_file
(e) ‘--help’: This option shows or displays the general syntax or information about the cp command.
Syntax:
$ cp --help
11) mv command
‘mv’ stands for move and is used to move one and more files and directories from one place to another directory or location in a Linux file system.
This command is also used to rename a file, folder, and directory by passing the old name of a file and new name as an argument.
- To move directory
Syntax:
$ mv [option] source_directory destination_directory
- Move Multiple files in a single directory
Syntax:
$ mv file1 file2 .... fileN destination_directory_name
- To rename a file or directory
Syntax:
$ mv old_name new_name
12) touch command
The touch command is a standard command used in Linux for creating the empty files if not exits. It is also used to create, change and modify the timestamp (i.e., date and time) of the file. It takes at least one argument as a file name.
Syntax:
$ touch file_name
- Create multiple files
By using the touch command, you can also create multiple files by passing their names in the command.
Syntax:
$ touch file1 file2 file3 .... fileN
Different options used in touch command
Like all the other commands touch command also has the various option which are as follows:
(a) ‘ --version’: This option shows the version of touch and information regarding the license.
Syntax:
$ touch --version
(b) ‘-a’: This option is used to change the access time of a file.
Syntax:
$ touch -a file_name
(c) ‘-m’: This option changes the modification time of the file.
Syntax:
$ touch -m file_name
(d) ‘-r’: This option will update the timestamp (access and modify time) of file2 according to the timestamp of file1.
Syntax:
$ touch -r file1 file2
(e) ‘-t’: This option is used to create a file with specifies time rather than the current time.
Syntax:
$ touch -t YYMMDDHHMM.SS file_name
13) cat command
cat (concatenate) command reads the data from the file and displays the content of the file as an output on the terminal. It also helps to append the content of a text file to the end of another text file.
- Display the content of a file
You can display the content of a file by passing the file name as an argument in a command line.
Syntax:
$ cat [option] file_name
- Display the content of multiple files
You can also display the content of multiple files by passing their names as an argument in the cat command.
Syntax:
$ cat file1 file2 file3 ...... fileN
- Copy the content of one file into another file
Syntax:
$ cat source_file > destination_file
- Append the content of one file to the end of another file
Syntax:
$ cat source_file >> destination_file
- Display the content in reverse order
You can display the content of a file in reverse order by using the tac command.
Syntax:
$ tac file_name
Different options used in cat commands
(a) ‘-n’: This option shows the line number with each line in a file.
Syntax:
$ cat -n file_name
(b) ‘-E’: This option displays the ‘$’ sign at the end of the line.
Syntax:
$ cat -E file_name
(c) ‘-T’: This option displays the character (^I) for tab space if it is present in any line of a file.
Syntax:
$cat -T file_name
14) ln command
ln command is used to link two files and directories in Linux.
There are two types of link:
Hard link: Hard links create links between two or more filenames with the same inode number. By default, the ln command creates a hard link between files. It links directly to data on disk or the physical data.
Soft Link: It is also known as symbolic link or symlink. Symbolic link or soft link does not directly link to the data on disk they relate to another link.
- Create a hard link
Syntax:
$ ln file1 link1
If you delete a file ‘f1.txt', you can access the content of the file by ‘f2.txt' in the hard link as shown in the below figure:
Create a soft link/ symbolic link:
You can create a symbolic link or soft link by using the ‘-s.'
Syntax:
$ ln -s path_of_file_name link_name
If you delete the file ‘s1.txt', you cannot access the content of s1.txt by using the s2.txt in the symbolic link, as shown in the below figure, but it is possible in hard link:
15) date command
The ‘date’ command is used to display the current date and time of the system. A super-user can change the value of the system date and time.
Syntax:
$ date [option] [Format]
When you run date command without any option, then it will display the output as shown in below screen:
16) ps command
‘ps’ stands for ‘processes status,’ and this command is used to display the currently running processes with their process Identification number, i.e., PIDs along with some other information for each process depends on the different options.
Syntax:
$ ps [option]
If you execute the ps command without any option, it shows the processes for the current shell as shown in below screen.
Different options used in ps command
(a) ‘-A or -e’: This option prints all the processes running in your Linux system.
Syntax:
$ ps -A
(b) ‘au or axu’: This option displays all the process running in the BSD format.
Syntax:
$ ps au
(c) ‘ -U root -u root’: This option displays all the processes running as a root user (real and valid ID).
Syntax:
$ ps -U root -u root
(d) ‘ -e –forest’: This option displays the process tree (A process tree that shows how the processes linked to each other in a Linux system).
Syntax:
$ ps -e --forest
17) whoami command
whoami is mainly the concatenation of the string “who” ,”am”,”i” and when this command is executed, then it displays the username of the current user.
Syntax:
$whoami [option]
Different options used in the whoami command
(a) ‘--version’: This option displays the information about the version of whaomi.
Syntax:
$ whoami --version
(b) ‘--help’: This option gives the general syntax about whaomi command and its options.
Syntax:
$ whoami –help
18) passwd command
The passwd command changes the password of a user account in Linux. When you are changing the password, make sure that you are using a unique and strong password.
To change the password, run the following passwd command without any option:
Syntax:
$passwd
- ‘--help’: This option show information about how to use the passwd command.
Syntax:
$passwd --help
19) uname command
The uname command shows the information about the system, such as machine name, Operating system, and kernel.
Syntax:
$ uname [option]
Different options used in the uname command
(a) ‘-a’: This option displays the system information in an order as shown in below screen.
Syntax:
$ uname -a
(b) ‘-s’: This option displays the kernel name.
Syntax:
$ uname-s
(c) ‘-n’: This option displays the hostname of the network node.
Syntax:
$ uname-n
(d) ‘-v’: This option displays the current kernel’s version.
Syntax:
$ uname -v
(e) ‘-p’: This option prints the type of the processor.
Syntax:
$ uname-p
20) apt command
The Debian based ‘apt’ command stands for Advanced Package Tool. Apt is a package manager for Debian based systems like Ubuntu, Kubuntu, Lubuntu, etc., that automatically and intelligently search, update, install, and resolves dependency of packages on Linux system from command line Interface.
Syntax:
$apt-get package_name