Summary of commonly used commands in Linux

  • ls [options] [directory name | list all directories and files under the relevant directory
-a  列出包括.a开头的隐藏文件的所有文件
-A  通-a,但不列出"."".."
-l  列出文件的详细信息
-c  根据ctime排序显示
-t  根据文件修改时间排序
---color[=WHEN] 用色彩辨别文件类型 WHEN 可以是'never''always''auto'其中之一
   白色:表示普通文件
   蓝色:表示目录
   绿色:表示可执行文件
   红色:表示压缩文件
   浅蓝色:链接文件
   红色闪烁:表示链接的文件有问题
  • mv [options] source file or directory directory or multiple source files | move or rename files
-b  覆盖前做备份
-f  如存在不询问而强制覆盖
-i  如存在则询问是否覆盖
-u  较新才覆盖
-t  将多个源文件移动到统一目录下,目录参数在前,文件参数在后
eg:
   mv a /tmp/ 将文件a移动到 /tmp目录下
   mv a b 将a命名为b
   mv /home/zenghao test1.txt test2.txt test3.txt
  • cp [options] source file or directory directory or multiple source files | copy source file to destination file, or copy multiple source files to destination directory.
-r -R 递归复制该目录及其子目录内容
-p  连同档案属性一起复制过去
-f  不询问而强制复制
-s  生成快捷方式
-a  将档案的所有特性都一起复制
  • scp [parameters] [original path] [destination path] | Copy files and directories between Linux servers
-v  详细显示输出的具体情况
-r  递归复制整个目录
(1) 复制文件:  
命令格式:  
scp local_file remote_username@remote_ip:remote_folder  
或者  
scp local_file remote_username@remote_ip:remote_file  
或者  
scp local_file remote_ip:remote_folder  
或者  
scp local_file remote_ip:remote_file  1,2个指定了用户名,命令执行后需要输入用户密码,第1个仅指定了远程的目录,文件名字不变,第2个指定了文件名  3,4个没有指定用户名,命令执行后需要输入用户名和密码,第3个仅指定了远程的目录,文件名字不变,第4个指定了文件名   
(2) 复制目录:  
命令格式:  
scp -r local_folder remote_username@remote_ip:remote_folder  
或者  
scp -r local_folder remote_ip:remote_folder  1个指定了用户名,命令执行后需要输入用户密码;  2个没有指定用户名,命令执行后需要输入用户名和密码;
eg:
   从 本地 复制到 远程
   scp /home/daisy/test.txt
   root@10.55.130.213:/home/root 
   从 远程 复制到 本地
   scp root@10.55.130.213:/home/root/test.lua /home/daisy/test.lua
  • rm [options] file | delete file
-r  删除文件夹
-f  删除不提示
-i  删除提示
-v  详细显示进行步骤
  • touch [options] file | create empty file or update file time
-a  只修改存取时间
-m  值修改变动时间
-r  eg:touch -r a b ,使b的时间和a相同
-t  指定特定的时间 eg:touch -t 201804261053.40 log.log 
   -t time [[CC]YY]MMDDhhmm[.SS],C:年前两位
  • pwd View the current path
[root@10.25.130.177 lua]# pwd
/opt/soft/nginx/conf/lua
  • cd change the current directory
cd - :返回上层目录
cd .. :返回上层目录
cd 回车  :返回主目录
cd /   :根目录
  • mkdir [options] directory... | create new directory
-p  递归创建目录,若父目录不存在则依次创建
-m  自定义创建目录的权限  eg:mkdir -m 777 pain
-v  显示创建目录的详细信息
  • rmdir removes empty directories
-v  显示执行过程
-p  若自父母删除后父目录为空则一并删除
  • echo: display content
-n  输出后不换行
-e  遇到转义字符特殊处理  
   eg:
       echo "enh\enh"   显示eng\enh
       ehco -e "he\nhe"    显示he(换行了)he
  • . cat [options] [files]..| Show entire file at once or create one from keyboard or combine several files into one
-n  编号文件内容再输出
-E  在结束行提示$

tac | Show Reverse

  • less | Can move back and forth to view the content of the article screen by screen, without loading the entire file before viewing
-m  显示类似于more命令的百分比
-N  显示行号
/   字符串:向下搜索“字符串”的功能
?   字符串:向上搜索“字符串”的功能
n   重复前一个搜索(与 / 或 ? 有关)
N   反向重复前一个搜索(与 / 或 ? 有关)
b   向后翻一页
d   向后翻半页
  • nl [options]… [files]… | Automatically add line numbers to the output
 -b 
-b a 不论是否有空行,都列出行号(类似 cat -n) 
-b t 空行则不列行号(默认) 
-n 有ln rn rz三个参数,分别为再最左方显示,最右方显示不加0,最右方显示加0
  • head [parameter]… [file]… | Display the beginning of the file, the default is the first 10 lines
-v  显示文件名
-c number   显示前number个字符,若number为负数,则显示除最后number个字符的所有内容
-number/n (+)number     显示前number行内容,
-n numbernumber为负数,则显示除最后number行数据的所有内容
  • tail [required parameter] [optional parameter] [file] | show the content of the end of the file
-v  显示详细的处理信息
-q  不显示处理信息
-num/-n (-)num      显示最后num行内容
-n +num 从第num行开始显示后面的数据
-c  显示最后c个字符
-f  循环读取
  • vi edit file to create a file
:w filename 将文章以指定的文件名保存起来  
:wq 保存并退出
:q! 不保存而强制退出
命令行模式功能键
1)插入模式
   按「i」切换进入插入模式「insert mode」,按"i"进入插入模式后是从光标当前位置开始输入文件;
   按「a」进入插入模式后,是从目前光标所在位置的下一个位置开始输入文字;
   按「o」进入插入模式后,是插入新的一行,从行首开始输入文字。

2)从插入模式切换为命令行模式
 按「ESC」键。
3)移动光标
  vi可以直接用键盘上的光标来上下左右移动,但正规的vi是用小写英文字母「h」、「j」、「k」、「l」,分别控制光标左、下、上、右移一格。
  按「ctrl」+「b」:屏幕往"后"移动一页。
  按「ctrl」+「f」:屏幕往"前"移动一页。
  按「ctrl」+「u」:屏幕往"后"移动半页。
  按「ctrl」+「d」:屏幕往"前"移动半页。
  按数字「0」:移到文章的开头。
  按「G」:移动到文章的最后。
  按「$」:移动到光标所在行的"行尾"
  按「^」:移动到光标所在行的"行首"
  按「w」:光标跳到下个字的开头
  按「e」:光标跳到下个字的字尾
  按「b」:光标回到上个字的开头
  按「#l」:光标移到该行的第#个位置,如:5l,56l。

4)删除文字
  「x」:每按一次,删除光标所在位置的"后面"一个字符。
  「#x」:例如,「6x」表示删除光标所在位置的"后面"6个字符。
  「X」:大写的X,每按一次,删除光标所在位置的"前面"一个字符。
  「#X」:例如,「20X」表示删除光标所在位置的"前面"20个字符。
  「dd」:删除光标所在行。
  「#dd」:从光标所在行开始删除#行

5)复制
  「yw」:将光标所在之处到字尾的字符复制到缓冲区中。
  「#yw」:复制#个字到缓冲区
  「yy」:复制光标所在行到缓冲区。
  「#yy」:例如,「6yy」表示拷贝从光标所在的该行"往下数"6行文字。
  「p」:将缓冲区内的字符贴到光标所在位置。注意:所有与"y"有关的复制命令都必须与"p"配合才能完成复制与粘贴功能。

6)替换
  「r」:替换光标所在处的字符。
  「R」:替换光标所到之处的字符,直到按下「ESC」键为止。

7)回复上一次操作
  「u」:如果您误执行一个命令,可以马上按下「u」,回到上一个操作。按多次"u"可以执行多次回复。

8)更改
  「cw」:更改光标所在处的字到字尾处
  「c#w」:例如,「c3w」表示更改3个字

9)跳至指定的行
  「ctrl」+「g」列出光标所在行的行号。
  「#G」:例如,「15G」,表示移动光标至文章的第15行行首。
  • which executable file name | View the location of the executable file, check whether the system command exists and its location in the path specified by the PATH variable

  • whereis [-bmsu] [BMS dirname -f ] filename | locate executable files, source code files, help files in the file system

-b   定位可执行文件。
-m   定位帮助文件。
-s   定位源代码文件。
-u   搜索默认路径下除可执行文件、源代码文件、帮助文件以外的其它文件。
-B   指定搜索可执行文件的路径。
-M   指定搜索帮助文件的路径。
-S   指定搜索源代码文件的路径。
  • locate | Find files quickly by searching the database
-r  使用正规运算式做寻找的条件
  • find find [PATH] [option] [action] | Find files in the file tree and make corresponding processing

`Options and parameters:
1. Time-related options: a total of -atime, -ctime and -mtime and -amin, -cmin and -mmin, with -mtime description -mtime n: n is a number, meaning before n days -mtime +n : list the file names of the files whose content has been changed before n days (excluding n days itself); -mtime -n : list in The filename of the file whose content has been changed within n days (including n days itself).
-newer file : file is an existing file, listing the filenames newer than file

  1. Parameters related to the user or group name: -uid n : n is a number, this number is the user's account ID, that is, UID -gid n : n is a number, this number is the ID of the group name, that is, GID -user name : name is the user account name! For example dmtsai
    -group name: name is the group name, such as users; -nouser: looking for the owner of the file who does not exist in /etc/passwd! -nogroup : Find files whose owning group does not exist in /etc/group!

  2. Parameters related to file permissions and names: -name filename: Search for files with filename filename (wildcards can be used) -size [+-]SIZE: Search for files larger (+) or smaller (-) than SIZE. The specifications of this SIZE are:
    c: stands for byte
    k: stands for 1024bytes. Therefore, to find a file larger than 50KB, it is "-size +50k" -type TYPE: the type of the search file is TYPE, the main types are:
    general regular file (f)
    device file (b, c)
    directory ( d)
    Link file (l)
    socket (s)
    FIFO (p) -perm mode: search for files with file permissions "just equal to" mode, this mode is an attribute value similar to chmod, for example, -rwsr-xr-x The attribute is 4755! -perm -mode
    : Search for files whose file permissions "must include all the permissions of mode". For example,
    we want to search for files with -rwxr-r- which is 0744. Use -perm -0744, when the permissions of a file are -rwsr-xr-x, ie 4755, will also be listed, because the properties of -rwsr-xr-x already include the properties of -rwxr–r–. -perm
    +mode: search for files with file permissions "including the permissions of any mode", for
    example , when we search for -rwxr-xr-x, that is, -perm +755, but a file attribute is -rw——- will also be listed because he has the -rw…. attribute!

  3. Additional actions that can be performed: -exec command: command is other commands, and additional commands can be followed by -exec to process the searched results. -print : print the result to the screen, this action is a preset action! eg:
    find / -perm +7000 -exec ls -l {} \; , the extra instruction starts with -exec, and ends with \; {} replaces the previously found content | xargs
    -i The default previous output is replaced by {}
    eg:
    find . -name "*.log" | xargs -i mv {}
    test4`
  • grep 'regular expression' filename | Use regular expressions to search for text and print out the matching lines (this is the most commonly used query command, you must remember it)
-c  只输出匹配行的计数。
-I  不区分大小写(只适用于单字符)。
-l  只显示文件名
-v  显示不包含匹配文本的所有行。
-n  显示匹配行数据及其行号
  • gzip [-cdtv#] filename | compress, decompress, source files no longer exist
-d  进行解压缩
-c  将压缩的数据输出到屏幕上
-v  :显示原档案/压缩文件案的压缩比等信息
-#  :压缩等级,-1最快,但压缩比最差,=9最慢,但压缩比最好
  • gunzip | unzip
  • bzip2 | compress, decompress
-d  :解压
-z  :压缩
-k  :保留源文件
-c :将压缩的过程产生的数据输出到屏幕上!
-v :可以显示出原档案/压缩文件案的压缩比等信息;
-# :与 gzip 同样的,都是在计算压缩比的参数, -9 最佳, -1 最快!
  • bzcat reads data without decompression

  • .tar [ main option + auxiliary option] file or directory | multiple directories or archives are packaged and compressed into a large file

主选项:
   -c  建立打包档案,可搭配 -v 来察看过程中被打包的档名(filename)
   -t  察看打包档案的内容含有哪些档名,重点在察看『档名』就是了;
   -x  解打包或解压缩的功能,可以搭配 -C (大写) 在特定目录解开
辅选项:
   -j  透过 bzip2 的支持进行压缩/解压缩:此时档名最好为 *.tar.bz2
   -z  透过 gzip 的支持进行压缩/解压缩:此时档名最好为 *.tar.gz
    -v  在压缩/解压缩的过程中,将正在处理的文件名显示出来!
   -f filename -f 后面要立刻接要被处理的档名!
   -C 目录   这个选项用在解压缩,若要在特定目录解压缩,可以使用这个选项。
   --exclude FILE:在压缩打包过程中忽略某文件 eg: tar --exclude /home/zenghao -zcvf myfile.tar.gz /home/* /etc
   -p  保留备份数据的原本权限与属性,常用于备份(-c)重要的配置文件
   -P(大写)  保留绝对路径,亦即允许备份数据中含有根目录存在之意;
   eg:解压到 opt/soft/apollo/ 
   tar -zxvf xxx.tzr.gz -C ../opt/soft/apollo/
  • exit exit the current shell
  • logout exit the login shell
  • users Displays the users currently logged in to the system
  • who logs in the user and source of the machine
-H或--heading  显示各栏位的标题信息列。
  • w Log in to the local user and the programs they run
-s  使用简洁格式列表,不显示用户登入时间,终端机阶段作业和程序所耗费的CPU时间。
-h  不显示各栏位的标题信息列。
  • finger [options] [user] [user@host] | View user information
-s 显示用户的注册名、实际姓名、终端名称、写状态、停滞时间、登录时间等信息
-l 除了用-s选项显示的信息外,还显示用户主目录、登录shell、邮件状态等信息,以及用户主目录下的.plan、.project和.forward文件的内容。
-p 除了不显示.plan文件和.project文件以外,与-l选项相同
  • hostname View hostname
  • unalias ii | clear aliases
  • useradd [-u UID] [-g initial group] [-G secondary group] [-c description column] [-d absolute path to home directory] [-s shell] user account name | add user
-M  不建立用户家目录!(系统账号默认值)
-m  建立用户家目录!(一般账号默认值)
-r  建立一个系统的账号,这个账号的 UID 会有限制 
-e  账号失效日期,格式为『YYYY-MM-DD-D  查看useradd的各项默认值
  • passwd | change password
-l  使密码失效
-u  与-l相对,用户解锁
-S  列出登陆用户passwd文件内的相关参数
-n  后面接天数,shadow 的第 4 字段,多久不可修改密码天数
-x  后面接天数,shadow 的第 5 字段,多久内必须要更动密码
-w  后面接天数,shadow 的第 6 字段,密码过期前的警告天数
-i  后面接『日期』,shadow 的第 7 字段,密码失效日期
使用管道刘设置密码:echo "zeng" | passwd --stdin zenghao
  • userdel delete user
-r  用户文件一并删除
  • chage [-ldEImMW] account name | Modify the related attributes of user password
-l  列出该账号的详细密码参数;
-d  后面接日期,修改 shadow 第三字段(最近一次更改密码的日期),格式YYYY-MM-DD
-E  后面接日期,修改 shadow 第八字段(账号失效日),格式 YYYY-MM-DD
-I  后面接天数,修改 shadow 第七字段(密码失效日期)
-m  后面接天数,修改 shadow 第四字段(密码最短保留天数)
-M  后面接天数,修改 shadow 第五字段(密码多久需要进行变更)
-W  后面接天数,修改 shadow 第六字段(密码过期前警告日期)
  • usermod [-cdegGlsuLU] username | Modify user related attributes
-c  后面接账号的说明,即 /etc/passwd 第五栏的说明栏,可以加入一些账号的说明。
-d  后面接账号的家目录,即修改 /etc/passwd 的第六栏;
-e  后面接日期,格式是 YYYY-MM-DD 也就是在 /etc/shadow 内的第八个字段数据啦!
-f  后面接天数为 shadow 的第七字段。
-g  后面接初始群组,修改 /etc/passwd 的第四个字段,亦即是GID的字段!
-G  后面接次要群组,修改这个使用者能够支持的群组
-l  后面接账号名称。亦即是修改账号名称, /etc/passwd 的第一栏!
-s  后面接 Shell 的实际档案,例如 /bin/bash 或 /bin/csh 等等。
-u  后面接 UID 数字啦!即 /etc/passwd 第三栏的资料;
-L  冻结密码
-U  解冻密码
  • id [username] | View user-related id information, and can also be used to determine whether the user exists
  • groups View the groups supported by the logged in user, the first output group is the valid group
  • newgrp switch effective group
  • groupadd [-g gid] group name | add group
-g  设定添加组的特定组id
  • groupmod [-g gid] [-n group_name] group name | modify group information
-g  修改既有的 GID 数字
-n  修改既有的组名
  • groupdel [groupname] | delete group
  • gpasswd | group admin function
root管理员动作:
   -gpasswd groupname 设定密码
   -gpasswd [-A user1,...] [-M user3,...] groupname
       -A  将 groupname 的主控权交由后面的使用者管理(该群组的管理员)
       -M  将某些账号加入这个群组当中
   -gpasswd [-r] groupname
       -r  将 groupname 的密码移除
群组管理员动作:
 - gpasswd [-ad] user groupname 
       -a  将某位使用者加入到 groupname 这个群组当中
       -d  将某位使用者移除出 groupname 这个群组当中
  • df [options] [files] | Display the free space of the specified disk file, if no file name is specified, the free space of all currently mounted file systems will be displayed
-a  显示全部文件系统
-h  文件大小友好显示
-l  只显示本地文件系统
-i  显示inode信息
-T  显示文件系统类型
  • diff [parameters] [file 1 or directory 1] [file 2 or directory 2] | Compare the contents of a single file or directory
-b  不检查空格字符的不同。
-B  不检查空白行。
-i  不检查大小写
-q  仅显示差异而不显示详细信息
eg: diff a b > parch.log 比较两个文件的不同并产生补丁
  • cal [parameter] month] [year] | view calendar
-1  显示当月的月历
-3  显示前、当、后一个月的日历
-m  显示星期一为一个星期的第一天
-s  (默认)星期天为第一天
-j  显示当月是一年中的第几天的日历
-y  显示当前年份的日历
  • ps | list snapshots of the current process
a   显示所有的进程
-a  显示同一终端下的所有程序
e   显示环境变量
f   显示进程间的关系
-H  显示树状结构
r   显示当前终端的程序
T   显示当前终端的所有程序
-au 显示更详细的信息
-aux    显示所有包含其他使用者的行程 
-u  指定用户的所有进程
  • top [parameters] | Displays information about the process currently being executed by the system, including process ID, memory usage, CPU usage, etc.
  • kill [parameter] [process number] | kill process
  • free [parameter] | Display the free, used physical memory and swap memory in the Linux system, and the buffer used by the kernel
  • at [parameter] [time] | Execute a specified task at a specified time, only once
  • ping [parameters] [hostname or IP address] | Test connectivity to the target host
  • netstat | Displays statistics related to IP, TCP, UDP and ICMP protocols
  • rcp [parameters] [source file] [destination file] | remote file copy
-r  递归复制
-p  保留源文件的属性
usage: rcp –r remote_hostname:remote_dir local_dir
  • awk
-F 分隔符  以分隔符分隔内容
{}  要执行的脚本内容 eg:cat /etc/passwd |awk  -F ':'  '{print $1"\t"$7}'
  • sed to replace, delete, add, select, etc.
a   新增,在新的下一行出现
c   取代,取代 n1,n2 之间的行 eg: sed '1,2c Hi' ab
d   删除
i   插入,在新的上一行出现
  • paste merges files, make sure the number of lines in the merged two files is the same
-d  指定不同于空格或tab键的域分隔符
-s  按行合并,单独一个文件为一行
  • su [parameter] user | switch login
-l  切换时连同环境变量、工作目录一起改变
-c command  执行command变回原来的使用者
  • sudo | execute a specific command with the privileges of a specific user
-l  列出当前用户可执行的命令
-u username#uid 以指定用户执行命

Partial reference – https://mp.weixin.qq.com/s/DdADHt-UKeaXWDvty2dHhw

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325860088&siteId=291194637