Summary of commonly used Linux commands

A good memory is not as good as a bad pen. If you can jot down something, just jot it down. If you have time to take it out, you will find a different feeling.

table of Contents

1. Files and directories

Second, view the contents of the file

3. File search

4. File permissions-use "+" to set permissions, use "-" to cancel

V. Text processing

Six, package and compress files

7. System and shutdown (shutdown, restart and logout)

8. Process-related commands


1. Files and directories

  1. The cd command is used to switch the current directory. Its parameter is the path of the directory to be switched to, which can be an absolute path or a relative path.

cd /home    进入 '/ home' 目录
cd ..            返回上一级目录 
cd ../..         返回上两级目录 
cd               进入个人的主目录 
cd ~user1   进入个人的主目录 
cd -             返回上次所在的目录
  1. pwd command, display the working path

[root@mailvip ~]# pwd
/root
  1. ls command, command to view files and directories, meaning of list

ls 查看目录中的文件 
ls -l 显示文件和目录的详细资料 
ls -a 列出全部文件,包含隐藏文件
ls -R 连同子目录的内容一起列出(递归列出),等于该目录下的所有文件都会显示出来  
ls [0-9] 显示包含数字的文件名和目录名
  1. The cp command is used to copy files. It means copy. It can also copy multiple files to a directory at once

-a :将文件的特性一起复制
-p :连同文件的属性一起复制,而非使用默认方式,与-a相似,常用于备份
-i :若目标文件已经存在时,在覆盖时会先询问操作的进行
-r :递归持续复制,用于目录的复制行为 //经常使用递归复制
-u :目标文件与源文件有差异时才会复制
  1. mv command, used to move files, directories or renames, meaning move

-f :force强制的意思,如果目标文件已经存在,不会询问而直接覆盖
-i :若目标文件已经存在,就会询问是否覆盖
-u :若目标文件已经存在,且比目标文件新,才会更新
  1. The rm command is used to delete files or directories, meaning remove

-f :就是force的意思,忽略不存在的文件,不会出现警告消息
-i :互动模式,在删除前会询问用户是否操作
-r :递归删除,最常用于目录删除,它是一个非常危险的参数

Second, view the contents of the file

  1. The cat command is used to view the content of a text file, followed by the name of the file to be viewed, usually a pipe can be used with more and less

cat file1 从第一个字节开始正向查看文件的内容 
tac file1 从最后一行开始反向查看一个文件的内容 
cat -n file1 标示文件的行数 
more file1 查看一个长文件的内容 

head -n 2 file1 查看一个文件的前两行 
tail -n 2 file1 查看一个文件的最后两行 
tail -n +1000 file1  从1000行开始显示,显示1000行以后的
cat filename | head -n 3000 | tail -n +1000  显示1000行到3000行
cat filename | tail -n +3000 | head -n 1000  从第3000行开始,显示1000(即显示3000~3999行)

3. File search

  1. find command, used to find the system

find / -name file1 从 '/' 开始进入根文件系统搜索文件和目录 
find / -user user1 搜索属于用户 'user1' 的文件和目录 
find /usr/bin -type f -atime +100 搜索在过去100天内未被使用过的执行文件 
find /usr/bin -type f -mtime -10 搜索在10天内被创建或者修改过的文件 
whereis halt 显示一个二进制文件、源码或man的位置 
which halt 显示一个二进制文件或可执行文件的完整路径

Delete files larger than 50M:

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

4. File permissions-use "+" to set permissions, use "-" to cancel

  1. chmod command to change file/folder permissions

ls -lh 显示权限 
chmod ugo+rwx directory1 设置目录的所有人(u)、群组(g)以及其他人(o)以读(r,4 )、写(w,2)和执行(x,1)的权限 
chmod go-rwx directory1  删除群组(g)与其他人(o)对目录的读写执行权限
  1. chown command to change the owner of the file

chown user1 file1 改变一个文件的所有人属性 
chown -R user1 directory1 改变一个目录的所有人属性并同时改变改该目录下所有文件的属性 
chown user1:group1 file1 改变一个文件的所有人和群组属性

11.chgrp command to change the user group to which the file belongs

chgrp group1 file1 改变文件的群组

V. Text processing

  1. The grep command analyzes the information of a line and displays the line if there is the information we need. This command is usually used together with the pipeline command to filter and process the output of some commands, etc.

grep Aug /var/log/messages  在文件 '/var/log/messages'中查找关键词"Aug" 

grep ^Aug /var/log/messages 在文件 '/var/log/messages'中查找以"Aug"开始的词汇 
grep [0-9]  /var/log/messages 选择 '/var/log/messages' 文件中所有包含数字的行 

grep Aug -R /var/log/* 在目录 '/var/log' 及随后的目录中搜索字符串"Aug" 

sed 's/stringa1/stringa2/g' example.txt 将example.txt文件中的 "string1" 替换成 "string2" 

sed '/^$/d' example.txt 从example.txt文件中删除所有空白行
  1. paste command

paste file1 file2 合并两个文件或两栏的内容 
paste -d '+' file1 file2 合并两个文件或两栏的内容,中间用"+"区分
  1. sort command

sort file1 file2 排序两个文件的内容 
sort file1 file2 | uniq 取出两个文件的并集(重复的行只保留一份) 
sort file1 file2 | uniq -u 删除交集,留下其他的行 
sort file1 file2 | uniq -d 取出两个文件的交集(只留下同时存在于两个文件中的文件)
  1. comm command

comm -1 file1 file2 比较两个文件的内容只删除 'file1' 所包含的内容 
comm -2 file1 file2 比较两个文件的内容只删除 'file2' 所包含的内容 
comm -3 file1 file2 比较两个文件的内容只删除两个文件共有的部分

Six, package and compress files

  1. The tar command packs files. By default, they will not be compressed. If the corresponding parameters are specified, it will also call the corresponding compression program (such as gzip and bzip, etc.) to compress and decompress

-c :新建打包文件
-t :查看打包文件的内容含有哪些文件名
-x :解打包或解压缩的功能,可以搭配-C(大写)指定解压的目录,注意-c,-t,-x不能同时出现在同一条命令中
-j :通过bzip2的支持进行压缩/解压缩
-z :通过gzip的支持进行压缩/解压缩
-v :在压缩/解压缩过程中,将正在处理的文件名显示出来
-f filename :filename为要处理的文件
-C dir :指定压缩/解压缩的目录dir

Compression: tar -jcv -f filename.tar.bz2 To be processed file or directory name query: tar -jtv -f filename.tar.bz2 Decompression: tar -jxv -f filename.tar.bz2 -C To be decompressed table of Contents

bunzip2 file1.bz2 解压一个叫做 'file1.bz2'的文件 
bzip2 file1 压缩一个叫做 'file1' 的文件 
gunzip file1.gz 解压一个叫做 'file1.gz'的文件 
gzip file1 压缩一个叫做 'file1'的文件 
gzip -9 file1 最大程度压缩 
rar a file1.rar test_file 创建一个叫做 'file1.rar' 的包 
rar a file1.rar file1 file2 dir1 同时压缩 'file1', 'file2' 以及目录 'dir1' 
rar x file1.rar 解压rar包

zip file1.zip file1 创建一个zip格式的压缩包 
unzip file1.zip 解压一个zip格式压缩包 
zip -r file1.zip file1 file2 dir1 将几个文件和目录同时压缩成一个zip格式的压缩包

7. System and shutdown (shutdown, restart and logout)

shutdown -h now 关闭系统(1) 
init 0 关闭系统(2) 
telinit 0 关闭系统(3) 
shutdown -h hours:minutes & 按预定时间关闭系统 
shutdown -c 取消按预定时间关闭系统 
shutdown -r now 重启(1) 
reboot 重启(2) 
logout 注销 
time 测算一个命令(即程序)的执行时间 

8. Process-related commands

  1. jps command, display the current system java process, and its id number

jps (Java Virtual Machine Process Status Tool) is a command provided by JDK 1.5 to display the pids of all current java processes. It is simple and practical. It is very suitable for simply viewing some simple conditions of current java processes on the linux/unix platform.

  1. The ps command is used to select and output the running status of the process at a certain point in time, meaning process

-A :所有的进程均显示出来
-a :不与terminal有关的所有进程
-u :有效用户的相关进程
-x :一般与a参数一起使用,可列出较完整的信息
-l :较长,较详细地将PID的信息列出

ps aux # 查看系统所有的进程数据
ps ax # 查看不与terminal有关的所有进程
ps -lA # 查看系统所有的进程数据
ps axjf # 查看连同一部分进程树状态
  1. 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 parameter][process id]

Command parameters:

-l  信号,若果不加信号的编号参数,则使用“-l”参数会列出全部的信号名称
-a  当处理当前进程时,不限制命令名和进程号的对应关系
-p  指定kill 命令只打印相关进程的进程号,而不发送任何信号
-s  指定发送信号
-u  指定用户

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

Description:

Only the 9th signal (SIGKILL) can terminate the process unconditionally, and other signal processes have the right to ignore. The following are commonly used signals:

HUP    1    终端断线
INT     2    中断(同 Ctrl + C)
QUIT    3    退出(同 Ctrl + \)
TERM   15    终止
KILL    9    强制终止
CONT   18    继续(与STOP相反, fg/bg命令)
STOP    19    暂停(同 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 to find the process, and then kill it with kill

命令: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

命令:kill –9 3268   // -9 强制杀掉进程
  1. The killall command sends a signal to the process started by a command to kill the process with the specified name

Command format: killall[command parameter][process name]

命令参数:
-Z 只杀死拥有scontext 的进程
-e 要求匹配进程名称
-I 忽略小写
-g 杀死进程组而不是进程
-i 交互模式,杀死进程前先询问用户
-l 列出所有的已知信号名称
-q 不输出警告信息
-s 发送指定的信号
-v 报告信号是否成功发送
-w 等待进程死亡
--help 显示帮助信息
--version 显示版本显示

Example

1:杀死所有同名进程
    killall nginx
    killall -9 bash

2.向进程发送指定信号
    killall -TERM ngixn  或者  killall -KILL nginx
  1. The top command is a commonly used performance analysis tool under Linux, which can display the resource occupancy status of each process in the system in real time, similar to the task manager of Windows.

How to kill the process:

(1)图形化界面的方式
(2)kill -9 pid  (-9表示强制关闭)
(3)killall -9 程序的名字
(4)pkill 程序的名字

View the process port number:

netstat -tunlp|grep 端口号

Guess you like

Origin blog.csdn.net/supingemail/article/details/112378104