文章目录

Linux常用命令

1、查看端口占用情况

netstat

netstat -lnp|grep 88 
netstat -tunlp|grep 80
netstat -lntup

2、压缩、解压

zip

常用参数:

-q 不显示指令执行过程
-r 递归处理,将指定目录下的所有文件和子目录一并处理
-z 替压缩文件加上注释
-v 显示指令执行过程或显示版本信息
-n<字尾字符串> 不压缩具有特定字尾字符串的文件
# 将 /home/html/ 这个目录下所有文件和文件夹打包为当前目录下的 html.zip:
zip -q -r html.zip /home/html
# 压缩文件 cp.zip 中删除文件 a.c:
zip -dv cp.zip a.c 
# 把/home目录下面的mydata目录压缩为mydata.zip:
zip -r mydata.zip mydata 
# 把/home目录下面的abc文件夹和123.txt压缩成为abc123.zip
zip -r abc123.zip abc 123.txt 
# 将 logs目录打包成 log.zip
zip -r log.zip ./logs

unzip

-l 显示压缩文件内所包含的文件
-v 执行时显示详细的信息
-c 将解压缩的结果显示到屏幕上,并对字符做适当的转换
-n 解压缩时不要覆盖原有的文件
-j 不处理压缩文件中原有的目录路径
-d 指定文件解压缩后所要存储的目录;
unzip test.zip                 ->将test.zip解压到当前文件下
unzip -n test.zip -d /tmp     ->将test.zip解压到/tmp目录下,并且不要覆盖已有文件
unzip -v test.zip            ->查看test.zip内容,但不解压
unzip -o test.zip -d tmp/    ->将test.zip解压到/tmp目录下,并且覆盖已有文件
# 把/home目录下面的abc12.zip、abc23.zip、abc34.zip同时解压到/home目录里面
unzip abc\*.zip

tar

# 将所有.jpg的文件打成一个名为all.tar的包
tar -cf all.tar *.jpg
# 打包文件以后,以 gzip 压缩
tar -zcvf log.tar.gz linuxcool.log
[root@linux ~]# tar -cvf /tmp/etc.tar /etc <==仅打包,不压缩!
[root@linux ~]# tar -zcvf /tmp/etc.tar.gz /etc <==打包后,以 gzip 压缩
[root@linux ~]# tar -jcvf /tmp/etc.tar.bz2 /etc <==打包后,以 bzip2 压缩
# 
tar -zxvf a_dir.tar.gz   # 解包

3、查看磁盘使用率

df

[root@VM-12-5-centos ~]# df
Filesystem     1K-blocks     Used Available Use% Mounted on
devtmpfs          929388        0    929388   0% /dev
tmpfs             941004       24    940980   1% /dev/shm
tmpfs             941004     1008    939996   1% /run
tmpfs             941004        0    941004   0% /sys/fs/cgroup
/dev/vda1       61795096 30497232  28681200  52% /
tmpfs             188204        0    188204   0% /run/user/0
overlay         61795096 30497232  28681200  52% /var/lib/docker/overlay2/868ce13376bdc9345366c2903c0b9089307c57f9effc4718daeb6c3c53fd9c5c/merged
overlay         61795096 30497232  28681200  52% /var/lib/docker/overlay2/ce781124ae7fa9bc3e333e7f30707f14771733c5a04ef01956a569658b48fc43/merged
overlay         61795096 30497232  28681200  52% /var/lib/docker/overlay2/a8a51105596a7993f82ea38c2169c4cbda8337fdab45e389b6adf025d1a36b77/merged
overlay         61795096 30497232  28681200  52% /var/lib/docker/overlay2/f5b2b7e5214b1672666a859680d25bff30df81d76598f4e36b207a35b3f1c024/merged
overlay         61795096 30497232  28681200  52% /var/lib/docker/overlay2/456574599dad1743508b2e3e4a49d154c0333af2efa743b5e9ae5d1ad4c61026/merged

4、统计目录下文件及文件夹的个数

#统计当前目录下文件的个数(不包括目录)
ls -l | grep "^-" | wc -l
#统计当前目录下文件的个数(包括子目录)
ls -lR| grep "^-" | wc -l
#查看某目录下文件夹(目录)的个数(包括子目录)
ls -lR | grep "^d" | wc -l

5、查看网络使用情况

iftop、nethogs 命令

安装命令:

yum install iftop -y

yum install libpcap nethogs

iftop

                          1.91Mb                     3.81Mb                     5.72Mb                     7.63Mb               9.54Mb
+-------------------------+--------------------------+--------------------------+--------------------------+--------------------------
VM-12-5-centos                                        => 120.24.137.129                                        1.97Kb  6.26Kb  5.22Kb
                                                      <=                                                        980b   85.8Kb  71.5Kb
VM-12-5-centos                                        => 210.51.43.126                                            0b   10.1Kb  8.46Kb
                                                      <=                                                          0b   1.11Kb   949b
VM-12-5-centos                                        => 169.254.0.55                                             0b   5.96Kb  4.97Kb
                                                      <=                                                        160b    351b    293b
VM-12-5-centos                                        => 113.204.228.2                                         3.64Kb  4.68Kb  4.79Kb
                                                      <=                                                        208b    282b    471b
VM-12-5-centos                                        => 169.254.0.4                                              0b   3.24Kb  2.70Kb
                                                      <=                                                          0b   1.46Kb  1.22Kb
VM-12-5-centos                                        => 183.60.83.19                                             0b    918b    765b
                                                      <=                                                          0b   1.58Kb  1.32Kb
VM-12-5-centos                                        => 180.76.107.245                                           0b   1.00Kb   854b
                                                      <=                                                          0b    230b    192b
VM-12-5-centos                                        => 122.51.213.127                                           0b    205b    320b
                                                      <=                                                          0b    179b    331b
VM-12-5-centos                                        => 220-137-160-98.dynamic-ip.hinet.net                      0b    250b    208b
                                                      <=                                                          0b     96b     80b
VM-12-5-centos                                        => 106.52.187.113                                           0b    161b    134b
                                                      <=                                                          0b    161b    134b
VM-12-5-centos                                        => 183.60.82.98                                             0b    115b     96b
                                                      <=                                                          0b    115b     96b
VM-12-5-centos                                        => 169.254.128.6                                          112b     67b     75b
                                                      <=                                                        112b     67b     75b
VM-12-5-centos                                        => 169.254.128.8                                            0b     67b     56b
                                                      <=                                                          0b     67b     56b
VM-12-5-centos                                        => 169.254.0.138                                            0b     32b     27b
                                                      <=                                                          0b     32b     27b


--------------------------------------------------------------------------------------------------------------------------------------
TX:             cum:   42.9KB   peak:    114Kb                                                        rates:   5.72Kb  33.1Kb  28.6Kb
RX:                     115KB            439Kb                                                                 1.43Kb  91.5Kb  76.7Kb
TOTAL:                  158KB            553Kb                                                                 7.15Kb   125Kb   105Kb

nethogs

NetHogs version 0.8.5

    PID USER     PROGRAM                                                                          DEV        SENT      RECEIVED       
    549 nginx    nginx: worker process                                                            eth0        1.523       0.428 KB/sec
  28214 root     java                                                                             eth0        0.317       0.274 KB/sec
      ? root     10.0.12.5:53738-169.254.0.4:80                                                               0.188       0.108 KB/sec
      ? root     10.0.12.5:53734-169.254.0.4:80                                                               0.345       0.108 KB/sec
      ? root     10.0.12.5:53732-169.254.0.4:80                                                               0.140       0.108 KB/sec
      ? root     10.0.12.5:53728-169.254.0.4:80                                                               0.194       0.108 KB/sec
      ? root     10.0.12.5:53726-169.254.0.4:80                                                               0.339       0.108 KB/sec
      ? root     10.0.12.5:44340-169.254.0.55:8080                                                            0.162       0.087 KB/sec
      ? root     172.17.0.2:6379-106.52.187.113:50904                                                         0.000       0.068 KB/sec
      ? root     10.0.12.5:8083-74.120.14.116:51182                                                           0.057       0.057 KB/sec
  22291 root     sshd: root@pts/0                                                                 eth0        0.557       0.052 KB/sec
      ? root     10.0.12.5:6379-106.52.187.113:50904                                                          0.014       0.034 KB/sec
      ? root     106.52.187.113:50904-172.17.0.2:6379                                                         0.000       0.027 KB/sec
  12978 root     /usr/local/qcloud/YunJing/YDEyes/YDService                                       eth0        0.061       0.011 KB/sec
      ? root     10.0.12.5:53722-169.254.0.4:80                                                               0.000       0.000 KB/sec
      ? root     10.0.12.5:53720-169.254.0.4:80                                                               0.000       0.000 KB/sec
      ? root     10.0.12.5:53718-169.254.0.4:80                                                               0.000       0.000 KB/sec
      ? root     10.0.12.5:53714-169.254.0.4:80                                                               0.000       0.000 KB/sec
      ? root     10.0.12.5:53712-169.254.0.4:80                                                               0.000       0.000 KB/sec
      ? root     10.0.12.5:53710-169.254.0.4:80                                                               0.000       0.000 KB/sec
      ? root     10.0.12.5:53708-169.254.0.4:80                                                               0.000       0.000 KB/sec
      ? root     10.0.12.5:53706-169.254.0.4:80                                                               0.000       0.000 KB/sec
      ? root     10.0.12.5:53704-169.254.0.4:80                                                               0.000       0.000 KB/sec
      ? root     10.0.12.5:53702-169.254.0.4:80                                                               0.000       0.000 KB/sec
      ? root     10.0.12.5:53700-169.254.0.4:80                                                               0.000       0.000 KB/sec
      ? root     10.0.12.5:3389-80.66.76.36:10935                                                             0.000       0.000 KB/sec
      ? root     10.0.12.5:80-101.89.43.238:47032                                                             0.000       0.000 KB/sec
      ? root     10.0.12.5:53698-169.254.0.4:80                                                               0.000       0.000 KB/sec
      ? root     10.0.12.5:53696-169.254.0.4:80                                                               0.000       0.000 KB/sec
      ? root     10.0.12.5:53694-169.254.0.4:80                                                               0.000       0.000 KB/sec
      ? root     10.0.12.5:53692-169.254.0.4:80                                                               0.000       0.000 KB/sec
      ? root     10.0.12.5:53690-169.254.0.4:80                                                               0.000       0.000 KB/sec
  22396 root     /usr/local/qcloud/tat_agent/tat_agent                                            eth0        0.000       0.000 KB/sec
      ? root     10.0.12.5:53688-169.254.0.4:80                                                               0.000       0.000 KB/sec
      ? root     unknown TCP                                                                                  0.000       0.000 KB/sec