Common basic commands for linux system

  The linux system has two working interfaces, a command line interface and a graphical interface. In the daily operation and maintenance of the server, operations are performed by the command line interface, and the use of common command tools is the basic skill of operation and maintenance personnel. This chapter explains all the most practical commands and skills in the company's daily life, which must be mastered and used skillfully.

3.1.1 Basic operation of file directory

  1)cd

  Role: Switch working directory

  Example: 

  [centos7 the root @ ~] #   CD / home    # enter / home directory
  [centos7 the root @ Home] #   CD ..    # switch the current directory to a directory on
  [@ centos7 the root /] #   CD -      # switching back to a The directory where the cd command is executed
  [root@centos7 home]# cd #Switch       to the user's home directory

  Tip: When using the cd command, you must specify the complete directory path to be switched, and execute subsequent commands after confirming that the directory path is correct to avoid errors.

  2) ls

  Role: Display and view file directory information

  Parameters: -a display all information including hidden files; -d display the information of the directory itself; -l list detailed information line by line; -t sort by time; -u display the last access time; -c display the attribute change time.

  Example:

  [root@centos7 home]# ls #Display             the name of the current directory and file
  [root@centos7 home]# ls -a #Display             the hidden files under /home
  [root@centos7 home]# ls -l #Display the             files and directories Detailed information
  [root@centos7 home]# ls -ld #View  detailed information of the          /home directory itself
  [root@centos7 home]#  ls -lt /etc/profile
  #View       detailed information sorted by time [root@centos7 home]#  ls -lu /etc/profile #Display the       last visit time
  [root@centos7 home]# ls -lc /etc/profile #Display        the time of attribute change

  Tip: ls command is the most commonly used command, ll is another name for ls -l, so you can directly use ll command to view file directory information, which is faster and more convenient.

  3)pwd

  Role: View the full path of the current working directory

  Example:

  [root@centos7 test]# pwd #Execute the         command to view the full path information of the current directory
  /home/test #The  current working directory returned is /home/test

  Tip: Use decisively when you are not sure about the current working directory

  4)mkdir

  Role: Create a directory

  Parameters: -p is used to recursively create multi-level directories; -m specifies directory permissions.

  Example:

  [root@centos7 home]# mkdir test #Create      a test directory
  [root@centos7 home]#  mkdir apple huiwei xiaomi #Create    multiple directories at the same level at once
  [root@centos7 home]#  mkdir -m 777 meizu #Create    a 777-privileged Directory
  [root@centos7 home]# mkdir -p /home/super/linux/charter #Create    multi-level directories at once

  Tip: The directory can be created at will, but for management and safety, please use concise vocabulary to plan the name and level of the directory.

  5)touch

  Role: create a new file

  Example: 

  [root@centos7 home]#  touch oppo.txt #Create  a txt file
  [root@centos7 home]#  touch 1.conf mayi.txt dog #Create  multiple files at once

  Tip: The touch command also has many parameters, but it is not recommended to use it, and it is not necessary to add parameters in daily life.

  6)cp

  Role: Copy file directory

  Parameters: -r recursively copy the directory; -a copy content remains exactly the same as the source file directory, including links and attributes.

  Example:

  [root@centos7 home]# cp /home/oppo.txt /tmp  #复制文件到tmp目录
  [root@centos7 home]# cp /home/oppo.txt /tmp/vivo.txt  #复制文件到tmp目录,并修改文件名
  [root@centos7 home]# cp -r /home/test /tmp/      #将test目录复制到tmp目录下
  [root@centos7 home]# cp -a /home/super/linux /tmp/  #将linux目录完整复制到tmp目录下

  提示:cp命令常用于备份文件,复制过程中注意命令的反馈提示。

  7)mv

  作用:移动、重命名文件目录

  示例:

  [root@centos7 home]# mv mayi.txt /tmp/     #移动文件到tmp目录
  [root@centos7 home]# mv o.txt /tmp/u.txt     #移动文件到tmp目录,并改名
  [root@centos7 home]# mv super /tmp/      #移动目录到tmp下

  提示:移动文件目录时,必须确认文件是否被运行中的应用使用。

   8)rm

  作用:删除文件目录

  参数:-r 删除目录;-f 强制删除,无提示信息。

  示例:

  [root@centos7 home]# rm 1.conf         #删除文件
  [root@centos7 home]# rm -f oppo.txt       #直接删除文件,无提示
  [root@centos7 home]# rm -r /home/huiwei     #删除目录
  [root@centos7 home]# rm -rf /home/test     #直接删除目录,无提示
  [root@centos7 home]# rm -rf /home/test/*    #保留test目录并删除该目录下所有内容

  提示:使用删除命令时,在未确认删除目标详情时,不要使用-f参数,该参数需谨慎使用。

3.1.2 查看文件内容

  1)cat 

  作用:直接查看文件内容

  参数:-n 显示所有行行号;-b 空白行不显示行号。

  示例:

  [root@centos7 home]# cat 1.txt           #查看文件内容
  [root@centos7 home]# cat -n 1.txt          #查看文件并显示非空白行行号
  [root@centos7 home]# cat -b 1.txt           #查看文件并显示行号
  [root@centos7 home]# cat 1.txt 2.txt 3.conf     #同时打印查看多个文件
  [root@centos7 home]# cat /dev/null > 1.txt     #清空1.txt文件内容
  [root@centos7 home]# cat /var/install.log > 2.txt   #将日志输出到新文件中

  提示:cat命令更适合小文件内容查看,大文件则与grep命令结合使用效果最好。

  2)head

   作用:从文件开头查看文件内容,默认前10行

  参数:-n 显示前n行内容;-c nK 按容量字节大小显示nKB内容。

  示例:

  [root@centos7 home]# head /etc/passwd       #默认查看前10行
  [root@centos7 home]# head -5 /etc/passwd     #查看前5行
  [root@centos7 home]# head -c 1K /etc/passwd   #查看前1k大小的内容

  提示:该命令常与排序sort命令一起使用,用于筛选最高最小排序值。

  3)tail

  作用:查看文件尾部内容

  参数:-n 显示末尾n行;-f 动态输出文件内容;-c nK 显示文件最后nKB内容。

  示例: 

  [root@centos7 home]# tail /var/log/messages       #查看文件最后10行
  [root@centos7 home]# tail -200 /var/log/messages     #查看文件最后200行内容
  [root@centos7 home]# tail -f /var/log/messages     #动态输出查看文件内容
  [root@centos7 home]# tail -100f /var/log/messages   #查看从最后100行持续输出的内容
  [root@centos7 home]# tail -c 1K /var/log/messages   #查看文件末尾1KB大小的内容

  提示:使用-f 动态输出后,可使用Ctrl+c组合键退出查看。

  4)more

   作用:分页查看内容

  示例

  [root@centos7 home]# more /var/log/secure  #分页查看

  提示:more查看状态下,空格键看下翻一页,Enter键看下一行,q键退出查看。

  5)less

   作用:分页查看内容

  示例

  [root@centos7 home]# less /var/log/secure  #分页查看

  提示:在查看状态下,空格键看下翻一页,b键上翻一页,q键退出查看。

  6)echo

  作用:输出指定内容

  示例:

  [root@centos7 home]# echo $LANG      #输出查看系统参数LANG值
  [root@centos7 home]# echo "I love linux"    #直接输出字符串
  [root@centos7 home]# echo "这是教程" > 1.txt  #将文本输出到文件,无文件会默认创建新文件
  [root@centos7 home]# echo "这是新教程" >> 1.txt   #追加写入文本到文件末尾
  [root@centos7 home]# echo "" > /var/log/nginx.log  #清空nginx日志文件

  提示:echo使用 > 时,具有覆盖文件内容的功能,务必确认文件可否清空或覆盖。  

3.1.3 查找与统计

   1)find

  作用:查找文件目录

  参数:-name 按名称查找;-user 按用户查找;-group 按组查找;-empty 查找空目录空文件;-perm 按权限查找;-mtime 按修改时间查找;-size 按容量大小查找;-exec 对找到的内容执行命令;-type 按类型查找,f-文件 d-目录 b c-设备 l-链接;-o 或者条件;-a 并且条件。

  示例:

  [root@centos7 ~]# find -name test.txt    #查找名称为test.py的文件
  [root@centos7 ~]# find -iname tesT.txt      #查找test.py文件,不区分大小写
  [root@centos7 ~]# find / -user game      #查找用户名为game的文件或目录
  [root@centos7 ~]# find ./ -group game      #查找组名为game的文件或目录
  [root@centos7 ~]# find ./ empty        #查找空的目录或文件
  [root@centos7 ~]# find ./ -perm 644     #查找当前目录下权限为644的文件
  [root@centos7 ~]# find /home -mtime -3    #查找home下所有3天内修改过的文件或目录
  [root@centos7 ~]# find /home -mtime +5    #查找home下所有5天前被修改过的文件或目录
  [root@centos7 ~]# find /home -mtime 6   #查找6天前当天修改的文件或目录
  [root@centos7 ~]# find /data -size +20M    #查找data目录下大于20M的文件或目录
  [root@centos7 ~]# find /home -size +2M -exec ls -l {} \;   #列出home下大于2M的文件或目录
  [root@centos7 ~]# find /home -size +3M -a -type f -exec rm -rf {} \;  #删除home下大于3M的文件
  [root@centos7 ~]# find /home -type f -name "*.log"  #查找日志文件
  [root@centos7 ~]# find /home -amin n         #最后n分钟 
  [root@centos7 ~]# find /home -atime n       #最后n天
  [root@centos7 ~]# find /home -cmin n       #最后n分钟改变状态
  [root@centos7 ~]# find /home -ctime n        #最后n天改变状态
  [root@centos7 ~]# find -name *.txt |xargs -i cp {} /tmp/    #把TXT文件复制到/tmp/目录下
  [root@centos7 ~]# find -name *.txt -exec cp '{}' /tmp/ \;  #把TXT文件复制到/tmp/目录下

  提示:find命令功能强大应用广泛,常用于查找并执行后续命令,注意exec和xargs的用法。

  2)grep

  作用:查找打印找与关键词匹配的行

  参数:-n 在输出匹配行及行号一起输出;-v 打印不符合要求的行(取反);-i 不区分大小写;-w 打印完整单词的行;-An 打印符合要求的行以及下面n行;-Bn 打印符合要求的行以及上面n行;-Cn 打印符合要求的行以及上下n行。

  示例:

  [root@centos7 ~]# grep -A2 'halt' /etc/passwd     #把包含halt的行及其下面的两行输出
  [root@centos7 ~]# grep -B2 'halt' /etc/passwd     #把包含halt的行及其上面的两行输出
  [root@centos7 ~]# grep -C2 'halt' /etc/passwd     #把包含halt的行及其上下各两行输出
  [root@centos7 ~]# grep -n 'root' /etc/passwd      #过滤出带有root关键字的行及行号
  [root@centos7 ~]# grep -v 'nologin' /etc/passwd     #过滤不带有nologin的行
  [root@centos7 ~]# grep '[0-9]' /etc/inittab           #过滤出所有包含数字的行
  [root@centos7 ~]# grep -v '[0-9]' /etc/inittab        #过滤出所有不包含数字的行
  [root@centos7 ~]# grep -v '^#' /etc/inittab           #过滤出非#开头的行
  [root@centos7 ~]# grep  'game$' /etc/inittab         #过滤出以game结尾的行
  [root@centos7 ~]# grep -v '^#' /etc/crontab |grep -v '^$'   #去除所有空行和以#开头的行

  提示:grep拥有众多参数用法,这里是常用的几种示例,更多用法在后续shell脚本编程的章节讲解。

  3)wc

   作用:统计文件内容信息

  参数:-w 统计单词书;-c 统计字节数;-l 统计行数。

  示例:

  [root@centos7 ~]# wc -c  /etc/passwd    #显示文件总字节数
  [root@centos7 ~]# wc -l   /etc/passwd    #显示文件总行数
  [root@centos7 ~]# wc -w /etc/passwd    #显示单词总数,重复不计

  提示:常于cat、sort、grep等组合使用,来筛选统计特定信息数量。

  4)sort

  作用:排序

  参数:-n 依照数值的大小排序;-u 去除重复内容;-r 降序排列;-o 将输出写入文件。

  示例:

  [root@centos7 ~]# sort oppo.txt      #默认以字母排序
  [root@centos7 ~]# sort -n num.txt     #以数值排序
  [root@centos7 ~]# sort -r oppo.txt     #以字母降序排列
  [root@centos7 ~]# sort -nr num.txt     #以数值降序排列
  [root@centos7 ~]# sort -u oppo.txt     #去除重复排序
  [root@centos7 ~]# sort num.txt -o /tmp/n.txt   #排序后写入到新文件

  提示:sort是只针对文本内容,按行进行排序的。

  5)uniq

  作用:检查统计重复的行

  参数:-c 去重并计数。

  示例:

  [root@centos7 ~]# uniq -c oppo.txt     # 去重后排列计数
  [root@centos7 ~]# sort test.txt | uniq -c   # 排序后去重计数

  提示:该命令常与sort一起使用,用来排序统计信息

  6)cut

  作用:剪切出指定的字符字段

  参数:-b 以字节为单位分割;-c 以字符为单位分割;-d 用自定义分割符分割;-f 指定列显示内容。

  示例:

  [root@centos7 ~]# cut -f2,3 test.txt     #截取第2、3列
  [root@centos7 ~]# cut -f1 -d ":" 1.txt     #以:为分隔符截取第一列
  [root@centos7 ~]# cut -c -2 1.txt      #输出每行的前两个字符

  提示:常用于提取特定文本信息。

3.1.4 打包压缩与解压

   1)zip/unzip

  作用:压缩/解压缩文件

  参数:-d 指定解压目录 

  示例:

  [root@centos7 ~]# zip j.zip jjj.txt    #将jjj.txt压缩为j.zip文件
  [root@centos7 ~]# unzip j.zip      #将j.zip解压
  [root@centos7 ~]# unzip j.zip -d /tmp/  #将j.zip解压到指定tmp目录下

  提示:系统默认编制zip和unzip,需自行安装 yum install zip unzip -y。

  2)tar

  作用:打包压缩与解压

  参数:-c 创建打包文件;-f 指定打包名;-j 打包后通过bzip2格式压缩;-r  追加文件到打包文档;-z 打包后通过gzip格式压缩;-x 解包文件;-C 指定解压路径;--delete 从打包文件中去除某个文件;--remove-files 打包后删除源文件。

  示例:

  [root@centos7 ~]# tar -cf test.tar /home/       #将home目录打包为test.tar
  [root@centos7 ~]# tar -czf test.tar.gz /home/       #将home目录打包并压缩为test.tar.gz
  [root@centos7 ~]# tar -zxf test.tar.gz           #将gz文件解压
  [root@centos7 ~]# tar -cjf test.tar.bz2 /home/       #将home目录打包并压缩为test.tar.bz2
  [root@centos7 ~]# tar -jxf test.tar.bz2 -C /tmp/     #将bz2文件解压到指定目录
  [root@centos7 ~]# tar -f test.tar -r test.txt         #将test.txt追加到打包中
  [root@centos7 ~]# tar --delete game.txt -f test.tar    #从打包文件中删除game.txt

  提示:tar是最常用的打包工具,谨慎使用--delete和--remove-files参数。

  3)gzip/bzip2

  作用:压缩与解压

  参数:-d 解压。

  示例: 

  [root@centos7 ~]# gzip test.py       #压缩后为test.py.gz
  [root@centos7 ~]# gzip -d test.py.gz     #解压文件
  [root@centos7 ~]# bzip2 test.py      #压缩后为test.py.bz2
  [root@centos7 ~]# bzip2 -d test.py.gz    #解压文件

  提示:gzip和bzip2 只能打包压缩文件,不能打包压缩目录。

3.1.5 计划任务与链接

  1)at

   作用:指定时间一次性执行命令

  参数:-l 查看用户任务计划;-c 查看任务具体内容;-d 删除任务计划。

  示例:

  [root@centos7 ~]# at 10:10     #在10点10分执行任务
  at> touch mac.txt         #任务内容创建文件
  at> mkdir -p /home/backup     #任务内容创建目录
  at> <EOT>            #Ctrl+d组合键结束编辑
  [root@centos7 ~]# at -l      #查看任务计划
  [root@centos7 ~]# at -c 1     #查看编号1任务内容
  [root@centos7 ~]# at -d 1     #删除1号任务

  提示:at 使用必须先启动atd服务,systemctl start atd。

  2)crontab

  作用:创建周期性计划任务

  参数:-u 指定一个用户;-l 列出某个用户的任务计划;-r 删除某个用户的任务;-e 编辑某个用户的任务。

  时间格式:分时日月周+命令

    Minute  Hour Day  Month Week  command 
    分钟   小时  天    月    星期    命令 
    0-59   0-23  1-31  1-12    0-6    command

  示例:

  [root@centos7 ~]# crontab -e     #创建计划任务
  5 * * * * ls -l /tmp      #指定每小时的第5分钟执行一次查看命令 
  10 7 1 * * ping 192.168.1.1   #指定每月1号的7:10分执行ping命令 
  30 8-15 * * * ll           #每天8-15点的第30分钟执行ll命令 
  */10 * * * * ls           #每10分钟执行一次ls命令 
  30 6 */10 * * ll           #每月每隔10天6:30执行一次ll命令
  0 2 * * * sh /home/test.sh      #每天两点执行脚本
  :wq! 或者shift+zz        #快捷保存退出
  [root@centos7 ~]# crontab -l    #查看当前用户的计划任务

  提示:crontab的编辑与vim的编辑操作一样。

  3)ln

   作用: 创建链接

  示例:

  [root@centos7 ~]# ln -s /home/oppo.txt /tmp/o.txt    #创建文件软连接
  [root@centos7 ~]# ln -s /home/backup /tmp/backup    #创建目录软连接
  [root@centos7 ~]# ln -s /home/vivo.txt /tmp/p.txt     #创建文件硬连接

  提示:软连接可跨分区,不可删源文件;硬链接不可跨分区,可删源文件。

3.1.6 系统服务

   1)systemctl

  作用:启动、停止服务

  示例: 

  [root@centos7 ~]# systemctl start nginx     #启动nginx服务
  [root@centos7 ~]# systemctl stop mysql      #停止mysql服务
  [root@centos7 ~]# systemctl restart sshd      #重启sshd服务

   2)history

  作用:查看历史命令记录

  示例: [root@centos7 ~]# history      #查看历史命令

   3)sysctl

  作用:加载系统参数

  示例:[root@centos7 ~]# sysctl -p  #默认加载/etc/sysctl.conf中的参数

  4)source

  作用:执行环境变量文件

  示例:[root@centos7 ~]# source /etc/profile  #执行/etc/profile中的环境变量,使其生效。

  5)stat

  作用:查看文件inode信息,包含文件大小、类型、权限和时间状态

  示例:[root@centos7 ~]# stat oppo.txt  #查看oppo.txt的详细inode信息

   6)reboot

  作用:重启linux系统

   示例:[root@centos7 ~]# reboot  #直接重启,谨慎使用

  7)hostnamectl

  作用:设置主机名称

  示例:[root@centos7 ~]# hostnamectl set-hostname supertest  #设置主机名为supertest

  8)lscpu

  作用:查看cpu的数量

  示例:[root@centos7 ~]# lscpu    #查看机器cpu详细信息和数量

   本节的基础命令均是企业服务器管理中需要用到的,示例中列出的是最常用的几种形式,务必学习掌握其使用方法。每个命令都有很多参数,可以通过--help查看,但大多参数在日常服务器管理操作中是用不到的。命令介绍中有提示一项,这是使用经验的提醒,了解提示,可少走弯路。

 

Guess you like

Origin blog.51cto.com/superleedo/2544884