一.用> 把输出转向*
例: ifconfig > 2.txt 把ifconfig命令运行结果保存到2.txt文件中。
例: ls >> 2.txt 把输出附到文件后面
说明:>是把输出转向到指定文件,文件存在会重新写入原内容不会保留 >>是把输出附到文件后面原文件会保留。 | |
---|---|
二、tee 输出信息同时把信息记录到文件
*参数:-a 附加到给出文件而不是覆盖它。*
例1: ls | tee 2.txt 在终端上显示ls执行结果并将结果保存到2.txt文件中
[root@localhost /]# ls | tee 2.txt
1
1.txt
2.txt
bin
boot
corefiles
dev
etc
home
lianxi_con
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
u01
usr
var
[root@localhost /]# cat 2.txt //查看2.txt文件内容与上边执行结果一致
1
1.txt
2.txt
bin
boot
corefiles
dev
etc
home
lianxi_con
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
u01
usr
var
加参数例: ifconfig | tee -a 2.txt 保留2.txt原内容并把ifconfig执行结果添加到后面
[root@localhost /]# cat 2.txt
1
1.txt
2.txt
bin
boot
corefiles
dev
etc
home
lianxi_con
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
u01
usr
var
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.25.130 netmask 255.255.255.0 broadcast 192.168.25.255
inet6 fe80::58db:2a2a:8567:f390 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:d0:91:b3 txqueuelen 1000 (Ethernet)
RX packets 25360 bytes 34722481 (33.1 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4802 bytes 453597 (442.9 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 1 (Local Loopback)
RX packets 92 bytes 7580 (7.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 92 bytes 7580 (7.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
三、 script 可记录多个命令的输出
参数: -a 指定文件名
说明:启动script时若没有指定文件名,它会自动生成到当前目录下一个名为typescrip文件里。也可用-a参数指定。 | |
---|---|
例1: #
script
Script started, file is typescript
[root@localhost /]# ls
1 boot home lost+found proc srv u01
1.txt corefiles lianxi_con media root sys usr
2.txt dev lib mnt run tmp typescript
[root@localhost /]# exit
exit
Script done, file is typescript
参数: -a 例:将cpu、内存、硬盘、网口配置、ssh端口状态输出到2.txt文件中。
```cpp
[root@localhost /]# script -a 2.txt
Script started, file is 2.txt
[root@localhost /]# top
top - 13:14:35 up 9:54, 5 users, load average: 0.10, 0.05, 0.13
Tasks: 213 total, 1 running, 212 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.9 us, 0.7 sy, 0.0 ni, 98.4 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 4030416 total, 209572 free, 1142092 used, 2678752 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 2336948 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
6738 liveuser 20 0 2033968 305572 48284 S 4.0 7.6 21:44.20 gnome-shell
[root@localhost /]# netstat -anp |grep sshd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 11747/sshd
tcp6 0 0 :::22 :::* LISTEN 11747/sshd
[root@localhost /]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.25.130 netmask 255.255.255.0 broadcast 192.168.25.255
inet6 fe80::58db:2a2a:8567:f390 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:d0:91:b3 txqueuelen 1000 (Ethernet)
RX packets 25790 bytes 34771681 (33.1 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5169 bytes 561875 (548.7 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 1 (Local Loopback)
RX packets 92 bytes 7580 (7.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 92 bytes 7580 (7.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost /]# df -hl
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/live-rw 7.8G 5.6G 2.2G 73% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 2.0G 480K 2.0G 1% /dev/shm
tmpfs 2.0G 17M 2.0G 1% /run
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/sr0 2.0G 2.0G 0 100% /run/initramfs/live
tmpfs 2.0G 1.3M 2.0G 1% /tmp
varcacheyum 2.0G 218M 1.8G 12% /var/cache/yum
vartmp 2.0G 0 2.0G 0% /var/tmp
tmpfs 394M 32K 394M 1% /run/user/1001
tmpfs 394M 32K 394M 1% /run/user/0
[root@localhost /]# exit
exit
Script done, file is 2.txt
[root@localhost /]# cat 2.txt
脚本启动于 2020年04月21日 星期二 13时14分26秒
[root@localhost /]# top
top - 13:14:35 up 9:54, 5 users, load average: 0.10, 0.05, 0.13
Tasks: 213 total, 1 running, 212 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.9 us, 0.7 sy, 0.0 ni, 98.4 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 4030416 total, 209572 free, 1142092 used, 2678752 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 2336948 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
6738 liveuser 20 0 2033968 305572 48284 S 4.0 7.6 21:44.20 gnome-shell
[root@localhost /]# netstat -anp |grep sshd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 11747/sshd
tcp6 0 0 :::22 :::* LISTEN 11747/sshd
[root@localhost /]# netstat -anp |grep sshd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 11747/sshd
tcp6 0 0 :::22 :::* LISTEN 11747/sshd
[root@localhost /]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.25.130 netmask 255.255.255.0 broadcast 192.168.25.255
inet6 fe80::58db:2a2a:8567:f390 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:d0:91:b3 txqueuelen 1000 (Ethernet)
RX packets 25790 bytes 34771681 (33.1 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5169 bytes 561875 (548.7 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 1 (Local Loopback)
RX packets 92 bytes 7580 (7.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 92 bytes 7580 (7.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost /]# df -hl
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/live-rw 7.8G 5.6G 2.2G 73% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 2.0G 480K 2.0G 1% /dev/shm
tmpfs 2.0G 17M 2.0G 1% /run
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/sr0 2.0G 2.0G 0 100% /run/initramfs/live
tmpfs 2.0G 1.3M 2.0G 1% /tmp
varcacheyum 2.0G 218M 1.8G 12% /var/cache/yum
vartmp 2.0G 0 2.0G 0% /var/tmp
tmpfs 394M 32K 394M 1% /run/user/1001
tmpfs 394M 32K 394M 1% /run/user/0
[root@localhost /]# exit
exit
Script done on 2020年04月21日 星期二 13时15分52秒