What are Shell Commands?


The shell is the command interpreter on the Linux systems. It the program that interacts with the users in the terminal emulation window. Shell commands are instructions that instruct the system to do some action.

Some of the commonly used shell commands are −

basename

This command strips the directory and suffix from filenames. It prints the name of the file with all the leading directory components removed. It also removes a trailing suffix if it is specified.

Example of basename is as follows −

$ basename country/city.txt

This gets the name of the file i.e. city which is present in folder country.

city.txt

cat

This command concatenates and prints the contents of the file. If there is no file, then it reads the standard input.

Examples of cat are as follows −

Let us see how to print the contents of a file −

$ cat example.txt

The above example displays the contents of the file example.txt.

This is the content of the example text file

Let us see how to concatenate two files −

$ cat example1.txt example2.txt > example3.txt
$ cat example3.txt

In the above example, the contents of the text files example1 and example2 are concatenated into the text file example3. Then the contents of example3 file are displayed.

This is the example1 text file
This is the example2 text file

cal

This command is used to display a calendar. If a single parameter is specified, then the four-digit year is displayed. If there are two parameters, that denotes the month and the year. No parameter means that the current month is displayed.

Example of cal is as follows −

$ cal

Since there is no parameter specified with cal, it returns the calendar of the current month i.e. September.

September 2018
Mo Tu We Th Fr Sa Su
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

cd

cd is used to change the current working directory to the required folder. If the directory name is given, then the shell’s name is changed to that directory. Otherwise, it changes to home.

Example of cd is as follows:

$ cd /user/local/example
$ pwd

The current directory is changed to example using cd and that is displayed using pwd command −

/user/local/example

cp

This command copies the contents of a file into another file.

Example of cp is as follows −

$ cat example.txt
This file is used as an example
$ cp example.txt example.bak
$cat example.bak
This file is used as an example

cp copies the contents of the text file example into the backup file example.bak

clear

The clear command clears the terminal screen. It ignores any command line parameters that may be present.

Example of clear is as follows −

$ clear

This clears the terminal screen.

cmp

This function compares two different files and reports the differences between them character by character. If the files differ, it tells the first byte and line number where they differ.

Example of cmp is as follows −

$ cat example1.txt
This is an example text file
$ cat example2.txt
This is also an example text file
$ cmp example1.txt example2.txt
example1.txt example2.txt differ: byte 10, line 1

This example demonstrates that the first difference in example1.txt and exampl2.txt is in line 1 and at byte 10.

mkdir

This command is used to create a directory in the Linux operating system.

Example of mkdir is as follows:

mkdir /fruit/apple

The above command creates a directory apple in the directory fruit.

rmdir

This command is used to remove a directory. All the files and subdirectories in a directory should be deleted first before deleting a directory.

Example of rmdir is as follows −

rmdir example

This deletes the directory example.

mv

The mv i.e. move command can be used for renaming directories.

Example of mv is as follows −

mv name1 name2

The initial name of the directory is name1 which is changed to name2.

Updated on: 22-Jun-2020

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements