Summary of Commonly Used Linux Commands for Operations and Maintenance Personnel

Click on "Programmer Interview", select "Star🔝"

As operations and maintenance personnel, these commonly used commands are essential. Mastering these commands will significantly improve work efficiency.

1. File and Directory
The cd command is used to switch the current directory. Its parameter is the path to the directory to switch to, which can be an absolute or relative path.
cd /home    Enter the '/home' directory
cd ..            Return to the previous directory
cd ../..         Return to two levels up
cd               Enter the user's home directory
cd ~user1   Enter the home directory of user1
cd -             Return to the last directory

The pwd command displays the current working path.
[root@mailvip ~]# pwd
/root

The ls command is used to view files and directories, meaning "list".
ls View files in the directory
ls -l Display detailed information about files and directories
ls -a List all files, including hidden files
ls -R List contents including subdirectories (recursive listing), meaning all files in the directory will be displayed
ls [0-9] Display filenames and directory names that contain numbers

The cp command is used to copy files, meaning "copy". It can also copy multiple files to a directory at once.
-a: Copy file attributes along with the files
-p: Copy files along with their attributes, rather than using the default method, similar to -a, often used for backups
-i: If the target file already exists, it will ask for confirmation before overwriting
-r: Recursively copy, used for copying directories // often used for recursive copying
-u: Only copy if the target file differs from the source file

The mv command is used to move files, directories, or rename them, meaning "move".
-f: Force, meaning if the target file already exists, it will overwrite without asking
-i: If the target file already exists, it will ask whether to overwrite
-u: Only update if the target file exists and is newer than the target file

The rm command is used to delete files or directories, meaning "remove".
-f: Force, meaning ignore nonexistent files and do not show warning messages
-i: Interactive mode, will ask the user for confirmation before deleting
-r: Recursive delete, most commonly used for deleting directories, it is a very dangerous parameter

2. Viewing File Content
The cat command is used to view the contents of text files, followed by the filename to be viewed, and can usually be used with pipes and more or less.
cat file1 View the content of the file from the first byte

The tac command views a file's content in reverse from the last line.
cat -n file1 Number the lines of the file
more file1 View the content of a long file

head -n 2 file1 View the first two lines of a file

The tail command views the last lines of a file.
tail -n 2 file1 View the last two lines of a file
tail -n +1000 file1 Start displaying from line 1000, showing lines after that
cat filename | head -n 3000 | tail -n +1000  Display lines from 1000 to 3000
cat filename | tail -n +3000 | head -n 1000  Start from line 3000, display 1000 lines (i.e., display lines 3000 to 3999)

3. File Search
The find command is used to search the system.
find / -name file1 Start searching for files and directories from the root file system '/'
find / -user user1 Search for files and directories belonging to user 'user1'
find /usr/bin -type f -atime +100 Search for executable files that have not been used in the past 100 days
find /usr/bin -type f -mtime -10 Search for files created or modified in the last 10 days
whereis halt Show the location of a binary file, source code, or man page
which halt Show the full path of a binary file or executable

Delete files larger than 50M:
find /var/mail/ -size +50M -exec rm {} \;

4. File Permissions - Use "+" to set permissions, use "-" to revoke permissions
The chmod command changes file/folder permissions.
ls -lh Display permissions
chmod ugo+rwx directory1 Set read (r, 4), write (w, 2), and execute (x, 1) permissions for the owner (u), group (g), and others (o)
chmod go-rwx directory1 Remove read, write, and execute permissions for group (g) and others (o) on the directory

The chown command changes the file owner.
chown user1 file1 Change the owner attribute of a file
chown -R user1 directory1 Change the owner attribute of a directory and all files within it
chown user1:group1 file1 Change the owner and group attributes of a file

The chgrp command changes the file's group.
chgrp group1 file1 Change the group of the file

5. Text Processing
The grep command analyzes a line of information. If it contains the information we need, it will display that line. This command is often used with pipe commands to filter and process the output of some commands, etc.
grep Aug /var/log/messages  Search for the keyword "Aug" in the file '/var/log/messages'

grep ^Aug /var/log/messages Search for words starting with "Aug" in the file '/var/log/messages'
grep [0-9]  /var/log/messages Select all lines in '/var/log/messages' that contain numbers

grep Aug -R /var/log/* Search for the string "Aug" in the directory '/var/log' and subsequent directories

The sed command is used to replace strings in files.
sed 's/stringa1/stringa2/g' example.txt Replace "string1" with "string2" in example.txt

sed '/^$/d' example.txt Delete all blank lines from example.txt

The paste command merges two files or columns of content.
paste file1 file2 Merge two files or columns of content
paste -d '+' file1 file2 Merge two files or columns of content, separated by "+"

The sort command sorts the contents of two files.
sort file1 file2 Sort the contents of two files
sort file1 file2 | uniq Get the union of two files (only keep one copy of duplicate lines)
sort file1 file2 | uniq -u Delete the intersection, leaving other lines
sort file1 file2 | uniq -d Get the intersection of two files (only keep files that exist in both files)

The comm command compares the contents of two files.
comm -1 file1 file2 Compare the contents of two files, only deleting the contents contained in 'file1'
comm -2 file1 file2 Compare the contents of two files, only deleting the contents contained in 'file2'
comm -3 file1 file2 Compare the contents of two files, only deleting the common parts of both files

6. Packaging and Compressing Files
The tar command is used to package files. By default, it does not compress. If the corresponding parameters are specified, it will also call the corresponding compression program (such as gzip and bzip2) for compression and decompression.
-c: Create a new package file
-t: View the contents of the package file
-x: Unpack or decompress, can be combined with -C (uppercase) to specify the directory to decompress to, note that -c, -t, -x cannot appear in the same command
-j: Compress/decompress with bzip2 support
-z: Compress/decompress with gzip support
-v: Display the names of files being processed during compression/decompression
-f filename: filename is the file to be processed
-C dir: Specify the directory dir for compression/decompression

Compress: tar -jcv -f filename.tar.bz2 Name of the file or directory to be processed
Query: tar -jtv -f filename.tar.bz2 Unpack: tar -jxv -f filename.tar.bz2 -C Directory to decompress to
bunzip2 file1.bz2 Unpack a file called 'file1.bz2'
bzip2 file1 Compress a file called 'file1'
gunzip file1.gz Unpack a file called 'file1.gz'
gzip file1 Compress a file called 'file1'
gzip -9 file1 Maximum compression
rar a file1.rar test_file Create a package called 'file1.rar'
rar a file1.rar file1 file2 dir1 Compress 'file1', 'file2', and directory 'dir1' at the same time
rar x file1.rar Unpack rar package

zip file1.zip file1 Create a zip format compressed package
unzip file1.zip Unpack a zip format compressed package
zip -r file1.zip file1 file2 dir1 Compress several files and directories into a zip format compressed package

7. System and Shutdown (Shutdown, Restart, and Logout)
shutdown -h now Shut down the system (1)
init 0 Shut down the system (2)
telinit 0 Shut down the system (3)
shutdown -h hours:minutes & Shut down the system at a scheduled time
shutdown -c Cancel the scheduled shutdown
shutdown -r now Restart (1)
reboot Restart (2)
logout Logout
time Measure the execution time of a command (i.e., program)

8. Process Related Commands
The jps command displays the current Java process status and its ID number.
The jps (Java Virtual Machine Process Status Tool) command provided by JDK 1.5 displays the current Java process PIDs, which is simple and practical, very suitable for quickly checking the current Java process status on Linux/Unix platforms.

The ps command is used to capture and output the running status of processes at a certain point in time, meaning "process".
-A: Display all processes
-a: Display all processes not associated with the terminal
-u: Related processes of the effective user
-x: Generally used with the a parameter, can list more complete information
-l: List PID information in a longer, more detailed format

ps aux # View all process data in the system
ps ax # View all processes not associated with the terminal
ps -lA # View all process data in the system
ps axjf # View part of the process tree status

The kill command is used to send a signal to a job (%jobnumber) or a PID (number). It is usually used with the ps and jobs commands.
Command format: kill [command parameters] [process id]
Command parameters:
-l  Signal, if no signal number parameter is added, using the "-l" parameter will list all signal names
-a  When processing the current process, do not limit the correspondence between command names and process numbers
-p  Specify that the kill command only prints the process number of the related process without sending any signal
-s  Specify the signal to send
-u  Specify the user

Example 1: List all signal names Command: kill -l Output:
[root@localhost test6]# kill -l
 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL
 5) SIGTRAP      6) SIGABRT      7) SIGBUS       8) SIGFPE
 9) SIGKILL     10) SIGUSR1     11) SIGSEGV     12) SIGUSR2
13) SIGPIPE     14) SIGALRM     15) SIGTERM     16) SIGSTKFLT
17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP
21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU
25) SIGXFSZ     26) SIGVTALRM   27) SIGPROF     28) SIGWINCH
29) SIGIO       30) SIGPWR      31) SIGSYS      34) SIGRTMIN
35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3  38) SIGRTMIN+4
39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12
47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14
51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10
55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7  58) SIGRTMAX-6
59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX

Note: Only the 9th signal (SIGKILL) can unconditionally terminate a process; other signals can be ignored by the process. Below are common signals:
HUP    1    Terminal disconnection
INT     2    Interrupt (same as Ctrl + C)
QUIT    3    Exit (same as Ctrl + \)
TERM   15    Terminate
KILL    9    Force terminate
CONT   18    Continue (opposite of STOP, fg/bg commands)
STOP    19    Pause (same as Ctrl + Z)

Example 2: Get the value of a specified signal
[root@localhost test6]# kill -l KILL
[root@localhost test6]# kill -l SIGKILL
[root@localhost test6]# kill -l TERM
[root@localhost test6]# kill -l SIGTERM

Example 3: First use ps to find the process, then use kill to terminate it.
Command: kill 3268
[root@localhost test6]# ps -ef|grep vim 
root      3268  2884  0 16:21 pts/1    00:00:00 vim install.log
root      3370  2822  0 16:21 pts/0    00:00:00 grep vim
[root@localhost test6]# kill 3268

Example 4: Completely kill the process
Command: kill –9 3268   // -9 forcefully kill the process

The killall command sends a signal to a process started by a command, used to kill processes with a specified name.
Command format: killall [command parameters] [process name]
Command parameters:
-Z Only kill processes with scontext
-e Require matching process names
-I Ignore case
-g Kill the process group instead of the process
-i Interactive mode, ask the user before killing the process
-l List all known signal names
-q Do not output warning messages
-s Send the specified signal
-v Report whether the signal was successfully sent
-w Wait for the process to die
--help Display help information
--version Display version information

Example 1: Kill all processes with the same name
    killall nginx
    killall -9 bash

Example 2: Send a specified signal to the process
    killall -TERM nginx  or  killall -KILL nginx

The top command is a commonly used performance analysis tool in Linux, which can display the resource usage status of various processes in real-time, similar to the task manager in Windows.

How to kill a process:
(1) Graphical interface method
(2) kill -9 pid  (-9 means force close)
(3) killall -9 program name
(4) pkill program name

View process port number:
netstat -tunlp|grep port number
Source: https://www.jianshu.com/p/7c0df6fcfc71

End of article benefits

Share 570 commonly used Linux commands, which can be searched and quickly referenced with one click, available in PDF format for easy access and memory. In addition to functionality, it also includes detailed explanations, making it very worthwhile to collect.

At the same time, other corresponding Linux command documents, including common command interpretations, Linux command quick reference, etc., are also shared, which everyone can refer to and collect.

570 Complete Linux Command Collection!

Complete Linux Command Collection!

……
The method to receive is here!

Long press or scan the QR code
And note the keyword: Linux Command Collection
To get it for free directly

Leave a Comment