编程知识 cdmana.com

Summary of Linux commands commonly used by operation and maintenance personnel

As operation and maintenance personnel , These common commands have to be , Master these commands , You'll get twice the result with half the effort at work , Provide efficiency .

 It is commonly used by operation and maintenance personnel Linux Command summary

One . Files and directories

  1. cd command , Used to switch the current directory , Its parameter is the path of the directory to switch to , It could be an absolute path , It could be a relative path .
cd /home     Get into  '/ home'  Catalog 
cd ..             Return to the previous directory  
cd ../..          Returns the two directories above  
cd                Enter personal home directory  
cd ~user1    Enter personal home directory  
cd -              Return to last directory 
  1. pwd command , Show work path
    [root@mailvip ~]# pwd
    /root
  2. ls command , Command to view files and directories ,list The meaning of
ls  View files in directory  
ls -l  Show file and catalog details  
ls -a  List all documents , Contains hidden files 
ls -R  List with contents of subdirectories ( Recursively list ), All files in the directory will be displayed   
ls [0-9]  Display file and directory names with numbers 
  1. cp command , For copying files ,copy The meaning of , It can also copy multiple files to one directory at a time
-a : Copy file properties together 
-p : Copy along with the properties of the file , Instead of using the default , And -a be similar , Often used for backup 
-i : If the target file already exists , When you overwrite, you will first ask for the operation 
-r : Recursive continuous replication , Replication behavior for directories  // Recursive replication is often used 
-u : Only when the target file is different from the source file will it be copied 
  1. mv command , For moving files 、 Catalog or rename ,move The meaning of
-f :force The meaning of coercion , If the target file already exists , Don't ask and cover directly 
-i : If the target file already exists , You will be asked if you want to overwrite 
-u : If the target file already exists , And newer than the target file , Will update 
  1. rm command , Used to delete files or directories ,remove The meaning of
-f : Namely force It means , Ignore nonexistent files , There will be no warning messages 
-i : Interactive mode , Before deleting, the user will be asked whether to operate 
-r : Recursive delete , Most commonly used for directory deletion , It's a very dangerous parameter 

Two 、 View file contents

  1. cat command , Used to view the contents of a text file , Followed by the name of the file you want to view , Usually, pipes and more and less Use it together
cat file1  View the contents of the file forward from the first byte  
tac file1  Reverse viewing the contents of a file from the last line  
cat -n file1  Number of lines indicating the file  
more file1  View the contents of a long file  

head -n 2 file1  View the first two lines of a file  
tail -n 2 file1  View the last two lines of a file  
tail -n +1000 file1   from 1000 Line start display , Show 1000 OK, later 
cat filename | head -n 3000 | tail -n +1000   Show 1000 Row to 3000 That's ok 
cat filename | tail -n +3000 | head -n 1000   From 3000 OK, let's start , Show 1000( It shows 3000~3999 That's ok )

3、 ... and . File search

  1. find command , Used to find the system
    find / -name file1  from  '/'  Start root file system search for files and directories  
    find / -user user1  Search belongs to user  'user1'  Files and directories for  
    find /usr/bin -type f -atime +100  Search in the past 100 Execution files not used in days  
    find /usr/bin -type f -mtime -10  Search in 10 Files created or modified in days  
    whereis halt  Show a binary 、 Source code or man The location of  
    which halt  Displays the full path of a binary or executable 

Delete greater than 50M The file of :

find /var/mail/ -size +50M -exec rm {} \;

Four . File permissions - Use "+" Set the permissions , Use "-" For cancellation

  1. chmod command , Change file / Folder permissions

    ls -lh  Display permission  
    chmod ugo+rwx directory1  Set up the owner of the directory (u)、 group (g) And others (o) Reading (r,4 )、 Write (w,2) And execution (x,1) Authority  
    chmod go-rwx directory1   Delete Group (g) And others (o) Read and write permission to the directory 
  2. chown command , Change the owner of the document
    chown user1 file1  Change the owner property of a file  
    chown -R user1 directory1  Change the owner property of a directory and change the property of all files in the directory at the same time  
    chown user1:group1 file1  Change the owner and group properties of a file 

11.chgrp command , Change the user group to which the file belongs

chgrp group1 file1  Change the group of files 

5、 ... and . Text processing

  1. grep command , Analyze a line of information , If there is any information we need , Show that line , This command is usually used with the pipeline command , Used to filter the output of some commands and so on
grep Aug /var/log/messages   In the file  '/var/log/messages' Search for keywords in "Aug" 

grep ^Aug /var/log/messages  In the file  '/var/log/messages' Find in "Aug" Words at the beginning  
grep [0-9]  /var/log/messages  choice  '/var/log/messages'  All lines in the file that contain numbers  

grep Aug -R /var/log/*  In the catalog  '/var/log'  And subsequent search strings in the directory "Aug" 

sed 's/stringa1/stringa2/g' example.txt  take example.txt In the document  "string1"  Replace with  "string2" 

sed '/^$/d' example.txt  from example.txt Delete all blank lines in file 
  1. paste command
paste file1 file2  Merge the contents of two files or two columns  
paste -d '+' file1 file2  Merge the contents of two files or two columns , Intermediate use "+" distinguish 
  1. sort command
sort file1 file2  Sort the contents of two files  
sort file1 file2 | uniq  Take out the union of two files ( Keep only one duplicate line ) 
sort file1 file2 | uniq -u  Delete intersection , Leave other lines  
sort file1 file2 | uniq -d  Extract the intersection of two files ( Leave only files that exist in both files )
  1. comm command
comm -1 file1 file2  Comparing the contents of two files only delete  'file1'  Content contained  
comm -2 file1 file2  Comparing the contents of two files only delete  'file2'  Content contained  
comm -3 file1 file2  Comparing the contents of two files only removes the common parts of two files 

6、 ... and 、 Packaging and compressing files

  1. tar command , Package the files , The default is not to compress , If the corresponding parameters are specified , It also calls the corresponding compressor ( Such as gzip and bzip etc. ) Compress and decompress
-c : New package file 
-t : See what file names are included in the contents of the package file 
-x : Unpacking or decompressing functions , Collocation -C( Capitalization ) Specify the directory to extract , Be careful -c,-t,-x Can't be in the same command at the same time 
-j : adopt bzip2 Support for compression / decompression 
-z : adopt gzip Support for compression / decompression 
-v : In compression / During decompression , Display the file name being processed 
-f filename :filename For the documents to be processed 
-C dir : Specify compression / Decompressed Directory dir

Compress :tar -jcv -f filename.tar.bz2 The name of the file or directory to be processed
Inquire about :tar -jtv -f filename.tar.bz2
decompression :tar -jxv -f filename.tar.bz2 -C The directory you want to unzip

bunzip2 file1.bz2  Unzip one called  'file1.bz2' The file of  
bzip2 file1  Compress one called  'file1'  The file of  
gunzip file1.gz  Unzip one called  'file1.gz' The file of  
gzip file1  Compress one called  'file1' The file of  
gzip -9 file1  Maximum compression  
rar a file1.rar test_file  Create a  'file1.rar'  My bag  
rar a file1.rar file1 file2 dir1  Simultaneous compression  'file1', 'file2'  And directory  'dir1' 
rar x file1.rar  decompression rar package 

zip file1.zip file1  Create a zip Compressed package of format  
unzip file1.zip  Unpack one zip Format pack  
zip -r file1.zip file1 file2 dir1  Compress several files and directories into one zip Compressed package of format 

7、 ... and . System and shutdown ( To turn it off 、 Reboot and logout )

shutdown -h now  Shutdown system (1) 
init 0  Shutdown system (2) 
telinit 0  Shutdown system (3) 
shutdown -h hours:minutes &  Shut down the system at the scheduled time  
shutdown -c  Cancel system shutdown at scheduled time  
shutdown -r now  restart (1) 
reboot  restart (2) 
logout  Cancellation  
time  Measure an order ( I.e. procedure ) Execution time of  

8、 ... and 、 Process related commands

  1. jps command , Displays the... Of the current system java Process situation , And its id Number

jps(Java Virtual Machine Process Status Tool) yes JDK 1.5 The one provided shows all the current java process pid The order of , Simple and practical , It's very suitable for linux/unix Look at the current situation on the platform java Some simple cases of the process .

  1. ps command , It is used to select the process running status of a certain time point and output ,process The meaning of
-A : All the processes are shown 
-a : Not with terminal All the processes involved 
-u : Processes related to effective users 
-x : Generally speaking, it is related to a Parameters used together , More complete information can be listed 
-l : longer  , In more detail PID The information listed in 

ps aux #  View all process data of the system 
ps ax #  Check not with terminal All the processes involved 
ps -lA #  View all process data of the system 
ps axjf #  View the status along with a part of the process tree 
  1. kill command , Used to work towards (%jobnumber) Or something PID( Numbers ) Send a signal , It is usually with ps and jobs Command is used together

Command format : kill[ Command parameter ][ process id]

Command parameter :

-l   The signal , If no signal number parameter is added , Then use “-l” The parameter lists all the signal names 
-a   When processing the current process , There is no limit to the correspondence between the command name and the process number 
-p   Appoint kill  The command only prints the process number of the relevant process , Without sending any signals 
-s   Specify to send a signal 
-u   Designated 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

explain :

Only the first one 9 Signals (SIGKILL) To terminate the process unconditionally , Other signaling processes have the right to ignore . Here are some common signals :

HUP    1     Terminal disconnection 
INT     2     interrupt ( Same as  Ctrl + C)
QUIT    3     sign out ( Same as  Ctrl + \)
TERM   15     End 
KILL    9     Mandatory termination 
CONT   18     continue ( And STOP contrary , fg/bg command )
STOP    19     Pause ( Same as  Ctrl + Z)

example 2: Get the value of the 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
[root@localhost test6]#

example 3: First use ps Find the process , And then use kill kill

 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: Kill the process completely

 command :kill –9 3268   // -9  Force kill process 
  1. killall command , Send a signal to a process started by a command , Used to kill a process with the specified name

Command format : killall[ Command parameter ][ Process name ]

 Command parameter :
-Z  Just kill the owner scontext  The process of 
-e  Matching process name is required 
-I  Ignore lowercase 
-g  Kill process groups instead of processes 
-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  Whether the report signal is sent successfully 
-w  Waiting for the process to die 
--help  Display help information 
--version  Show version show 

Example

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

2. Send the specified signal to the process 
    killall -TERM ngixn   perhaps   killall -KILL nginx
  1. top command , yes Linux Common performance analysis tools , It can display the resource usage of each process in the system in real time , Be similar to Windows Task manager .

How to kill the process :

(1) The way of graphical interface 
(2)kill -9 pid  (-9 Indicates forced close )
(3)killall -9  Name of program 
(4)pkill  Name of program 

Check the process port number :

netstat -tunlp|grep  Port number 

From the original :https://www.jianshu.com/p/7c0df6fcfc71

版权声明
本文为[osc_s7fsyuo1]所创,转载请带上原文链接,感谢

Scroll to Top