实用命令汇总
# cat
点击查看
[root@summer ~]# cat 123.sh
123
[root@summer ~]# cat 456.sh
456
[root@summer ~]# cat 123.sh 456.sh //同时显示文件123.sh和456.sh的内容
123
456
[root@summer ~]# cat 123.sh 456.sh > hebing.sh //将文件123.sh和456.sh合并后放入文件hebing.sh中
[root@summer ~]# cat hebing.sh
123
456
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
[root@summer ~]# cat -b test.sh //显示文件内容要求不对空行编号
1 123
2 456
3 789 2131 213123
[root@summer ~]# cat -n test.sh //需要显示文件内容的行数
1 123
2
3
4 456
5
6
7 789 2131 213123
8
[root@summer ~]# cat -s test.sh //当遇到有连续两行以上的空白行,就代换为一行的空白行
123
456
789 2131 213123
[root@summer ~]# cat -ns test.sh //常用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
cat log.txt | grep 'ERROR' -A 5
意思是,在log.txt文件中,查找ERROR字符,并显示ERROR所在行的之后5行
cat log.txt | grep 'ERROR' -B 5 之前5行
cat log.txt | grep 'ERROR' -C 5 前后5行
cat log.txt | grep -v 'ERROR' 排除ERROR所在的行
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# pidof
点击查看
[root@summer ~]# ps -ef |grep nginx
root 6618 6588 0 17:34 pts/1 00:00:00 grep --color=auto nginx
root 31022 1 0 Nov24 ? 00:00:00 nginx: master process ./nginx
root 31388 31022 0 Nov24 ? 00:00:00 nginx: worker process
[root@summer ~]# pidof nginx
31388 31022
[root@summer ~]# kill -9 $(pidof nginx)
1
2
3
4
5
6
7
2
3
4
5
6
7
# seq
点击查看
[root@summer include]# seq -s '#' 5 //指定分隔符横着输出
1#2#3#4#5
[root@summer include]# seq -s ' ' 10 //以空格作为分格,且输出单数
1 2 3 4 5 6 7 8 9 10
[root@summer include]# seq -w 1 10 //默认补位操作
01
02
03
04
05
06
07
08
09
10
[root@summer include]# seq -2 2 10 //产生-2~10内的整数,增量为2
-2
0
2
4
6
8
10
[root@summer include]# seq -f "%03g" 98 101 //产生98~101之间的整数,并且要求输出数字宽度相同,%3g 表示宽度为3,不足用0补足
098
099
100
101
[root@summer include]# seq -f 'dir%03g' 1 5 //% 前面还可以指定字符串,一次性创建5个名为dir001 , dir002 .. dir005 的目录
dir001
dir002
dir003
dir004
dir005
[root@summer]# ls -l |grep dir
drwxr-xr-x 2 root root 6 Dec 10 17:06 dir001
drwxr-xr-x 2 root root 6 Dec 10 17:06 dir002
drwxr-xr-x 2 root root 6 Dec 10 17:06 dir003
drwxr-xr-x 2 root root 6 Dec 10 17:06 dir004
drwxr-xr-x 2 root root 6 Dec 10 17:06 dir005
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# tail
点击查看
## tail file (不加任何参数 显示文件file的最后10行)
[root@summer config]# tail -n 5 elasticsearch.yml //查看 elasticsearch.yml 文件末尾 5 行的数据内容
#
#
path.repo: ["/home/summer/elasticsearch-6.3.1/es_backup"]
http.cors.enabled: true
http.cors.allow-origin: "*"
[root@summer config]# tail -5 elasticsearch.yml //查看 elasticsearch.yml 文件末尾 5 行的数据内容
#
#
path.repo: ["/home/summer/elasticsearch-6.3.1/es_backup"]
http.cors.enabled: true
http.cors.allow-origin: "*"
[root@summer config]# tail -c 100 elasticsearch.yml //查看 elasticsearch.yml 文件末尾 100 个字节的数据内容
me/summer/elasticsearch-6.3.1/es_backup"]
http.cors.enabled: true
http.cors.allow-origin: "*"
[root@summer config]#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# netstat
点击查看
[root@summer]# netstat -anp | grep 端口号 //LINUX中如何查看某个端口是否被占用
[root@summer]# netstat -anp | grep 23306
tcp6 0 0 :::23306 :::* LISTEN 1853/mysqld
[root@summer]# netstat -anp | grep 2330
tcp6 0 0 :::23306 :::* LISTEN 1853/mysqld
[root@summer]# netstat -anp | grep 123
[root@summer]# netstat -nultp //查看当前所有已经使用的端口情况
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 23077/nginx: master
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 23077/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 999/sshd
tcp 0 0 0.0.0.0:888 0.0.0.0:* LISTEN 23077/nginx: master
tcp 0 0 0.0.0.0:8888 0.0.0.0:* LISTEN 1207/python
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1180/master
tcp6 0 0 :::23306 :::* LISTEN 1853/mysqld
tcp6 0 0 :::22 :::* LISTEN 999/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1180/master
[root@summer]#
[root@summer]# netstat -a | more //列出所有端口
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:zabbix-agent 0.0.0.0:* LISTEN
tcp 0 0 localhost:cslistener 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:ssh 0.0.0.0:* LISTEN
tcp 0 0 localhost:smtp 0.0.0.0:* LISTEN
tcp 0 52 localhost.localdoma:ssh 192.168.0.112:56257 ESTABLISHED
tcp6 0 0 [::]:zabbix-agent [::]:* LISTEN
tcp6 0 0 [::]:http [::]:* LISTEN
tcp6 0 0 [::]:ssh [::]:* LISTEN
tcp6 0 0 localhost:smtp [::]:* LISTEN
raw6 0 0 [::]:ipv6-icmp [::]:* 7
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags Type State I-Node Path
unix 3 [ ] DGRAM 8710 /run/systemd/notify
unix 2 [ ] DGRAM 8712 /run/systemd/cgroups-agent
unix 2 [ ACC ] STREAM LISTENING 13330 /run/systemd/private
[root@summer]# netstat -at //列出所有 tcp 端口
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:zabbix-agent 0.0.0.0:* LISTEN
tcp 0 0 localhost:cslistener 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:ssh 0.0.0.0:* LISTEN
tcp 0 0 localhost:smtp 0.0.0.0:* LISTEN
tcp 0 52 localhost.localdoma:ssh 192.168.0.112:56257 ESTABLISHED
tcp6 0 0 [::]:zabbix-agent [::]:* LISTEN
tcp6 0 0 [::]:http [::]:* LISTEN
tcp6 0 0 [::]:ssh [::]:* LISTEN
tcp6 0 0 localhost:smtp [::]:* LISTEN
[root@summer]# netstat -au //列出所有 udp 端口
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
[root@summer]#
[root@summer]# netstat -l //只显示监听端口
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:zabbix-agent 0.0.0.0:* LISTEN
tcp 0 0 localhost:cslistener 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:ssh 0.0.0.0:* LISTEN
tcp 0 0 localhost:smtp 0.0.0.0:* LISTEN
tcp6 0 0 [::]:zabbix-agent [::]:* LISTEN
tcp6 0 0 [::]:http [::]:* LISTEN
tcp6 0 0 [::]:ssh [::]:* LISTEN
[root@summer]# netstat -lt //只列出所有监听 tcp 端口
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:zabbix-agent 0.0.0.0:* LISTEN
tcp 0 0 localhost:cslistener 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:ssh 0.0.0.0:* LISTEN
tcp 0 0 localhost:smtp 0.0.0.0:* LISTEN
tcp6 0 0 [::]:zabbix-agent [::]:* LISTEN
tcp6 0 0 [::]:http [::]:* LISTEN
tcp6 0 0 [::]:ssh [::]:* LISTEN
tcp6 0 0 localhost:smtp [::]:* LISTEN
[root@summer]# netstat -lu //只列出所有监听 udp 端口
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
[root@summer]# netstat -lx //只列出所有监听 UNIX 端口
Active UNIX domain sockets (only servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 13330 /run/systemd/private
unix 2 [ ACC ] STREAM LISTENING 8730 /run/systemd/journal/stdout
unix 2 [ ACC ] STREAM LISTENING 17182 /var/run/vmware/guestServicePipe
unix 2 [ ACC ] STREAM LISTENING 13346 /run/lvm/lvmpolld.socket
unix 2 [ ACC ] SEQPACKET LISTENING 13357 /run/udev/control
unix 2 [ ACC ] STREAM LISTENING 20585 private/scache
unix 2 [ ACC ] STREAM LISTENING 13367 /run/lvm/lvmetad.socket
[root@summer]# netstat -s //显示所有端口的统计信息
Ip:
58416 total packets received
0 forwarded
0 incoming packets discarded
58231 incoming packets delivered
31712 requests sent out
366 reassemblies required
183 packets reassembled ok
Icmp:
14 ICMP messages received
2 input ICMP message failed.
ICMP input histogram:
destination unreachable: 6
echo requests: 8
42 ICMP messages sent
0 ICMP messages failed
ICMP output histogram:
destination unreachable: 34
echo replies: 8
IcmpMsg:
InType3: 6
InType8: 8
OutType0: 8
OutType3: 34
## 显示 TCP 或 UDP 端口的统计信息 netstat -st 或 -su
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# find
点击查看
[root@summer]# find . -type f -size +10M //查找一个文件大小超过10M的文件
./server/panel/pyenv/bin/python3.7
./server/panel/pyenv/bin/python3.7m
./server/panel/pyenv/lib/python3.7/config-3.7m-x86_64-linux-gnu/libpython3.7m.a
./server/panel/pyenv/lib/libpython3.7m.a
[root@summer]# find . -mmin -5 //# 查找在最后5分钟里修改过的文件
.
./push.sh
[root@summer]# find . -name '*.txt' -type f -mmin -30 //查找当前文件夹下30分钟内修改过的txt文件
[root@summer]# find . -type f -mtime +4 //查找当前文件夹下5天内修改过的文件
[root@summer]# find -type d | grep .svn$ //查找目录名.svn
./web/static/GWWZ/home/.svn
./web/static/GWWZ/loginbg/.svn
./web/static/GWWZ/menuicon/.svn
./web/static/GWWZ/submenuleft/.svn
./web/static/GWWZ/.svn
./web/static/GWWZ/appbutton/.svn
./web/static/GWWZ/appstore/.svn
./web/static/GWWZ/dark/appbutton/.svn
[root@summer]# find -type d | grep .svn$ | xargs rm -r //查找并删除
[root@summer]# find -type f -size 0 -exec ls -l {} \; //查找当前路径下所有文件长度为 0 的普通文件,并列出它们的完整路径
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# watch
点击查看
该命令最为常用的两个选项是-d和-n,其中-n表示间隔多少秒执行一次"command",-d表示高亮发生变化的位置。
[root@summer]# watch -d -n 1 'df -h; ls -l'
Every 1.0s: df -h; ls -l Mon Apr 12 14:35:09 2021
Filesystem Size Used Avail Use% Mounted on
devtmpfs 63G 0 63G 0% /dev
tmpfs 63G 20K 63G 1% /dev/shm
tmpfs 63G 9.4M 63G 1% /run
tmpfs 63G 0 63G 0% /sys/fs/cgroup
/dev/mapper/os-root 880G 47G 789G 6% /
/dev/sda1 976M 328M 582M 37% /boot
tmpfs 13G 0 13G 0% /run/user/0
total 992
-rwxrwxrwx 1 root root 8556 Apr 12 14:20 config.html
-rwxrwxrwx 1 root root 212 Apr 12 14:20 errorlog.html
-rwxrwxrwx 1 root root 300242 Apr 12 14:35 flatfile.html
-rwxrwxrwx 1 root root 163 Apr 12 14:20 histogram.html
-rwxrwxrwx 1 root root 98572 Apr 12 14:34 localhost-0.html
-rwxrwxrwx 1 root root 7248 Apr 12 14:32 localhost-0.stdout.html
-rwxrwxrwx 1 root root 98705 Apr 12 14:34 localhost.html
-rwxrwxrwx 1 root root 181 Apr 12 14:20 localhost.var_adm_msgs.html
-rwxrwxrwx 1 root root 116757 Apr 12 14:35 logfile.html
-rwxrwxrwx 1 root root 705 Apr 12 14:20 parmfile.html
-rwxrwxrwx 1 root root 1529 Apr 12 14:20 parmscan.html
-rwxrwxrwx 1 root root 164 Apr 12 14:20 sd1.histogram.html
-rwxrwxrwx 1 root root 98527 Apr 12 14:34 sd1.html
-rwxrwxrwx 1 root root 420 Apr 12 14:20 skew.html
-rwxrwxrwx 1 root root 830 Apr 12 14:20 status.html
-rwxrwxrwx 1 root root 99386 Apr 12 14:34 summary.html
-rwxrwxrwx 1 root root 0 Apr 12 14:20 swat_mon_total.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# awk
点击查看
- 输出指定列
[root@summer]# ceph df | awk '{print $1,$3}' | grep 10240G
test 10240G
1
2
3
2
3
# cp
点击查看
Linux下默认cp命令是有别名(alias cp=‘cp -i’)的,无法强制覆盖,即使你用 -f 参数也无法强制覆盖文件
- 取消cp的alias,放心这不是永久生效
#unalias cp
#cp a /test/a
1
2
2
- 用 \cp 执行cp命令时不走alias (通常使用这种方法)
#\cp a /test/a
1
- 使用管道(相当于在问是否要覆盖时,给一个yes,这样就不会在让你输入yes or no了)
#yes|cp a /test/a
1
上次更新: 4/28/2021, 4:26:41 PM