Linux Directory Commands
1. pwd Command
The pwd command is used to display the location of the current working directory.
Syntax:
pwd
Output:
2. mkdir Command
The mkdir command is used to create a new directory under any directory.
Syntax:
mkdir <directory name>
Output:
3. rmdir Command
The rmdir command is used to delete a directory.
Syntax:
rmdir <directory name>
Output:
4. ls Command
The ls command is used to display a list of content of a directory.
Syntax:
ls
Output:
5. cd Command
The cd command is used to change the current directory.
Syntax:
cd <directory name>
Output:
Linux File commands
6. touch Command
The touch command is used to create empty files. We can create multiple empty files by executing it
once.
Syntax:
touch <file name>
touch <file1> <file2> ....
Output:
7. cat Command
The cat command is a multi-purpose utility in the Linux system. It can be used to create a file, display
content of the file, copy the content of one file to another file, and more.
Syntax:
cat [OPTION]... [FILE]..
To create a file, execute it as follows:
cat > <file name>
// Enter file content
Press "CTRL+ D" keys to save the file. To display the content of the file, execute it as follows:
cat <file name>
Output:
8. rm Command
The rm command is used to remove a file.
Syntax:
rm <file name>
Output:
9. cp Command
The cp command is used to copy a file or directory.
Syntax:
To copy in the same directory:
cp <existing file name> <new file name>
To copy in a different directory:
Output:
10. mv Command
The mv command is used to move a file or a directory form one location to another location.
Syntax:
mv <file name> <directory path>
Output:
11. rename Command
The rename command is used to rename files. It is useful for renaming a large group of files.
Syntax:
rename 's/old-name/new-name/' files
For example, to convert all the text files into pdf files, execute the below command:
rename 's/\.txt$/\.pdf/' *.txt
Output:
Linux File Content Commands
12. head Command
The head command is used to display the content of a file. It displays the first 10 lines of a file.
Syntax:
head <file name>
Output:
13. tail Command
The tail command is similar to the head command. The difference between both commands is that it
displays the last ten lines of the file content. It is useful for reading the error message.
Syntax:
tail <file name>
Output:
14. tac Command
The tac command is the reverse of cat command, as its name specified. It displays the file content in
reverse order (from the last line).
Syntax:
tac <file name>
Output:
15. more command
The more command is quite similar to the cat command, as it is used to display the file content in the
same way that the cat command does. The only difference between both commands is that, in case of
larger files, the more command displays screenful output at a time.
In more command, the following keys are used to scroll the page:
ENTER key: To scroll down page by line.
Space bar: To move to the next page.
b key: To move to the previous page.
/ key: To search the string.
Syntax:
more <file name>
Output:
16. less Command
The less command is similar to the more command. It also includes some extra features such as
'adjustment in width and height of the terminal.' Comparatively, the more command cuts the output in
the width of the terminal.
Syntax:
less <file name>
Output:
Linux User Commands
17. su Command
The su command provides administrative access to another user. In other words, it allows access of the
Linux shell to another user.
Syntax:
su <user name>
Output:
18. id Command
The id command is used to display the user ID (UID) and group ID (GID).
Syntax:
id
Output:
19. useradd Command
The useradd command is used to add or remove a user on a Linux server.
Syntax:
useradd username
Output:
20. passwd Command
The passwd command is used to create and change the password for a user.
Syntax:
passwd <username>
Output:
21. groupadd Command
The groupadd command is used to create a user group.
Syntax:
groupadd <group name>
Output:
Linux Filter Commands
22. cat Command
The cat command is also used as a filter. To filter a file, it is used inside pipes.
Syntax:
cat <fileName> | cat or tac | cat or tac |. . .
Output:
23. cut Command
The cut command is used to select a specific column of a file. The '-d' option is used as a delimiter, and
it can be a space (' '), a slash (/), a hyphen (-), or anything else. And, the '-f' option is used to specify a
column number.
Syntax:
cut -d(delimiter) -f(columnNumber) <fileName>
Output:
24. grep Command
The grep is the most powerful and used filter in a Linux system. The 'grep' stands for "global regular
expression print." It is useful for searching the content from a file. Generally, it is used with the pipe.
Syntax:
command | grep <searchWord>
Output:
25. comm Command
The 'comm' command is used to compare two files or streams. By default, it displays three columns,
first displays non-matching items of the first file, second indicates the non-matching item of the
second file, and the third column displays the matching items of both files.
Syntax:
comm <file1> <file2>
Output:
26. sed command
The sed command is also known as stream editor. It is used to edit files using a regular expression. It
does not permanently edit files; instead, the edited content remains only on display. It does not affect
the actual file.
Syntax:
command | sed 's/<oldWord>/<newWord>/'
Output:
27. tee command
The tee command is quite similar to the cat command. The only difference between both filters is that
it puts standard input on standard output and also write them into a file.
Syntax:
cat <fileName> | tee <newFile> | cat or tac |.....
Output:
28. tr Command
The tr command is used to translate the file content like from lower case to upper case.
Syntax:
command | tr <'old'> <'new'>
Output:
29. uniq Command
The uniq command is used to form a sorted list in which every word will occur only once.
Syntax:
command <fileName> | uniq
Output:
30. wc Command
The wc command is used to count the lines, words, and characters in a file.
Syntax:
wc <file name>
Output:
31. od Command
The od command is used to display the content of a file in different s, such as hexadecimal, octal, and
ASCII characters.
Syntax:
od -b <fileName> // Octal format
od -t x1 <fileName> // Hexa decimal format
od -c <fileName> // ASCII character format
Output:
32. sort Command
The sort command is used to sort files in alphabetical order.
Syntax:
sort <file name>
Output:
33. gzip Command
The gzip command is used to truncate the file size. It is a compressing tool. It replaces the original file
by the compressed file having '.gz' extension.
Syntax:
gzip <file1> <file2> <file3>...
Output:
34. gunzip Command
The gunzip command is used to decompress a file. It is a reverse operation of gzip command.
Syntax:
gunzip <file1> <file2> <file3>. .
Output:
Linux Utility Commands
35. find Command
The find command is used to find a particular file within a directory. It also supports various options to
find a file such as byname, by type, by date, and more.
The following symbols are used after the find command:
(.) : For current directory name
(/) : For root
Syntax:
find . -name "*.pdf"
Output:
36. locate Command
The locate command is used to search a file by file name. It is quite similar to find command; the
difference is that it is a background process. It searches the file in the database, whereas the find
command searches in the file system. It is faster than the find command. To find the file with the
locates command, keep your database updated.
Syntax:
locate <file name>
Output:
37. date Command
The date command is used to display date, time, time zone, and more.
Syntax:
date
Output:
38. cal Command
The cal command is used to display the current month's calendar with the current date highlighted.
Syntax:
cal<
Output:
39. sleep Command
The sleep command is used to hold the terminal by the specified amount of time. By default, it takes
time in seconds.
Syntax:
sleep <time>
Output:
40. time Command
The time command is used to display the time to execute a command.
Syntax:
time
Output:
41. zcat Command
The zcat command is used to display the compressed files.
Syntax:
zcat <file name>
Output:
42. df Command
The df command is used to display the disk space used in the file system. It displays the output as in
the number of used blocks, available blocks, and the mounted directory.
Syntax:
df
Output:
43. mount Command
The mount command is used to connect an external device file system to the system's file system.
Syntax:
mount -t type <device> <directory>
Output:
44. exit Command
Linux exit command is used to exit from the current shell. It takes a parameter as a number and exits
the shell with a return of status number.
Syntax:
exit
Output:
After pressing the ENTER key, it will exit the terminal.
45. clear Command
Linux clear command is used to clear the terminal screen.
Syntax:
clear
Output:
After pressing the ENTER key, it will clear the terminal screen.
Linux Networking Commands
46. ip Command
Linux ip command is an updated version of the ipconfig command. It is used to assign an IP address,
initialize an interface, disable an interface.
Syntax:
ip a or ip addr
Output:
47. ssh Command
Linux ssh command is used to create a remote connection through the ssh protocol.
Syntax:
ssh user_name@host(IP/Domain_name)</p>
48. mail Command
The mail command is used to send emails from the command line.
Syntax:
mail -s "Subject" <recipient address>
Output:
49. ping Command
The ping command is used to check the connectivity between two nodes, that is whether the server is
connected. It is a short form of "Packet Internet Groper."
Syntax:
ping <destination>
Output:
50. host Command
The host command is used to display the IP address for a given domain name and vice versa. It
performs the DNS lookups for the DNS Query.
Syntax:
1. host <domain name> or <ip address>
Output:
Vi Editor with Commands
What is vi
The vi editor is elaborated as visual editor. It is installed in every Unix system. In other words, it is
available in all Linux distros. It is user-friendly and works same on different distros and platforms. It is a
very powerful application. An improved version of vi editor is vim.
The vi editor has two modes:
o Command Mode: In command mode, actions are taken on the file. The vi editor starts in command
mode. Here, the typed words will act as commands in vi editor. To pass a command, you need to be in
command mode.
o Insert Mode: In insert mode, entered text will be inserted into the file. The Esc key will take you to the
command mode from insert mode.
By default, the vi editor starts in command mode. To enter text, you have to be in insert mode, just
type 'i' and you'll be in insert mode. Although, after typing i nothing will appear on the screen but
you'll be in insert mode. Now you can type anything.
To exit from insert mode press Esc key, you'll be directed to command mode.
PauseNext
Unmute
Current Time 0:07
/
Duration 18:10
Loaded: 4.40%
Â
Fullscreen
If you are not sure which mode you are in, press Esc key twice and you'll be in command mode.
Using vi
The vi editor tool is an interactive tool as it displays changes made in the file on the screen while you
edit the file.
In vi editor you can insert, edit or remove a word as cursor moves throughout the file.
Commands are specified for each function like to delete it's x or dd.
The vi editor is case-sensitive. For example, p allows you to paste after the current line while P allows
you to paste before the current line.
vi syntax:
vi <fileName>
In the terminal when you'll type vi command with a file name, the terminal will get clear and content of
the file will be displayed. If there is no such file, then a new file will be created and once completed file
will be saved with the mentioned file name.
Linux vi example
Let's understand vi through an example:
To start vi open your terminal and type vi command followed by file name. If your file is in some other
directory, you can specify the file path. And if in case, your file doesn't exist, it will create a new file with
the specified name at the given location.
Example:
vi /home/sssit/Downloads/[Link]
Look at the above snapshot, we are creating a new file [Link] (as this file doesn't exist) and have
entered the full path for the directory Downloads.
Command mode
This is what you'll see when you'll press enter after the above command. If you'll start typing, nothing
will appear as you are in command mode. By default vi opens in command mode.
Look at the above snapshot, it is blank as it is a new file. To start typing, you have to move to the insert
mode. At the end of the terminal window, directory name and file name are displayed.
Insert mode
To move to the insert mode press i. Although, there are other commands also to move to insert mode
which we'll study in next page.
Look at the above snapshot, after pressing i we have entered into insert mode. Now we can write
anything. To move to the next line press enter.
Once you have done with your typing, press esc key to return to the command mode.
To save and quit
You can save and quit vi editor from command mode. Before writing save or quit command you have
to press colon (:). Colon allows you to give instructions to vi.
exit vi table:
Commands Action
:wq Save and quit
:w Save
:q Quit
:w fname Save as fname
ZZ Save and quit
:q! Quit discarding changes made
:w! Save (and write to non-writable file)
To exit from vi, first ensure that you are in command mode. Now, type :wq and press enter. It will save
and quit vi.
Type :wq to save and exit the file.
Look at the above snapshot, command :wq will save and quit the vi editor. When you'll type it in
command mode, it will automatically come at bottom left corner.
If you want to quit without saving the file, use :q. This command will only work when you have not
made any changes in the file.
Look at the above snapshot, this file is modified and hence on typing :q it displays this message at
bottom left corner.
The above file can be saved with the command :!q. It discards the changes made in the file and save it.
Look at the above snapshot, we have typed :!q, it will save our file by discarding the changes made.
Vi Commands
Linux vi editor is different from other editors. You have to use different keys to use different functions.
Although, it's quite easy and interesting to use vi editor.
The vi editor commands are case sensitive.
Have a look at the vi commands in the following table.
To switch from command to insert mode:
Command Action
i Start typing before the current character
I Start typing at the start of current line
a Start typing after the current character
A Start typing at the end of current line
o Start typing on a new line after the current line
O Start typing on a new line before the current line
To move around a file:
Commands Action
j To move down
k To move up
h To move left
l To move right
To jump lines:
Commands Action
G Will direct you at the last line of the file
`` Will direct you to your last position in the file
To delete:
Commands Action
x Delete the current character
X Delete the character before the cursor
r Replace the current character
xp Switch two characters
dd Delete the current line
D Delete the current line from current character to the end of the line
dG delete from the current line to the end of the file
To repeat and undo:
Commands Action
u Undo the last command
. Repeat the last command
Command to cut, copy and paste:
Commands Action
dd Delete a line
yy (yank yank) copy a line
p Paste after the current line
P Paste before the current line
Command to cut, copy and paste in blocks:
Commands Action
<n>dd Delete the specified n number of lines
<n>yy Copy the specified n number of lines
Start and end of line:
Commands Action
θ Bring at the start of the current line
^ Bring at the start of the current line
$ Bring at the end of the current line
dθ Delete till start of a line
d$ Delete till end of a line
Joining lines:
Commands Action
J Join two lines
yyp Repeat the current line
ddp Swap two lines
Move forward or backward:
Commands Action
w Move one word forward
b Move one word backward
<n>w Move specified number of words forward
dw Delete one word
yw Copy one word
<n>dw Delete specified number of words
Search a string:
Commands Action
/string Forward search for given string
?string Backward search for given string
/^string Forward search string at beginning of a line
/string$ Forward search string at end of a line
n Go to next occurrence of searched string
/\<he\> Search for the word he (and not for there, here, etc.)
/pl[abc]ce Search for place, plbce, and plcce
Replace all
Syntax:
:<startLine,endLine> s/<oldString>/<newString>/g
Example:
Commands Action
:1,$ s/readable/changed/ Replace forward with backward from first line to the last line
:3,6 s/letters/neww/g Replace forward with backward from third line to the ninth line
Text buffers:
Commands Action
"add Delete current line and put text in buffer a
"ap Paste the line from buffer a
Abbreviation
Syntax:
:ab <abbreviation> <abbreviatedWord>
Example:
Commands Action
:ab au abbrevition and unabbreviation Abbreviate au to be 'abbrevition and unabbreviation'
:una au Un - abbreviate au
Vi Editor Index
Vi Editor
o Vi Editor
o 1) switch
o 2) move
o 3) jump
o 4) delete
o 5) undo and repeat
o 6) cut, copy, paste
o 7) cut, copy, paste blocks
o 8) Start and end
o 9) Joining
o 10) word
o 11) Search
o 12) Replace all
o 13) Text Buffers
o 14) Abbreviations
What is Shell Scripting
A shell script is a type of computer program developed to be executed by a Unix shell, which is also known as a
command-line interpreter. Several shell script dialects are treated as scripting languages. Classic operations
implemented by shell scripts contain printing text, program execution, and file manipulation. A script configures the
environment, executes the program, and does necessary logging or clean-up is known as a wrapper.
How to determine Shell
You can get the name of your shell prompt, with following command :
Syntax:
echo $SHELL
Shell Scripting She-bang
The sign #! is called she-bang and is written at top of the script. It passes instruction to program /bin/sh.
To run your script in a certain shell (shell should be supported by your system), start your script with #! followed by the shell
name.
Example:
#!/bin/bash
echo Hello World
#!/bin/ksh
echo Hello World
Shell Scripting Practical:-
A. Write a shell script to print a message.
B. Write a shell script to access arguments passed on command line.
C. Write ashell script to craete files with the name passed on command line
D. Write a shell script to input number from user and display its factorial
E. Write a shell script to input number from user and display wheather it is prime number or not.
How to create file in Linux
To create files and write the data into them.
cat >filename
ctrl + C is used to save the content of file
To display content of file.
Cat file
Creating file using the `touch` command
We can create an empty file (or multiple empty files) using touch command in Linux. But its main purpose is to
change or update the time-stamp of a file.
touch filename
touch file_1 file_2 file_3 file_4 (multiple file can also be created at a time)
Creating File using `vi` or `vim` command in Linux
Its main function is to edit files. It is commonly used by programmers to edit the textual content of any file on vi text
editor.
Note: To save and exit from the vi-text editor, press the Escape key and then type :wq and hit enter.
vi file_1