Linux常用命令(ping、kill、seq、du、df、free、date、tar、read)

本章命令:

1 2 3 4 5 6 7 8 9
ping kill seq du df free date tar read

1、" ping "

作用:向网络主机发送ICMP(检测主机是否在线)

╭─root@localhost.localdomain ~  
╰─➤  ping -c 2 -w 5 -i 0.1 baidu.com   #-c 发送包的数量 ;-w  等待时间 ;-i  <间隔秒数>几秒发一个包;
PING baidu.com (123.125.114.144) 56(84) bytes of data.
64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=1 ttl=128 time=25.9 ms
64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=2 ttl=128 time=49.9 ms

--- baidu.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 100ms
rtt min/avg/max/mdev = 25.991/37.979/49.968/11.990 ms
  

2、" kill "

作用:终止进程

kill  PID
kill -9        #强制终止     
kill -15      #kill  默认kill -15    
kill -l         #打印信号名列表

3、" seq "

作用:生成数列

╭─root@localhost.localdomain ~  
╰─➤  seq 4           #指定结束位置
1
2
3
4
╭─root@localhost.localdomain ~  
╰─➤  seq 3 7       #指定起始和结束位置
3
4
5
6
7
╭─root@localhost.localdomain ~  
╰─➤  seq 3 3 14           #指定起始位置结束位置和步长
3
6
9
12


4、" du "

作用:输出文件及目录大小(文本占用磁盘块的大小 默认最小4k单位)

-s或--summarize 仅显示总计

常用选项:

╭─root@localhost.localdomain ~/2  
╰─➤  du -s /root          #仅显示总和
9320	/root
╭─root@localhost.localdomain ~/2  
╰─➤  du -sh /root
9.2M	/root                #人类易懂显示总和

du 目录    #会默认显示一个目录下的所有的文件,最后一行会有大小的总和(可自行尝试);

5、" df "

作用:报告文件系统磁盘空间的使用情况

╭─root@localhost.localdomain ~/2  
╰─➤  df -h                  #-h 人类易懂的输出
文件系统             容量  已用  可用 已用% 挂载点
/dev/mapper/cl-root   17G  4.7G   13G   28% /
devtmpfs             473M     0  473M    0% /dev
tmpfs                489M  144K  489M    1% /dev/shm
tmpfs                489M  7.1M  482M    2% /run
tmpfs                489M     0  489M    0% /sys/fs/cgroup
/dev/sda1           1014M  173M  842M   18% /boot
tmpfs                 98M  8.0K   98M    1% /run/user/0
/dev/sr0             4.1G  4.1G     0  100% /run/media/root/CentOS 7 x86_64
  • 第一列指定文件系统的名称
  • 第二列指定一个特定的文件系统1K-块1K是1024字节为单位的总内存。用和可用列正在使用中,分别指定的内存量。
  • 第三列使用列指定使用的内存的百分比
  • 而最后一列"安装在"指定的文件系统的挂载点。

6、" free "

作用:显示系统中已用和未用的内存空间总和

常用选项:

╭─root@localhost.localdomain ~/2  
╰─➤  free -m         #以m为单位
              total        used        free      shared  buff/cache   available
Mem:            976         506          62           8         407         253
Swap:          2047           0        2047
╭─root@localhost.localdomain ~/2  
╰─➤  free -h         #人类易读 (human)
              total        used        free      shared  buff/cache   available
Mem:           976M        504M         65M        8.5M        407M        256M
Swap:          2.0G          0B        2.0G
╭─root@localhost.localdomain ~/2  
╰─➤  free -s 3         #-s <间隔秒数>  持续观察内存使用状况。
              total        used        free      shared  buff/cache   available
Mem:         999936      516164       66896        8704      416876      262392
Swap:       2097148           0     2097148

              total        used        free      shared  buff/cache   available
Mem:         999936      516164       66896        8704      416876      262392
Swap:       2097148           0     2097148

              total        used        free      shared  buff/cache   available
Mem:         999936      516164       66896        8704      416876      262392
Swap:       2097148           0     2097148

^C                          #Ctrl + c  停止


7、" date "

作用: 打印或设置系统日期和时间

常用选项:

-s: 根据 STRING 设置时间

%Y     年(用 4 位表示) (1970...)
%m     月份(用两位表示) (01..12)
%d     (月份中的)几号(用两位表示) (01..31)
%H     小时(按 24 小时制显示,用两位表示) (00..23)
%M     分钟数(用两位表示) (00..59)
%S     秒数(用两位表示)(00..60)

%T 时间,按 24 小时制显示(hh:mm:ss)

%F 显示 %Y-%m-%d

举例

实例1:

[root@fda ~]# date '+%F %T'
2019-01-13 13:29:56

实例2:

[root@fda ~]# date -s "2019-01-13 13:30:00"
Sun Jan 13 13:30:00 CST 2019
[root@fda ~]# date
Sun Jan 13 13:30:01 CST 2019

8、tar

常用组合:

  • 创建压缩归档:czvf
    v:显示过程细节
    z:有gzip属性的
  • 解压:xzvf (xf)
  • 查看tar包里的内容:tf
╭─root@localhost.localdomain ~/test  
╰─➤  touch file{1..35}
╭─root@localhost.localdomain ~/test  
╰─➤  ls
file1   file13  file17  file20  file24  file28  file31  file35  file7
file10  file14  file18  file21  file25  file29  file32  file4   file8
file11  file15  file19  file22  file26  file3   file33  file5   file9
file12  file16  file2   file23  file27  file30  file34  file6
╭─root@localhost.localdomain ~/test  
╰─➤  tar -cvzf file.tar.zg ./*
./file1
./file10
./file11
./file12
./file13
./file14
./file15
./file16
./file17
./file18
./file19
./file2
./file20
./file21
./file22
./file23
./file24
./file25
./file26
./file27
./file28
./file29
./file3
./file30
./file31
./file32
./file33
./file34
./file35
./file4
./file5
./file6
./file7
./file8
./file9
╭─root@localhost.localdomain ~/test  
╰─➤  ls
file1   file13  file17  file20  file24  file28  file31  file35  file7
file10  file14  file18  file21  file25  file29  file32  file4   file8
file11  file15  file19  file22  file26  file3   file33  file5   file9
file12  file16  file2   file23  file27  file30  file34  file6   file.tar.zg
╭─root@localhost.localdomain ~/test  
╰─➤  rm file{1..35}
╭─root@localhost.localdomain ~/test  
╰─➤  ls
file.tar.zg
╭─root@localhost.localdomain ~/test  
╰─➤  tar -xzvf file.tar.zg ./
./file1
./file10
./file11
./file12
./file13
./file14
./file15
./file16
./file17
./file18
./file19
./file2
./file20
./file21
./file22
./file23
./file24
./file25
./file26
./file27
./file28
./file29
./file3
./file30
./file31
./file32
./file33
./file34
./file35
./file4
./file5
./file6
./file7
./file8
./file9
╭─root@localhost.localdomain ~/test  
╰─➤  ls
file1   file13  file17  file20  file24  file28  file31  file35  file7
file10  file14  file18  file21  file25  file29  file32  file4   file8
file11  file15  file19  file22  file26  file3   file33  file5   file9
file12  file16  file2   file23  file27  file30  file34  file6   file.tar.zg

╭─root@localhost.localdomain ~/test  
╰─➤#tar -tf httpd.tar
httpd/
httpd/apr-1.4.8-3.el7.x86_64.rpm
httpd/apr-util-1.5.2-6.el7.x86_64.rpm
httpd/httpd-2.4.6-40.el7.centos.x86_64.rpm
httpd/httpd-tools-2.4.6-40.el7.centos.x86_64.rpm
httpd/._httpd.conf
httpd/httpd.conf
httpd/._index.cgi
httpd/index.cgi
httpd/._index.html
httpd/index.html
httpd/install.sh
httpd/mailcap-2.1.41-2.el7.noarch.rpm
httpd/mod_ssl-2.4.6-40.el7.centos.x86_64.rpm


#打时间包
tar czf  file_$(date +%Y%m%d).tar.gz  /home/*

9、read

语法

#语法:
read [选项] [名称/变量]

#选项:
read [-ers] [-a 数组] [-d 分隔符] [-i 缓冲区文字] [-n 读取字符数] [-N 读取字符数] [-p 提示符] [-t 超时] [-u 文件描述符] [名称 ...]

-p 显示提示信息
-t 设置读入数据的超时时间
-n 设置读取n个字符后结束,而默认会读取标准输入的一整行内容
-r 支持读取\,而默认read命令理解\为特殊符号(转义字符) 
-s 静默模式,不显示标准输入的内容(Silent mode)

例子:-p

[root@kubeadm ~]# read -p "请输入名字" test
请输入名字root
[root@kubeadm ~]# echo $test
root

举例:一次性多个变量

[root@kubeadm ~]# read t1 t2 t3
1 2 3 4
[root@kubeadm ~]# echo $t1 $t2 $t3
1 2 3 4
[root@kubeadm ~]# echo $t1 $t2
1 2
[root@kubeadm ~]# echo $t3
3 4

例子:配合管道符,ssh免密

#下载sshpass
[root@kubeadm ~]# yum install -y sshpass
#生成密钥对
[root@kubeadm ~]# ssh-keygen
#编写待免密主机信息
[root@kubeadm ~]# vim host.txt
[root@kubeadm ~]# cat host.txt
root 192.168.75.10 root
root 192.168.75.11 root
root 192.168.75.12 root
#编写脚本
[root@kubeadm ~]# vim ssh_all.sh
[root@kubeadm ~]# cat ssh_all.sh
#!/bin/bash
cat host.txt |while read hostname ipaddr passwd
do
sshpass -p $passwd ssh-copy-id $ipaddr 
done
#优化sshd
[root@kubeadm ~]# vim /etc/ssh/ssh_config
[root@kubeadm ~]# cat /etc/ssh/ssh_config | grep -A2 "the end of"
#Add at the end of the file
        StrictHostKeyChecking no    #ssh未连接过的主机不输入yes进行确认,避免影响sshpass的执行
#执行
[root@kubeadm ~]# bash ssh_all.sh


posted @ 2019-05-06 21:02  du-z  阅读(1321)  评论(0编辑  收藏  举报