0% found this document useful (0 votes)
147 views14 pages

Essential Linux Commands Guide

This document provides over 100 Linux commands organized into categories including system information commands, networking commands, file creation commands, searching commands, sed commands, awk commands, common commands, RPM commands, and compressed file commands. The commands can be used to display system details, manage files and directories, view and edit files, install packages, work with networks, compress and uncompress files, and more.

Uploaded by

tahazzad hossain
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)
147 views14 pages

Essential Linux Commands Guide

This document provides over 100 Linux commands organized into categories including system information commands, networking commands, file creation commands, searching commands, sed commands, awk commands, common commands, RPM commands, and compressed file commands. The commands can be used to display system details, manage files and directories, view and edit files, install packages, work with networks, compress and uncompress files, and more.

Uploaded by

tahazzad hossain
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

Linux Commands

=========================================================

System information Commands


===========================
1. date :- to display the current system date and time
2. host name:- to display the system name
3. uname :-to display the os name
4. uname –a :- to display os name,kernel version,hostname,os bit
5. Uname –r :to display the kernel version
6. uptime:- to display system uptime ,load avg ,how many users working
7. Su – username :- it switches one user to another user
8. Vim /etc/passwd:- user information location path
9. su – username :- switch one user to another user
10. Arch :- to display the os bit version
11. Cat /proc/version :- to display the kernel version
12. cat /etc/redhat-release :-to know the linux version
13. Service iptables status
Service iptables start
Service iptables stop
14. Service servicename status
Service servicename start
Service servicename stop
Networking commands
====================
15. ifconfig:- to display the system Ip and mac address
16. Ifconfig eth0 :- to display only ip address
17. netstat –anlp |grep “:80” :- to see the which service is running 80
port number
18. ping hostname :- to display to see the connection remote system
available or not
19. netstat –anlp |grep httpd :- to see the apache running or not
20. dig hostname :- to display the response time on remote server
21. nslookup hostname :- to display the hostname is dns registered or
not
22. telnet hostname portno:- to see the remote server and port is
available or not

nc -vz hostname portno:- to see the remote server and port is available or
not

here :-nc means net cat

23. Nmap hostname:- it is used for port number scanner


24. Traceroute hostname/websitename:- to reach the website path in 30
hops to display
File creation commands
=========================
25. Vim filename :- to display the file content and add the file content
Esc mode:
:wq ---save and quit
:q!—forcebully quit
Yy – copy the line,P --- paste the line
Shift +gg ----- go to firstline
Shift+g -------go to last line
U –undo last delete or modified line
:set nu---to be set number on file
Dd --- to delete the line
/searchkeyword :-- to be search keyword in particular file existed or
not
n – to search next word by top to down
N – to search next word by down to top
:%/oldkeyword/newkeyword/gc it is replace old name to newname
I ---insert the data
26. cat >[Link] :- to be create file and to give the data and save
the data in a file
27. cat filename:- to be see the content of the file
28. cat >> [Link] :- to be append the data already existing file
29. touch:- to be create the empty file
30. touch filename:- to be create the empty or change the timestamp
for file
31. View filename:- to display the content of the file
32. more filename:- it displays the content of file
33. Head filename :- to display the first 10 lines in a file
34. Head -100 filename :- to display the first 100 lines in a file
35. Head –n 5 filename :- to display the first 5 lines in a file
36. tail filename :- to display the last 10 lines in a file
37. tail -50 filename :- to display the last 50 lines in a file
38. tail –f filename :- to see the running content process file
39. Rm file:- it remove the file with conformation
40. Rm –rf filename :– it remove the file without confirmation
Searching Commands
=====================
41. locate filename:- to search entire file system for given file name
42. find / -name filename:- to search entire / file system for given file
name
43. find . –name filename:- it searches the file in current directory
44. find . –iname filename:- it searches the file in current directory with
ignore the case sensitive
45. find / -type f –name filename:- it searches the file in the file system of /
46. find / -type d –name dirname :- it searches the directory in the file
system of /
47. find / -type f –name filename –exec rm –rf {}\; :-to find the file and
remove that file
48. find / -type f –empty :- it display the empty file
49. find / -type d –empty :- it display the empty directories
50. find / -mtime 50 :- to be find the last 50 days modified files
51. find / -mtime +50 –mtime -100 :- to be find the last 50 days to 100days
modified files.
52. find / -mmin -60:- it displayes the last one hour modified file
53. find / -cmin -60:- it displayes the last one hour changed file
54. find / -atime 50:- it displayes the last 50 days accessed file
55. find / -size 50M :- it displayes the 50 mb files.
56. find / -size +50M –size -100M :- it displayes the 50 mb to 100mb files.
57. Find / -size +100M –exec rm –rf {} \; :- it finds the 100 mb files and
delete the files

How to find the largest file in the current directory and sub
directories
find . -type f -exec ls -s {} \; | sort -n -r | head -1
How to find the smallest file in the current directory and sub
directories
find . -type f -exec ls -s {} \; | sort -n -r | tail -1

or

find . -type f -exec ls -s {} \; | sort -n | head -1

How to find the files which are created between two files.

find . -cnewer f1 -and ! -cnewer f2

How to find the permissions of the files which contain the name
"java"?
find -name "*java*"|xargs ls -l

Alternate method is
find -name "*java*" -exec ls -l {} \;

Find the files which have the name "java" in it and then display
only the files which have "class" word in them?
find -name "*java*" -exec grep -H class {} \;

How to remove files which contain the name "java".


find -name "*java*" -exec rm -r {} \;

find and copy


-------------
find . -type f -name "*.mp3" -exec cp {} /tmp/MusicFiles \;
# cp *.mp3 files to /tmp/MusicFiles
copy one file to many dirs
--------------------------
find dir1 dir2 dir3 dir4 -type d -exec cp [Link] {} \;
# copy the file [Link] to those dirs

How to Find and Delete Files in the Linux Command Line

find . -name "*.bak" -delete

Find files with different file extensions

find . -type f \( -name "*.c" -o -name "*.sh" \)

find . -iname foo

If you’re just interested in directories, search like this:

find . -iname foo -type d

And if you’re just looking for files, search like this:

find . -iname foo -type f


Sed Command
====================
58. Sed ‘s/oldname/newname/’ filename :- to change first occurrence of
the each line old name to new name
59. Sed ‘s/oldname/newname/2’ filename :- to change second
occurrence of the each line old name to new name
60. Sed ‘s/oldname/newname/g’ filename :- to change all occurrence of
the each line old name to new name.
61. Sed –n ‘/name/p’ filename :- to print pattern match the line
62. Sed ‘/^#\|^$| -*#/d’ [Link] : to empty line file and start the #
beginning remove that lines

Awk Command
===================
63. Awk {print;} filename:-
64. Awk ‘/searchword/’ filename
65. Awk ‘{print $2,$3;}’ filename
66. Awk ‘{print $2,$5;}’ filename
67. Awk ‘{print $2,$NF;}’ filename
68. Awk ‘BEGIN {print col1 \t col2 \t…}
Awk “BEGIN {print $2 ,”\t” $3,;}
69. Awk ‘$1>200 ‘ filename :-
70. Awk ‘BEGIN {count=0:}
$4 ~ /searchkeyword/{count ++;}
END {print “no .ofmatching records:=,count;}’ filename
Linux Common Commands
============================
71. pwd :-present working directory
72. w:- to display system uptime ,load avg ,how many users
working full information of users
73. who:-to display the current active users
74. whoami:- current user name
75. who am i:-it gives first login user in the system command
prompt
76. which command :- to know the execution command location
path
77. cd:- to go the home directory
78. cd ~ :- to go the home directory
79. cd ..:- to go the previous directory
80. cd /somepath:- to go the given path
81. man :- to given the commands information
82. ls :- to display the list of files and directories
83. ls –a :- to display the list of files and directories and hidden
directories and files
84. ls –l :-
85. ls –lt:-it display the files with time based
86. ls –ltr:- it display the files with time reverse order
87. ls –ltrh:- it display the files with human readable format (like
gb,mb)
Rpm Commands
=================
88. Rpm –ivh rpmpkgname :- to install rpm package independently(with
in dependence)
89. Rpm –qa :- to display the all rpm packages installed details
90. Rpm –q kernel :- it display the kernel version
91. Rpm –e packagename :- to uninstall the rpm package
92. Rpm –l package :- to search the rpm location path
Compressed Commands
============================
93. Tar –cvf [Link] directory/filepath:- to create tar file
94. Tar –xvf [Link]:- to extract the tar file
95. Tar –cvf [Link] directory1 directory2 directory3:- to create tar
file multiple directory
96. Tar –xvf [Link] –d directorypath:- to extract the tar file in
specific location
97. Tar –tvf [Link]:- to see the contents tar file without extract
98. Tar –czvf [Link] directory1 directory2 directory3:- to create
tar file multiple directory
99. Tar –xzvf [Link]:- to extract the tar file
100. Tar –tzvf [Link]:- to see the contents tar file without extract
101. Zip –r [Link] directory1 :- to creates zip files
102. Unzip [Link] :- to unzip the files
103. Unzip –l [Link] (or) less [Link]:- to see the content
without extract to see the content
104. Gzip filename:- to create only the files not directories
Example: gzip filename
Output:[Link]
105. Gzip –r directory:- to create only zip In files in directories
106. Gunzip [Link] :- to extract the gz file
107. Tar –cjvf [Link] filename:- to create bz file
108. Jar –cvf [Link] file1 file2 :- to create a jar file
109. Jar –xvf [Link] :- extract the jar file
110. Jar –tvf [Link] :- to see the content of jar file without extract
111. Jar –cvf [Link] directorypath:- create a war file
112. Jar –xvf [Link]:- extract the war file
113. Jar –xvf [Link] –d directorypath:- extract the war file in
specific location
114. Jar –tvf [Link]:- to see the war file content without extract

Moving data one location to another


================================
115. Cp :- copy the files
116. Cp source destintion:- to copy the files one location to another.
117. Cp -r source destintion:- to copy the directories one location to
another.
118. mv source destintion:- to cut and paste the files one location to
another. Or rename the files /directories

Directory commands
========================
119. mkdir directoryname :- to create the directory.
120. Mkdir -p directoryname :- to create the directorys with in
directories.
121. Mkdir – p directory/directory{1..10} :- to create the directory in
directories
122. Rm –rf directoryname:- it removes the directory without
confirmation
123. Rmdir directoryname :- it remove the directory

Permission commands
==========================
124. Chmod 755 file :- to be give the change file permissions.
125. Chmod u+x file :- to be give the users execution permissions
126. chmod g+x file :- to give the groups execution permissions

Chmod o+x file :- to give the others execution permissions

127. chown username:username filename :- to change the ower


permissions
128. chgrp username:username filename :- to change the group
permissions
PS Command
=================
129. Ps :- to display the process for running
130. Ps –ef :to display the all process for running
131. Ps –ef|grep httpd :- to check the httpd process is running or not
132. ps –ef|grep httpd :-to see the apache running or not
133. ps –ef|grep httpd|grep –v grep :-to see the apache running or not ad
not display grep output
134. kill -9 <pid> (Or) kill –TERM <PID> :- it kill the running process
forcebully
135. kill -3 <pid>:- it create the thread dump
136. jstack -l <pid> :- Creating tread dump
137. jmap -dump:[live],format=b,file=<file-path> <pid>
User Creation commands
===========================
138. Useradd username
139. Id username
140. Passwd username:-to create the password
141. Userdel username:-delete the user
142. Useradd –g primarygrp username:-
143. Useradd –G secondarygrp username:-
144. Usermod –a –G groupname username:-

Disk Fragmentation commands


=============================
145. Df :- to display disk fragmentation
146. Df -h :-to display disk fragmentation in gb
147. Df –m :- to display disk fragmentation in mb
148. Df –h . :- to display the disk fragmentation in current directory
149. Du:- to display the disk usage information
150. Du –sm *|sort -rn :- usage of the path reverse order
151. Du –sm *|sort –rn |head :- to display top 10in reverse order in
top approach
152. Du –sm *|sort –rn |tail :- to display top 10in reverse order in
down approach
153. Scp source username@ipaddress/hostname :remoteserverPath :- to
copy data one server to another server
154. Scp -r usernameA@ipaddress/hostname:remoteserverPath
usernameB@ipaddress/hostname:remoteserverPath :- to copy data
second server to third server but your in first server

155. Ssh steps password less login


Step1:-ssh-keygen
Goto .ssh
Id_rsa.pub

Step2:-go to second server


Go to home directory
Create .ssh directory
Copy the first server id_rsa.pub from to second server .ssh directory
Step 3:- after copy the file rename id_rsa.pub to authorized_keys
Change the permissions in server 2
chmod 644 authorized_keys
step 4:- to verify the ssh user@ipaddress
156. Yum install packagename:- for install the package(with dependencies.
157. Yum remove package :- for uninstall the package
158. ln –s file1 file2 :- to create the symbolic link file
159. echo message :- it displays the message
160. echo message >[Link] :- to create a file and store the file echo
message information.
161. Set or env :- to check the system or user environment variable
162. Echo $HOME:- it display the current home directory
163. >[Link]:-
164. Wc filename:-
165. Grep “searchkeyword” filename:-
166. Grep –i “searchkeyword” filename:-
167. Echo $? :- if previous command is success print zero
Else :- print non zero
168. History :- to display the previous run command information

Common questions

Powered by AI

The command `tail -f filename` is used to continuously display new lines being added to the end of a log file, which is useful for real-time monitoring. This allows administrators to observe the newest log entries as they are written to the file, enabling immediate insights and troubleshooting .

`chown` is used to change the ownership of a file or directory, specifying a new owner or group, whereas `chmod` changes the permissions of a file or directory, specifying who can read, write, or execute it. Use `chown` when you need to transfer ownership, typically when files are moved between users but retain existing permissions. Conversely, use `chmod` to adjust access permissions without changing who owns the file .

To create a directory structure with subdirectories inside a main directory in a single command, you can use `mkdir -p main_directory/subdirectory1/subdirectory2`. The `-p` option tells `mkdir` to make parent directories as needed, creating `subdirectory1` and `subdirectory2` if they do not exist within `main_directory` .

To search for all running processes related to Apache, use `ps -ef | grep httpd`. This lists processes with 'httpd' in their command line, a common Apache server reference. To terminate these processes, use the command `kill -9 pid`, substituting `pid` with each process ID found from the previous command output .

To copy all files with the `.mp3` extension to a specific directory, you can use the command `find . -type f -name "*.mp3" -exec cp {} /tmp/MusicFiles \;`. Here, `find . -type f -name "*.mp3"` searches for files with the `.mp3` extension, and `-exec cp {} /tmp/MusicFiles \;` executes the `cp` command on each file found, copying it to the `/tmp/MusicFiles` directory .

To create a compressed archive of a directory using `tar`, use the command `tar -czvf archive.tar.gz directory`, where `-c` is to create an archive, `-z` compresses the archive with gzip, `-v` displays the progress, and `-f` specifies the filename. To extract this archive to a specific location, you would use `tar -xzvf archive.tar.gz -C /path/to/destination`, where `-x` extracts the files, `-z` decompresses the gzip archive, and `-C` specifies the destination directory .

To replace all occurrences of a word in a file using `sed`, you can use the command `sed 's/oldname/newname/g' filename`. This command substitutes every occurrence of 'oldname' with 'newname' throughout the file. In contrast, to replace only the first occurrence of 'oldname' in each line with 'newname', you would use `sed 's/oldname/newname/' filename` without the global replacement flag 'g' .

`nslookup` is preferred when a user needs a quick and simple lookup for an IP address or DNS information. It's less verbose and widely understood. In contrast, `dig` provides more detailed information and is better suited for comprehensive DNS diagnostics and troubleshooting. Opt for `nslookup` for simplicity and fast results, and `dig` when detailed DNS data analysis is needed .

`scp` copies files between servers securely over SSH but lacks advanced synchronization capabilities. `rsync`, also secure via SSH, offers incremental transfers, syncing only changed parts of files and reducing data transfer volume. Choose `scp` for simple, one-time copies and `rsync` for efficiency in frequently updated file systems or synchronizations involving large datasets .

To display the last 10 modified files in a directory, you can use the command `ls -lt | head`. The `-l` option provides a long listing format, `-t` sorts the files by modification time, with the newest files first, and `| head` shows only the first 10 entries in this sorted list, effectively displaying the last 10 modified files .

You might also like