143 Basic Linux Commands with Examples

By Bangar CH

September 5, 2017


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 file to another

cp file1 file2

8. Copy all files 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 file as text

cp file file1

Linux Commands about Symlink

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

ln -s file1 lnk1

13. Create a physical link to file or directory

ln file1 lnk1

14. View files of directory

ls

15. View files of directory

ls -F

16. Show details of files and directory

ls -l

17. Show hidden files

ls -a

18. Show files and directory containing numbers

ls *[0-9]*

19. Show files 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 file or directory

mv dir/file /new_path

24. Show the path of work directory

pwd

25. Delete file called 'file1'

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 file or directory - (YYMMDDhhmm)

touch -t 0712250000 file1

31. Show files 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/file
chmod 755 Linux_Directory
chmod 644 Linux_File

Different 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)

Briefing 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 file's owner may read, write, and execute the file. All others may read and execute the file. This setting is common for programs that are used by all users.

755 (rwxr-xr-x)

The file's owner may read, write, and execute the file. 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 file.

666 (rw-rw-rw-)

The owner may read and write a file, while all others may only read the file. A common setting for data files that everybody may read, but only the owner may change.

644 (rw-r--r--)

The owner may read and write a file. All others have no rights. A common setting for data files that the owner wants to keep private.

600 (rw-------)

How to use "Find Command"

The below Linux Commands gives you better Idea on find commands. You can also check more Find Commands in our other article too.

46. To find a file by name

find -name "File1"

47. To find a file by name, but ignore the case of the "File1"

find -iname "File1"

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

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

49. To find all files that are exactly 50 bytes

find /path -size 50c

50. To find all files less than 50 bytes

find /path -size -50c

51. To Find all files more than 700 Megabytes

find / -size +700M

52. To find files that have a modification time of a day ago

find / -mtime 1

53. To find files that were accessed in less than a day ago

find / -atime -1

54. To find files that last had their meta information changed more than 3 days ago

find / -ctime +3

55. To find files 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 file.

wc -l file_name OR cat file_name | wc -l

59. Prints the number of words in a file.

wc -w

60.  Displays the count of bytes in a file.

wc -c

61. Prints the count of characters from a file.

wc -m

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

wc -L

Compression Commands (tar, tar.gz, tar.bz2 and zip

Options to use the above Linux Commands

  • ​c - create a archive file.
  • x - extract a archive file.
  • v - show the progress of archive file.
  • f - filename of archive file.
  • t - viewing content of archive file.
  • j - filter archive through bzip2.
  • z - filter archive through gzip.
  • r - append or update files/directories to existing archive file.
  • w - verify a archive file.

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 specific directory

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

67. Untar Single file from tar File

tar -xvf compress.tar file1.txt

68. Untar Multiple files 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 specific directory

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

75. Untar Single file from tar.gz File

tar -zxvf compress.tar.gz file1.txt

76. Untar Multiple files 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 file from tar.bz2 File

tar -jxvf compress.tar.bz2 file1.txt

83. Untar Multiple files 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 file zip file)

87. To zipping a file or folder.

zip compress.zip file1 file2 folder1

88. To Zip individual files 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 files

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

zip compress.zip Folder/*

90. If there are sub folders and files 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 files stored in a zip file. 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 file in an archive without extracting entire zip file.

zip -d compress.zip path/to/file

93. To extract your files from a zip folder.

unzip compress.zip

94. To extract to a specific directory use -d option

unzip compress.zip -d /destination

95. To extract specific file from an archive

unzip compress.zip test.sh

Linux Commands for Special Attributes on Files

96. Allows write opening of a file only append mode

chattr +a file1

97. Allows that a file is compressed / decompressed automatically by the kernel

chattr +c file1

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

chattr +d file1

99. Makes it an immutable file, which can not be removed, altered, renamed or linked

chattr +i file1

100. Allows a file to be delete safely

chattr +s file1

101. Makes sure that if a file is modified changes are written in synchronous mode as with sync

chattr +S file1

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

chattr +u file1

103. Show specials attributes on file/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 file system partitions

fdisk -l

113. To view mounted file 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. Ifconfig  utility is used to configure 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 flexible 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 find 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 fine.

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 files 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 first 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.

 

Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Create a website in 3 simple steps

Choose a website template, add features, then customise! - Free Online Website builder.