我正在尝试编写一个使用 find 和 xargs 将旧文件存档在大目录中的脚本。这是该行:
find /tmp/messages/ -mtime +9 -print0 | xargs -x -t -0 -n 1000 sh -c 'tar rPf /tmp/backup.tar "$@" && rm -f "$@"; sleep 1 && echo Finished one loop: $(date +"%T")'
该脚本大部分工作正常,但每次命令运行 1000 个文件时它都会跳过第一个文件,我似乎不明白为什么。
这是另一个例子(我尝试使用更简单的命令来看看会发生什么:
如果我只使用 echo 和 xargs,我可以有效地运行一个命令:
$ echo a b c d e f| xargs -n 2 echo "$@"
a b
c d
e f
$ echo a b c d e f| xargs -n 3 echo "$@"
a b c
d e f
另一方面,如果我想在每次打印每组时休眠,我添加 sh -c 来添加一串命令:
$ echo a b c d e f| xargs -n 2 sh -c 'echo "$@"; sleep 1;'
b
d
f
$ echo a b c d e f| xargs -n 3 sh -c 'echo "$@"; sleep 1;'
b c
e f
看看第一项是如何被跳过的?
对这里可能发生的事情有什么想法吗?
提前致谢!如果您需要任何额外信息,请告诉我,我很乐意提供。
第一个论点sh -c
or bash -c
是脚本的名称,即$0
当您使用时不会打印$@
:
例子:
echo a b c d e f| xargs -n 3 bash -c 'echo "$0 $@"'
a b c
d e f
要解决这个问题,你可以通过_
作为脚本的虚拟名称,然后它应该可以工作:
echo a b c d e f| xargs -n 3 bash -c 'echo "$@"' _
a b c
d e f
即使与您一起使用它也能正常工作sleep
例子:
echo a b c d e f| xargs -n 3 bash -c 'echo "$@"; sleep 1' _
a b c
d e f
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)