Linux学习Day3:新手必须掌握的Linux命令(二)

  今天学习的命令都是运维工作中经常要用到的,非常实用,必须要用心学习,争取把这些命令烂熟于心,具体内容如下:

一、系统状态监测命令


   1、ifconfig命令

  用于获取网卡配置与网络状态等信息。

[root@linuxprobe Desktop]# ifconfig
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.134.128  netmask 255.255.255.0  broadcast 192.168.134.255
        inet6 fe80::20c:29ff:feff:6b1f  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ff:6b:1f  txqueuelen 1000  (Ethernet)
        RX packets 32  bytes 3653 (3.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 58  bytes 7267 (7.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 6  bytes 560 (560.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6  bytes 560 (560.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0 
  eno16777728 : 网卡名称
  inet 192.168.134.128 : IP地址
  ether 00:0c:29:ff:6b:1f : MAC地址
  RX packets 32  bytes 3653 (3.5 KiB) : 接收数据包的个数以及累计流量,重启系统则流量清零
  TX packets 58 bytes 7267 (7.0 KiB) :发送数据包的个数以及累计流量,重启系统则流量清零

   2、uname命令

  用于查看系统内核与系统版本等信息,一般会固定搭配-a参数

[root@linuxprobe ~]# uname -a
Linux linuxprobe.com 3.10.0-123.el7.x86_64 #1 SMP Mon May 5 11:16:57 EDT 2014 x86_64 x86_64 x86_64 GNU/Linux

   3、uptime命令

  用于查看系统的负载信息,系统在1分钟、5分钟、15分钟内的平均负载值(加粗信息)尽量不要长期超过1,在生产环境中不要超过5。

[root@linuxprobe ~]# uptime
 11:12:49 up 23 min,  2 users,  load average: 0.08, 0.04, 0.05

   4、free命令

   用于显示当前系统中内存的使用量信息,一般结合-h参数使用增加可读性。

[root@linuxprobe ~]# free -h
             total       used       free     shared    buffers     cached
Mem:          1.9G       910M       1.1G       9.6M       924K       295M
-/+ buffers/cache:       614M       1.3G
Swap:         2.0G         0B       2.0G

   5、who命令

  用于查看当前登录主机的用户终端信息,下面输出结果显示当前打开一个终端。

[root@linuxprobe ~]# who
root     :0           2020-02-17 10:49 (:0)
root     pts/0        2020-02-17 10:50 (:0)

  6、last命令

  用于查看所有本机的登录记录,要注意的是,登录记录可以被人为篡改。

[root@linuxprobe ~]# last
root     pts/0        :0               Mon Feb 17 10:50   still logged in   
root     :0           :0               Mon Feb 17 10:49   still logged in   
(unknown :0           :0               Mon Feb 17 10:49 - 10:49  (00:00)    
reboot   system boot  3.10.0-123.el7.x Mon Feb 17 10:49 - 11:32  (00:42)    
root     pts/2        192.168.134.1    Sun Feb 16 19:58 - 20:05  (00:07)    
root     pts/1        192.168.134.1    Sun Feb 16 16:51 - down   (04:48)

  7、history命令

   用于显示历史执行过的命令,还可以使用“!数字”的方式来重复执行某一次的命令。

[root@linuxprobe ~]# history
    1  cat /etc/redhat-release 
    2  reboot
    3  ls
    4  passwd root
    5  su - linuxprobe
    6  shutdown
    7  shutdown -r

  8、sosreport命令

  用于收集系统配置与架构信息并输出诊断文档。

[root@linuxprobe ~]# sosreport

sosreport (version 3.0)

This command will collect diagnostic and configuration information from
this Red Hat Enterprise Linux system and installed applications.

An archive containing the collected information will be generated in
/var/tmp and may be provided to a Red Hat support representative.

Any information provided to Red Hat will be treated in accordance with
the published support policies at:

  https://access.redhat.com/support/

The generated archive may contain data considered sensitive and its
content should be reviewed by the originating organization before being
passed to any third party.

No changes will be made to system configuration.

Press ENTER to continue, or CTRL-C to quit.   //此处按下回车键来确认收集信息

Please enter your first initial and last name [linuxprobe.com]:       //此处按下回车键来确认主机编号
Please enter the case number that you are generating this report for:  //此处按下回车键


Running plugins. Please wait ...

Running 1/69: abrt...
Running 6/69: auditd...
Running 7/69: block...
Running 69/69: yum...
Creating compressed archive...

Your sosreport has been generated and saved in:
/var/tmp/sosreport-linuxprobe.com-20200217114206.tar.xz    //生成的诊断文档保存的位置

The checksum is: fd53340877a57c34803cccbcc9d15676

Please send this file to your support representative.

二、工作目录切换命令


  1、pwd命令

  用于显示用户当前所在的工作目录。

[root@linuxprobe ~]# pwd
/root

  2、cd命令

  用于切换工作路径。“cd -”命令返回上一次所处的目录,“cd ..”命令返回上级目录,“cd ~”命令切换至当前用户的家目录。

[root@linuxprobe ~]# cd /etc
[root@linuxprobe etc]# 

  3、ls命令

  用于显示目录中的文件信息,-a参数可以显示隐藏文件,-l参数可以显示文件的属性、大小等详细信息。

[root@linuxprobe ~]# ls -al
total 64
dr-xr-x---. 14 root root 4096 Feb 17 16:28 .
drwxr-xr-x. 17 root root 4096 Feb 17 16:28 ..
-rw-------.  1 root root 1032 Feb 15 20:24 anaconda-ks.cfg
-rw-------.  1 root root 3299 Feb 17 16:28 .bash_history
-rw-r--r--.  1 root root   18 Dec 29  2013 .bash_logout
-rw-r--r--.  1 root root  176 Dec 29  2013 .bash_profile
-rw-r--r--.  1 root root  176 Dec 29  2013 .bashrc
drwx------.  9 root root 4096 Feb 15 19:46 .cache
drwx------. 15 root root 4096 Feb 15 19:46 .config
-rw-r--r--.  1 root root  100 Dec 29  2013 .cshrc
drwx------.  3 root root   24 Feb 15 12:25 .dbus
drwxr-xr-x.  2 root root    6 Feb 15 19:41 Desktop
drwxr-xr-x.  2 root root    6 Feb 15 19:41 Documents
drwxr-xr-x.  2 root root    6 Feb 15 19:41 Downloads
-rw-------.  1 root root   16 Feb 15 19:41 .esd_auth
-rw-------.  1 root root 1240 Feb 17 16:28 .ICEauthority
-rw-r--r--.  1 root root 1083 Feb 15 12:26 initial-setup-ks.cfg
drwxr-xr-x.  3 root root   18 Feb 15 19:41 .local
drwxr-xr-x.  2 root root    6 Feb 15 19:41 Music
drwxr-xr-x.  2 root root    6 Feb 15 19:41 Pictures
drwxr-xr-x.  2 root root    6 Feb 15 19:41 Public
-rw-r--r--.  1 root root  129 Dec 29  2013 .tcshrc
drwxr-xr-x.  2 root root    6 Feb 15 19:41 Templates
drwxr-xr-x.  2 root root    6 Feb 15 19:41 Videos
-rw-------.  1 root root 1150 Feb 15 20:11 .viminfo
-rw-------.  1 root root  120 Feb 16 19:58 .Xauthority

三、文本文件编辑命令


  1、cat命令

  用于查看纯文本文件(内容较少的),-n参数可以显示行号。

[root@linuxprobe ~]# cat -n initial-setup-ks.cfg 
     1    #version=RHEL7
     2    # X Window System configuration information
     3    xconfig  --startxonboot
     4    
     5    # License agreement
     6    eula --agreed
     7    # System authorization information
     8    auth --enableshadow --passalgo=sha512
     9    # Use CDROM installation media
    10    cdrom
    11    # Run the Setup Agent on first boot
    12    firstboot --enable

  2、more命令

  用于查看纯文本文件(内容较多的),可以使用空格健或回车健向下翻页。

[root@linuxprobe ~]# more initial-setup-ks.cfg 
#version=RHEL7
# X Window System configuration information
xconfig  --startxonboot

# License agreement
eula --agreed
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom

  3、head命令

  用于查看纯文本文档的前N行,一般配合-n参数使用,下面命令表示查看文档前10行。

[root@linuxprobe ~]# head -n 10 initial-setup-ks.cfg 
#version=RHEL7
# X Window System configuration information
xconfig  --startxonboot

# License agreement
eula --agreed
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom

  4、tail命令

  用于查看纯文本文档的后N行或持续刷新内容,前者的命令形式为“tail -n 20 文件名”,后者为“tail -f 文件名”。

[root@linuxprobe ~]# tail -f /var/log/messages
Feb 17 17:40:36 linuxprobe systemd: Starting Hostname Service...
Feb 17 17:40:36 linuxprobe dbus-daemon: dbus[1042]: [system] Successfully activated service 'org.freedesktop.hostname1'
Feb 17 17:40:36 linuxprobe dbus[1042]: [system] Successfully activated service 'org.freedesktop.hostname1'
Feb 17 17:40:36 linuxprobe systemd: Started Hostname Service.
Feb 17 17:40:37 linuxprobe chronyd[1013]: Selected source 162.159.200.123
Feb 17 17:40:37 linuxprobe kernel: TCP: lp registered
Feb 17 17:40:51 linuxprobe systemd-logind: Removed session c1.
Feb 17 17:41:07 linuxprobe systemd: Starting Session 2 of user root.
Feb 17 17:41:07 linuxprobe systemd: Started Session 2 of user root.
Feb 17 17:41:07 linuxprobe systemd-logind: New session 2 of user root.
Feb 17 17:41:25 linuxprobe chronyd[1013]: Selected source 202.112.29.82

  5、tr命令

  用于替换文本中的字符,例如把文本中的英文全部替换为大写。

[root@linuxprobe ~]# cat anaconda-ks.cfg | tr [a-z] [A-Z]
#VERSION=RHEL7
# SYSTEM AUTHORIZATION INFORMATION
AUTH --ENABLESHADOW --PASSALGO=SHA512

# USE CDROM INSTALLATION MEDIA
CDROM
# RUN THE SETUP AGENT ON FIRST BOOT
FIRSTBOOT --ENABLE
IGNOREDISK --ONLY-USE=SDA
# KEYBOARD LAYOUTS
KEYBOARD --VCKEYMAP=US --XLAYOUTS='US'
# SYSTEM LANGUAGE
LANG EN_US.UTF-8

   6、wc命令

  用于统计指定文本的行数、字数、字节数,-l参数显示文本行数。

[root@linuxprobe ~]# wc -l /etc/passwd
38 /etc/passwd

  7、stat命令

  用于查看文件的具体存储信息和时间等信息。

[root@linuxprobe ~]# stat anaconda-ks.cfg 
  File: ‘anaconda-ks.cfg’
  Size: 1032          Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 68860812    Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Context: system_u:object_r:admin_home_t:s0
Access: 2020-02-18 14:55:53.095941142 +0800      //最后一次查看文件内容的时间
Modify: 2020-02-15 20:24:56.710988547 +0800      //最后一次修改文件内容的时间
Change: 2020-02-15 20:24:56.710988547 +0800      //最后一次变更文件权限或属性的时间
 Birth: -

  8、cut命令

  用于按“列”提取文本字符,-d参数设置间隔符号,-f参数设置提取第几列字符。下面命令表示以“:”作为分隔符,提取第一列数据。

[root@linuxprobe ~]# cut -d : -f 1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp

  9、diff命令

  用于比较多个文本文件的差异,--brief参数仅返回比较结果,-c参数详细比较多个文件之间的差异之处。

[root@linuxprobe ~]# diff -c anaconda-ks.cfg initial-setup-ks.cfg 
*** anaconda-ks.cfg    2020-02-15 20:24:56.710988547 +0800
--- initial-setup-ks.cfg    2020-02-15 12:26:03.533009017 +0800
***************
*** 1,26 ****
  #version=RHEL7
  # System authorization information
  auth --enableshadow --passalgo=sha512
- 
  # Use CDROM installation media
  cdrom
  # Run the Setup Agent on first boot
  firstboot --enable
- ignoredisk --only-use=sda
  # Keyboard layouts
  keyboard --vckeymap=us --xlayouts='us'
  # System language
  lang en_US.UTF-8
-------------------------省略部分输出结果---------------------------------

四、文件目录管理命令


  1、touch命令

  用于创建空白文件或设置文件的时间,可以设置文件的查看时间(atime)、权限或属性的变更时间(ctime)以及文件的修改时间(mtime),分别对应stat命令里的三个时间。-d参数表示同时修改atime和mtime。

[root@linuxprobe ~]# ls -l anaconda-ks.cfg  
-rw-------. 1 root root 1032 Feb 15 20:24 anaconda-ks.cfg
[root@linuxprobe ~]# 
[root@linuxprobe ~]# touch -d "2019-2-18 15:20" anaconda-ks.cfg     //修改anaconda-ks.cfg文件的atime和mtime

  [root@linuxprobe ~]# ls -l anaconda-ks.cfg
-rw-------. 1 root root 1032 Feb 18 2019 anaconda-ks.cfg      //修改成功

  2、mkdir命令

  用于创建空白的目录,结合-p参数来创建具有嵌套层关系的文件目录。

[root@linuxprobe ~]# mkdir -p a/b/c/d
[root@linuxprobe ~]# 
[root@linuxprobe ~]# cd a/      //进入a目录
[root@linuxprobe a]# cd b/      //进入b目录
[root@linuxprobe b]# 

  3、cp命令

  用于复制文件和目录,格式为“cp 参数 源文件 目标文件”,复制操作分为4种情况:

  •   若目标文件是目录,则把源文件复制到该目录中;
  •   若目标文件也是普通文件,则会询问是否覆盖目标文件;
  •   若目标文件不存在,则执行正常的复制操作;
  •   若源文件也是目录,则需要加-r参数。
[root@linuxprobe ~]# cp anaconda-ks.cfg anaconda-ks.cfg_bak      //备份anaconda-ks.cfg文件
[root@linuxprobe ~]# ls 
a anaconda
-ks.cfg_bak Documents initial-setup-ks.cfg Pictures Templates anaconda-ks.cfg Desktop Downloads Music Public Videos
[root@linuxprobe
~]#

  4、mv命令

  用于剪切文件或将文件重命名,在同一目录下对一个文件进行剪切操作,就是对其进行重命名。

[root@linuxprobe ~]# mv anaconda-ks.cfg_bak anaconda-ks.cfg_cpoy      //将anaconda-ks.cfg_bak文件重命名为anaconda-ks.cfg_copy
[root@linuxprobe ~]# ls
a                anaconda-ks.cfg_cpoy  Documents  initial-setup-ks.cfg  Pictures  Templates
anaconda-ks.cfg  Desktop               Downloads  Music                 Public    Videos
[root@linuxprobe ~]# 

   5、rm命令

  用于删除文件或目录,-f参数表示强制删除,不提示确认信息,删除目录需要-r参数。

[root@linuxprobe ~]# rm -f anaconda-ks.cfg_cpoy      //强制删除anaconda-ks.cfg_copy文件
[root@linuxprobe ~]# 
[root@linuxprobe ~]# ls
a  anaconda-ks.cfg  Desktop  Documents  Downloads  initial-setup-ks.cfg  Music  Pictures  Public  Templates  Videos
[root@linuxprobe ~]# 

   6、dd命令

  用来按照指定大小和个数的数据块来复制文件或转换文件,相关参数及作用如下所示:

参数 作用
if 输入的文件名称,即input file
of 输出的文件名称,即output file
bs 设置每个“块”的大小
count 设置复制“块”的个数

[root@linuxprobe ~]# dd if=/dev/zero of=test_file count=1 bs=20M //在/dev/zero设备文件中取出一个大小为20M的数据块,然后保存命名为test_file的文件 1+0 records in 1+0 records out 20971520 bytes (21 MB) copied, 0.0999151 s, 210 MB/s [root@linuxprobe ~]# ls a anaconda-ks.cfg Desktop Documents Downloads initial-setup-ks.cfg Music Pictures Public Templates test_file Videos [root@linuxprobe ~]#

  7、file命令

   用于查看文件的类型。

[root@linuxprobe ~]# file anaconda-ks.cfg 
anaconda-ks.cfg: ASCII text     //文本文件
[root@linuxprobe ~]# 
[root@linuxprobe ~]# file Downloads
Downloads: directory        //目录
[root@linuxprobe ~]# 

五、打包压缩与搜索命令


  1、tar命令

   用于对文件进行打包压缩和解压,相关参数及其作用如下:

参数 作用
-c 创建压缩文件
-x 解开压缩文件
-z 用Gzip压缩或解压
-j 用bzip2压缩或解压
-v 显示压缩或解压过程
-f 目标文件名
-P 保留原始的权限和属性
-C 指定解压到的目录 

[root@linuxprobe ~]# tar -czvf test.tar.gz /root //把/root目录通过Gzip格式进行打包压缩,压缩包命名为test.tar.gz tar: Removing leading `/' from member names /root/ /root/.bash_logout /root/.bash_profile /root/.bashrc /root/.cshrc /root/.tcshrc /root/anaconda-ks.cfg
-----------------省略部分压缩过程信息-----------------

 [root@linuxprobe ~]# ls
  a Desktop Downloads Music Public test_file Videos
  anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates test.tar.gz

  接下来将压缩文件解压到 /root/test 目录中,注意:无论压缩还是解压,-f参数都一定要放在参数的最后面。

[root@linuxprobe ~]# mkdir test    //创建test目录
[root@linuxprobe ~]# 
[root@linuxprobe ~]# tar -xzvf test.tar.gz -C /root/test     //解压
root/
root/.bash_logout
root/.bash_profile
root/.bashrc
root/.cshrc
root/.tcshrc
root/anaconda-ks.cfg

  2、grep命令

  用于在文本中执行关键词搜索,并显示匹配结果,格式为“grep 参数 关键词 文件名”,相关参数及其作用如下所示:

参数 作用
-b 将可执行文件(binary)当作文本文件(text)来搜索
-c 仅显示找到的行数
-i 忽略大小写
-n 显示行号
-v 反向选择——仅列出没有“关键词”的行

[root@linuxprobe root]# grep root /etc/passwd //搜索/etc/passwd文件中,显示包含“root”关键词的行 root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin

  3、find命令

  用来按照指定的条件来查找文件,格式为“find [查找路径] 寻找条件 操作”。相关参数及其作用如下所示:

参数 作用
-name 匹配名称
-user 匹配所有者
-group 匹配所有组
--type b/d/c/p/l/f

匹配文件类型(后面字母参数依次表示块设备、目录、字符设备、管道、链接文件、文本文件)

-size 匹配文件大小(+50KB表示查找超过50KB的文件,-50KB表示查找小于50KB的文件)
-exec .... {}\; 后面可以跟用于进一步处理搜索结果的命令

[root@linuxprobe root]# find /root -name test* //在/root目录下查找文件名以test开头的文件 /root/test_file /root/test/root/test_file [root@linuxprobe root]#

  find命令进阶实验:在整个文件系统中找出所有归属于linuxprobe用户的文件并复制到/root/findresults目录。

[root@linuxprobe ~]# find / -user linuxprobe -exec cp -a {} /root/findresults/ \;      //其中{}代表find命令搜索出的每一个文件,即假设find命令第一次搜索出了test.txt文件,则执行cp -a test.txt /root/findresults/
find: ‘/proc/10213/task/10213/fd/6’: No such file or directory
find: ‘/proc/10213/task/10213/fdinfo/6’: No such file or directory
find: ‘/proc/10213/fd/6’: No such file or directory
find: ‘/proc/10213/fdinfo/6’: No such file or directory
cp: cannot overwrite non-directory ‘/root/findresults/linuxprobe’ with directory ‘/home/linuxprobe’
[root@linuxprobe ~]# 
[root@linuxprobe findresults]# ls -l      //查看命令执行结果
total 3868
srwxrwxrwx.  1 linuxprobe linuxprobe       0 Feb 15 12:37 1960
srwxrwxrwx.  1 linuxprobe linuxprobe       0 Feb 15 12:48 1967
srwxrwxrwx.  1 linuxprobe linuxprobe       0 Feb 15 12:45 1975
srwxrwxrwx.  1 linuxprobe linuxprobe       0 Feb 15 19:28 1979
srwxrwxrwx.  1 linuxprobe linuxprobe       0 Feb 15 15:22 1981
srwxrwxrwx.  1 linuxprobe linuxprobe       0 Feb 15 12:29 2031
-rw-r--r--.  1 linuxprobe linuxprobe     696 Feb 15 12:29 6c80566e9d01473b8952f753857b506f-card-database.tdb
-rw-r--r--.  1 linuxprobe linuxprobe      10 Feb 15 19:28 6c80566e9d01473b8952f753857b506f-default-sink
----------------------省略部分输出内容--------------------------

 

 

posted @ 2020-12-23 15:21  AugustusS  阅读(224)  评论(0编辑  收藏  举报