0% found this document useful (0 votes)
46 views

143 Basic Linux Commands With Examples

Aws aws aws

Uploaded by

sumitghuge2009
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

143 Basic Linux Commands With Examples

Aws aws aws

Uploaded by

sumitghuge2009
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Home Domains  Web Hosting  Se

143 Basic Linux Commands


with Examples

In this article you will learn most frequently used Basic Linux Commands

with examples. We tried to cover as many Linux Commands as we can.

File Commands
1. The following Linux Command take you to the '/ home' directory

cd /home

2. This command go back one level


cd ..

3. This command takes you two folders back.

cd ../..

4. This command take you to home directory

cd

5. This command takes you to the user's home directory

cd ~user

6. This command takes you to the previous directory

cd -

"COPY" Commands in Linux


7. This command helps you copy one le to another

cp file1 file2

8. Copy all les of a directory within the current work directory

cp dir/* .

9. Copy a directory within the current work directory

cp -a /tmp/dir1 .
10. Copy a directory

cp -a dir1 dir2

11. Outputs the mime type of the le as text

cp file file1

Linux Commands about Symlink


12. Linux Command to create a symbolic link to le or directory

ln -s file1 lnk1

13. Create a physical link to le or directory

ln file1 lnk1

14. View les of directory

ls

15. View les of directory

ls -F

16. Show details of les and directory

ls -l

17. Show hidden les


ls -a

18. Show les and directory containing numbers

ls *[0-9]*

19. Show les and directories in a tree starting from root

lstree

20. Create a directory called 'dir1'

mkdir dir1

21. Create two directories simultaneously

mkdir dir1 dir2

22. Create a directory tree

mkdir -p /tmp/dir1/dir2

23. Move a le or directory

mv dir/file /new_path

24. Show the path of work directory

pwd

25. Delete le called ' le1'


rm -f file1

26. Remove a directory called 'dir1' and contents recursively

rm -rf dir1

27. Remove two directories and their contents recursively

rm -rf dir1 dir2

28. Delete directory called 'dir1'

rmdir dir1

30. Modify timestamp of a le or directory - (YYMMDDhhmm)

touch -t 0712250000 file1

31. Show les and directories in a tree starting from root(1)

tree

Linux Commands for Process


Management
32. The top command gives you information on the processes that

currently exist.

top

33. The htop command is like top, but prettier and smarter.
htop

34. Use the ps command to list running processes (top and htop list all

processes whether active or inactive).

ps

35. A step up from the simple ps command, pstree is used to display a

tree diagram of processes that also shows relationships that exist


between them.

pstree

36. The who command will display a list of all the users currently logged

into your Linux system.

who

37. As its name suggests, kill can be used to terminate a process with

extreme prejudice.

kill

38. The pkill and killall commands can kill a process, given its name.

pkill & killall

39. pgrep returns the process IDs that match it.

pgrep
40. With the help of nice command, users can set or change the priorities

of processes in Linux.

nice

41. It is similar to nice command. Use this command to change the

priority of an already running process.

renice

42. Gives the Process ID (PID) of a process

pidof

43. Gives free hard disk space on your system

df

44. Gives free RAM on your system

free

File Permissions
45. chmod the command for changing permissions

Syntax: chmod permission dir/ le

chmod 755 Linux_Directory


chmod 644 Linux_File
Di erent File Permissions

rwx rwx rwx = 111 111 111

rw- rw- rw- = 110 110 110

rwx --- --- = 111 000 000

rwx = 111 in binary = 7

rw- = 110 in binary = 6

r-x = 101 in binary = 5

r-- = 100 in binary = 4

7 = 4+2+1 (read/write/execute)

6 = 4+2 (read/write)

5 = 4+1 (read/execute)

4 = 4 (read)

3 = 2+1 (write/execute)

2 = 2 (write)

1 = 1 (execute)
Brie ng about Permissions in Linux
There is a huge importance with Linux Commands when we discuss

about Permissions. No restrictions on permissions. Anybody may do

anything. Generally not a desirable setting.

777 (rwxrwxrwx)

The le's owner may read, write, and execute the le. All others may

read and execute the le. This setting is common for programs that are

used by all users.

755 (rwxr-xr-x)

The le's owner may read, write, and execute the le. Nobody else has

any rights. This setting is useful for programs that only the owner may

use and must be kept private from others.

700 (rwx------)

All users may read and write the le.

666 (rw-rw-rw-)

The owner may read and write a le, while all others may only read the

le. A common setting for data les that everybody may read, but only

the owner may change.

644 (rw-r--r--)
The owner may read and write a le. All others have no rights. A

common setting for data les that the owner wants to keep private.

600 (rw-------)

How to use "Find Command"


The below Linux Commands gives you better Idea on nd commands.

You can also check more Find Commands in our other article too.

46. To nd a le by name

find -name "File1"

47. To nd a le by name, but ignore the case of the "File1"

find -iname "File1"

48. To search all les that end in ".conf"

find /path -type f -name "*.conf"

49. To nd all les that are exactly 50 bytes

find /path -size 50c

50. To nd all les less than 50 bytes

find /path -size -50c

51. To Find all les more than 700 Megabytes

find / -size +700M


52. To nd les that have a modi cation time of a day ago

find / -mtime 1

53. To nd les that were accessed in less than a day ago

find / -atime -1

54. To nd les that last had their meta information changed more than
3 days ago

find / -ctime +3

55. To nd les that were accessed in less than a minute ago

find / -mmin -1

56. If we want to match an exact set of permissions

find / -perm 644

57. If we want to specify anything with at least those permissions

find / -perm -644

Linux Commands to check Word


Count
58. Prints the number of lines in a le.

wc -l file_name OR cat file_name | wc -l


59. Prints the number of words in a le.

wc -w

60. Displays the count of bytes in a le.

wc -c

61. Prints the count of characters from a le.

wc -m

62. Prints only the length of the longest line in a le.

wc -L

Compression Commands (tar,


tar.gz, tar.bz2 and zip

Options to use the above Linux Commands

c - create a archive le.


x - extract a archive le.
v - show the progress of archive le.
f - lename of archive le.
t - viewing content of archive le.
j - lter archive through bzip2.
z - lter archive through gzip.
r - append or update les/directories to existing archive le.
w - verify a archive le.
About TAR Command
63. To Create tar Archive File

tar -cvf compress.tar /path/directory

64. To List Content of tar Archive File

tar -tvf compress.tar

65. To Untar tar Archive File

tar -xvf compress.tar

66. To Untar tar Archive File in a speci c directory

tar -xvf compress.tar -C /path/to diretory

67. Untar Single le from tar File

tar -xvf compress.tar file1.txt

68. Untar Multiple les from tar

tar -xvf compress.tar "file 1" "file 2"

69. Extract Group of Files using Wildcard from tar Archive

tar -xvf compress.tar --wildcards '*.txt'

70. To Add Files or Directories to tar Archive File


tar -rvf compress.tar file/dir

About TAR.GZ
71. To Create tar.gz Archive File

tar -cvzf compresstar.gz /path/directory

72. To List Content tar.gz Archive File

tar -tvf compress.tar.gz

73. To Untar tar.gz Archive File

tar -zxvf compress.tar.gz

74. To Untar tar.gz Archive File in a speci c directory

tar -zxvf compress.tar.gz -C /path/to diretory

75. Untar Single le from tar.gz File

tar -zxvf compress.tar.gz file1.txt

76. Untar Multiple les from tar.gz

tar -zxvf compress.tar.gz "file 1" "file 2"

77. Extract Group of Files using Wildcard from tar.gz Archive

tar -zxvf compress.tar.gz --wildcards '*.tzt'


78. To Add Files or Directories to tar.gz

tar -rvf compress.tar.gz file/dir

About TAR.BZ2
79. To Create tar.bz2 Archive File

tar -cvfj compress.tar.bz2 /path/directory

80. To List Content tar.bz2 Archive File

tar -tvf compress.tar.bz2

81. To Uncompress tar.bz2 Archive File

tar -xvf compress.tar.bz2

82. Untar Single le from tar.bz2 File

tar -jxvf compress.tar.bz2 file1.txt

83. Untar Multiple les from tar.bz2

tar -jxvf compress.tar.bz2 "file 1" "file 2"

84. Extract Group of Files using Wildcard from tar.bz2 Archive

tar -jxvf compress.tar.bz2 --wildcards '*.tzt'

85. To Add Files or Directories to tar.bz2


tar -rvf compress.tar.bz2 file/dir

86. To Verify tar, tar.gz and tar.bz2 Archive File

tar -tvfW cmpress.tar

Linux Commands for ZIP


ZIP (The extension .zip is not mandatory and this is useful

only to identify the le zip le)

87. To zipping a le or folder.

zip compress.zip file1 file2 folder1

88. To Zip individual les to a zip archive

zip compress.zip file1 file2 file3

Zipping a folder is a tricky thing as by default zip will not


zip entire folder content such as sub folders and les

89. To zip rst level of folder content use * as shown below

zip compress.zip Folder/*

90. If there are sub folders and les in 1 folder, in order to zip all content

of a folder use -r option

zip -r compress.zip Folder


91. To list all the les stored in a zip le. Any of the below commands can

be used and they give the same results.

unzip -l compress.zip

less compress.zip

zipinfo -1 compress.zip

92. To delete a le in an archive without extracting entire zip le.

zip -d compress.zip path/to/file

93. To extract your les from a zip folder.

unzip compress.zip

94. To extract to a speci c directory use -d option

unzip compress.zip -d /destination

95. To extract speci c le from an archive

unzip compress.zip test.sh

Linux Commands for Special


Attributes on Files
96. Allows write opening of a le only append mode

chattr +a file1
97. Allows that a le is compressed / decompressed automatically by the

kernel

chattr +c file1

98. Makes sure that the program ignores Dump the les during backup

chattr +d file1

99. Makes it an immutable le, which can not be removed, altered,

renamed or linked

chattr +i file1

100. Allows a le to be delete safely

chattr +s file1

101. Makes sure that if a le is modi ed changes are written in

synchronous mode as with sync

chattr +S file1

102. Allows you to recover the contents of a le even if it is canceled

chattr +u file1

103. Show specials attributes on le/folder

lsattr file/folder
Linux Commands to know System
Information
104. To know only system name, you can use uname command

uname

105. To view your network hostname

uname -n

106. To get information about kernel-version

uname -v

107. To get the information about your kernel release

uname -r

108. To get the information about your kernel release

uname -r

109. To print your machine hardware name

uname -m

110. All this information can be printed at once. The below two

commands gives same result.

uname -a
cat /proc/version

111. Find out information about the Linux distribution and version

cat /etc/*release*

112. To gather information about le system partitions

fdisk -l

113. To view mounted le systems.

mount

114. To view information about your CPU architecture such as number of


CPU’s, cores, CPU family model, CPU caches, threads, etc. Either of the

two below commands gives same output.

lscpu

cat /proc/cpuinfo

115. To view information about block devices

lsblk

Extract Information about Hardware Components using


"dmidecode"

116. To print information about memory. You can get the similar output

with all the below commands.


dmidecode -t memory

cat /proc/meminfo

free or free -mt or free -gt

117. To print information about system

dmidecode -t system

118. To print information about BIOS

dmidecode -t bios

119. To print information about processor

dmidecode -t processor

120. To dump all hardware information

dmidecode | less

Network Commands
121. PING (Packet Internet Groper) command sends packet requests to

the address you specify to test the connectivity between 2 nodes.

ping IP/hostname

122. Ifcon g utility is used to con gure network interface parameters.

Mostly we use this command to check the IP address assigned to the


system.

ifconfig -a

123. traceroute print the route packets take to network host.


Destination host or IP is mandatory parameter to use this utility

traceroute website.com / IP

124. route command is the tool used to display or modify the routeing

table.

route

125. dig (Domain Information Groper) is a exible tool for interrogating

DNS name servers. It performs DNS lookups and displays the answers

that are returned from the name servers.

dig website.com

126. Whois To know the information about domain like

whois website.com

127. Host Command to nd name to IP or IP to name

host hostname
host 1.2.3.4

128. telnet connect destination host:port via a telnet protocol if

connection establishes means connectivity between two hosts is working

ne.
telnet website.com 80

130. Tracepath traces the path of the network to the destination you

have provided. It attempts to list the series of hosts through which your

packets travel on their way to a given destination.

tracepath website.com

131. nslookup is a program to query Internet domain name servers.

nslookup website.com

132. netstat command allows you a simple way to review each of your

network connections and open sockets. netstat with head output is very

helpful while performing web server troubleshooting.

netstat

133. scp allows you to secure copy les to and from another host in the

network.

scp -r -P 22 (ssh port)


user@source_hostname:/path/to/dir /destination/path

134. nmap is a very powerful command, which checks the opened port

on the server.

nmap hostname -p 80

SSH Commands
135. Connect to host as user
ssh user@host

136. connect to host on port

ssh -p port user@host

KeyBoard Shortcuts
137. Halts the current command

Ctrl+C

138. Stops the current command, resume with fg in the foreground or bg

in the background

Ctrl+Z

139. Log out of current session, similar to exit

Ctrl+D

140. Erases one word in the current line

Ctrl+W

141. Erases the whole line

Ctrl+U

142. Type to bring up a recent command. You need to type the rst letter

of the command you are searching for.


Ctrl+R

143. Log out of current session

exit

Conclusion
Hope you will get better with the Linux Commands we covered in this

article. Leave your comments in the below comment box.

Other Lists of Linux Commands

Grep Commands

Putty Commands

Find Commands

cURL Commands

df Commands

SED Commands

Wget Commands

Rsync Commands

by Bangar CH | September 5, 2017

You might also like