0% found this document useful (0 votes)
7 views35 pages

Linux Commands

The document provides an overview of basic Linux commands and the vi editor, detailing their functionalities and usage. It categorizes commands into sections such as Directory, File, User, Filter, Utility, and Networking commands, offering examples for each. Additionally, it introduces the vi editor, explaining how to create and edit files within it.

Uploaded by

spareacc1684
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views35 pages

Linux Commands

The document provides an overview of basic Linux commands and the vi editor, detailing their functionalities and usage. It categorizes commands into sections such as Directory, File, User, Filter, Utility, and Networking commands, offering examples for each. Additionally, it introduces the vi editor, explaining how to create and edit files within it.

Uploaded by

spareacc1684
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Basic Linux Commands and vi Editor

Basic Linux Commands

The Linux command is a utility of the Linux operating system. All basic and advanced
tasks can be done by executing commands. The commands are executed on the Linux
terminal. The terminal is a command-line interface to interact with the system, which
is similar to the command prompt in Windows OS. Commands in Linux are case-
sensitive.

Linux provides a powerful command-line interface compared to other operating


systems such as Windows and macOS. We can do basic work and advanced work
through its terminal. We can do some basic tasks such as creating a file, deleting a file,
moving a file, and more. In addition, we can also perform advanced tasks such as
administrative tasks (including package installation, and user management),
networking tasks (ssh connection), security tasks, and many more.

Linux terminal is a user-friendly terminal as it provides various support options. To


open the Linux terminal, press "CTRL + ALT + T" keys together, and execute a
command by pressing the 'ENTER' key.

In this topic, we will discuss the top 50 most frequently used Linux commands with
their examples. These commands are very useful for a beginner and professionals both.
We have divided these commands into the following sections so that you can easily
identify their usage:

o Linux Directory Commands


o Linux File Commands
o Linux File Content Commands
o Linux User Commands
o Linux Filter Commands
o Linux Utility Commands
o Linux Networking Command

Linux Primer Command


Pwd Present working directory
Date It displays the system date and time
Who information about all the users will be displayed who have logged into the
system currently
Whoami information about the user name currently logged into the system
Finger displays complete information about all the users who are logged in
Cal command

Cal (year) displays calender


Cal 2016 calendar of a given year
Cal 07 2016 calendar of a given month
Cal April 2016 calendar of a given month only

Linux Directory Commands


1. pwd Command (Present working directory)

The pwd command is used to display the location of the current working directory.

Syntax:

pwd

Output:

2. mkdir or md Command

The mkdir command is used to create a new directory under any directory.

Syntax:

mkdir <directory name>

Output:

3. rmdir or rd 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:

Ls to list information about files and directories


Ls with no options provide a very basic listing of the files in your
current working directory
Ls -a displays including hidden files
Ls -l long listing
Ls -al it combines the functionality of the -a and -l options
Ls -r displays the contents in reverse order
Ls -t displays files and directories based on the date and time of creation.
Last file to first file
Ls a* display all files and directories starting with the letter a
Ls [aeiou]* display all files starting with the character mentioned in the brackets

5. chdir or 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. It creates a file with a size of zero bytes. This command does not
allow you to store anything in a file. It is used to create several empty files quickly.

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:
Cat > (file name) is used to create a new file and enter data
Cat > sample1 this command will create a file named sample1
Press enter to insert data
Press ctrl + z to save and exit

Cat sample1 to display the contents of a file

Cat >> sample1 to append


Cat (file1) (file2) displays the content of file1 followed by file2

8. rm Command

The rm command is used to remove a file.

Syntax:

rm <file name>

Output:

Rm (file name) used to delete a file


Rm (file1) (file2) deletes more than one file
Rm * deletes all files
Rm -r (directory name) allows deleting a directory including all files of a directory

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:
Cp file1 file2 Copy the contents of file1 to file2
Cp -i -i option will ask for confirmation before copying

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 along with a new file name if required>

Output:

Mv (file1) (file2) used for renaming a file, mv command copies the content from source
file to destination file and deletes the source file

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:
Cmp command

Cmp (file1) (file2) comparing 2 files

Wc command

Wc (file name) display total no. of lines, words and characters

Wc -l (file name) total no. of lines

Wc -w (file name) total no. of words

Wc -c (file name) total no. of characters

Wc -lw
Wc - wc

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:

Ps command

Ps stands for Process statistics. It is used check the status of process that are running in the
linux system

Ps -f provides full information

Ps -e every process running

Ps -ef

Grep ‘codetantra’/etc/passwd searches for all occurrences of the text string ‘codetantra’
within the /etc/passwd file

Grep ‘code’ * searches for all occurences of the text string ‘cod’ within all files of the
current directory

Ps -ef|grep ‘java’ searches for all occurences of active ‘java’ processes


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:

host <domain name> or <ip address>

Output:

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:

1. vi /home/sssit/Downloads/file.txt
Look at the above snapshot, we are creating a new file file.txt (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 Editor
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 the 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.

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:

1. 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.

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 editors.

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:

1. :<startLine,endLine> s/<oldString>/<newString>/g

Example:

Commands Action

:1,$ Replace forward with backward


s/readable/changed/ 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:

1. :ab <abbreviation> <abbreviatedWord>

Example:

Commands Action

:ab au abbrevition and Abbreviate au to be


unabbreviation '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

You might also like