使用tee命令无法一次清空多个文件

时间:2018-09-11 15:53:30

标签: bash tee

我尝试了使用 tee 命令一次清空多个文件的三种方法,但是没有任何效果。怎么可能出问题了?

[root@APPSERVER11-S1 ~]# cat /dev/null | tee /etc/vinter/logs/*
[root@APPSERVER11-S1 ~]# ls -l /etc/vinter/logs/*
-rw-r--r-- 1 root root 9223905280 Sep 11 12:47 /etc/vinter/logs/rsp.appserver11-s1.apigoldnode-1.rsp.log
-rw-r--r-- 1 root root    9468202 Sep 11 12:47 /etc/vinter/logs/Services-APIGOLDNODE-1.log
-rw-r--r-- 1 root root          0 Sep 11 12:47 /etc/vinter/logs/Services-APIGOLDNODE-1.log.1
[root@APPSERVER11-S1 ~]#
[root@APPSERVER11-S1 ~]#
[root@APPSERVER11-S1 ~]# echo -n | tee /etc/vinter/logs/*
[root@APPSERVER11-S1 ~]# ls -l /etc/vinter/logs/*
-rw-r--r-- 1 root root 9224224768 Sep 11 12:48 /etc/vinter/logs/rsp.appserver11-s1.apigoldnode-1.rsp.log
-rw-r--r-- 1 root root    9493203 Sep 11 12:48 /etc/vinter/logs/Services-APIGOLDNODE-1.log
-rw-r--r-- 1 root root          0 Sep 11 12:47 /etc/vinter/logs/Services-APIGOLDNODE-1.log.1
[root@APPSERVER11-S1 ~]#
[root@APPSERVER11-S1 ~]#
[root@APPSERVER11-S1 ~]# printf '' | tee /etc/vinter/logs/*
[root@APPSERVER11-S1 ~]# ls -l /etc/vinter/logs/*
-rw-r--r-- 1 root root 9224380416 Sep 11 12:48 /etc/vinter/logs/rsp.appserver11-s1.apigoldnode-1.rsp.log
-rw-r--r-- 1 root root    9502868 Sep 11 12:48 /etc/vinter/logs/Services-APIGOLDNODE-1.log
-rw-r--r-- 1 root root          0 Sep 11 12:48 /etc/vinter/logs/Services-APIGOLDNODE-1.log.1

它不能单独工作:

[root@APPSERVER11-S1 ~]# echo > /etc/vinter/logs/rsp.appserver11-s1.apigoldnode-1.rsp.log
[root@APPSERVER11-S1 ~]# ls -l /etc/vinter/logs/*
-rw-r--r-- 1 root root 9236357120 Sep 11 12:58 /etc/vinter/logs/rsp.appserver11-s1.apigoldnode-1.rsp.log
-rw-r--r-- 1 root root     520807 Sep 11 12:58 /etc/vinter/logs/Services-APIGOLDNODE-1.log
-rw-r--r-- 1 root root   10264642 Sep 11 12:54 /etc/vinter/logs/Services-APIGOLDNODE-1.log.1

注释中的truncate命令无法正常工作:

[root@APPSERVER11-S1 ~]# truncate -s 0 /etc/vinter/logs/rsp.appserver11-s1.apigoldnode-1.rsp.log
[root@APPSERVER11-S1 ~]# ls -l /etc/vinter/logs/
total 8292
-rw-r--r-- 1 root root 9283125248 Sep 11 13:32 rsp.appserver11-s1.apigoldnode-1.rsp.log
-rw-r--r-- 1 root root    6026198 Sep 11 13:32 Services-APIGOLDNODE-1.log
-rw-r--r-- 1 root root          0 Sep 11 13:06 Services-APIGOLDNODE-1.log.1

tee版本是:

[root@APPSERVER11-S1 ~]# tee --version
tee (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Mike Parker, Richard M. Stallman, and David MacKenzie.

文件系统为xfs

[root@APPSERVER11-S1 ~]# df -Th
Filesystem                     Type      Size  Used Avail Use% Mounted on
devtmpfs                       devtmpfs  7.3G     0  7.3G   0% /dev
tmpfs                          tmpfs     7.3G     0  7.3G   0% /dev/shm
tmpfs                          tmpfs     7.3G  185M  7.1G   3% /run
tmpfs                          tmpfs     7.3G     0  7.3G   0% /sys/fs/cgroup
/dev/mapper/vg_main-lv_root    xfs        16G   14G  1.8G  89% /
/dev/mapper/vg_dados-lv_dados  xfs        20G   33M   20G   1% /datastorage
/dev/mapper/vg_dados-lv_docker btrfs      80G  128M   79G   1% /var/lib/docker
/dev/xvdb1                     xfs       497M  161M  337M  33% /boot
tmpfs                          tmpfs     1.5G     0  1.5G   0% /run/user/0
tmpfs                          tmpfs     1.5G     0  1.5G   0% /run/user/1002

我在另一台服务器上尝试过,只有最新更新的文件(每秒很多行)没有被清零。可能是文件被如此频繁地更新甚至无法归零吗?

[root@APPSERVER21-S1 ~]# ls -l /etc/vinter/logs/
total 2046352
-rw-r--r-- 1 root root     3920496 Sep 11 14:35 PlusoftCRMIntegration-APISILVERNODE-1.log
-rw-r--r-- 1 root root 30891423129 Sep 11 15:27 rsp.appserver21-s1.apigoldnode-1.api-oi-gold.log
-rw-r--r-- 1 root root   406151168 Sep 11 15:27 rsp.appserver21-s1.apisilvernode-1.api-oi-silver.log
[root@APPSERVER21-S1 ~]# cat /dev/null | tee /etc/vinter/logs/*
[root@APPSERVER21-S1 ~]# ls -l /etc/vinter/logs/
total 492
-rw-r--r-- 1 root root           0 Sep 11 15:28 PlusoftCRMIntegration-APISILVERNODE-1.log
-rw-r--r-- 1 root root 30892328752 Sep 11 15:28 rsp.appserver21-s1.apigoldnode-1.api-oi-gold.log
-rw-r--r-- 1 root root           0 Sep 11 15:28 rsp.appserver21-s1.apisilvernode-1.api-oi-silver.log

第二台服务器的/分区文件系统是xfs,与第一台服务器一样。

[root@APPSERVER21-S1 ~]# df -Th
Filesystem                     Type      Size  Used Avail Use% Mounted on
devtmpfs                       devtmpfs  3.6G     0  3.6G   0% /dev
tmpfs                          tmpfs     3.6G     0  3.6G   0% /dev/shm
tmpfs                          tmpfs     3.6G  137M  3.4G   4% /run
tmpfs                          tmpfs     3.6G     0  3.6G   0% /sys/fs/cgroup
/dev/mapper/vg_main-lv_root    xfs        16G  3.0G   13G  20% /
/dev/mapper/vg_dados-lv_dados  xfs        20G   33M   20G   1% /datastorage
/dev/mapper/vg_dados-lv_docker btrfs      80G  128M   79G   1% /var/lib/docker
/dev/xvdb1                     xfs       497M  161M  337M  33% /boot
tmpfs                          tmpfs     722M     0  722M   0% /run/user/0

1 个答案:

答案 0 :(得分:0)

tee用于在更新文件的同时查看文件内容。对于将文件归零并不十分有用。

如果要将文件归零,请使用“> file ”命令。如果您想将多个文件归零,则建议循环。将通配符放入 file 可能会导致有关歧义重定向的错误消息。像下面这样的东西应该起作用:

for f in /path/to/foo*; do
  >${f}
done

希望这会有所帮助