替代xargs的-l [英] Alternatives to xargs -l
问题描述
我要重新命名了一堆从DIR迪尔斯到DIR.OLD的。理想情况下,我会用以下内容:
I want to rename a bunch of dirs from DIR to DIR.OLD. Ideally I would use the following:
find . -maxdepth 1 -type d -name \"*.y\" -mtime +`expr 2 \* 365` -print0 | xargs -0 -r -I file mv file file.old
不过该机我想在有执行此安装的BusyBox和BusyBox的xargs的不支持-I选项。
But the machine I want to execute this on has BusyBox installed and the BusyBox xargs doesn't support the "-I" option.
什么是收集文件的数组,然后在shell脚本对他们执行?一些常见的替代方法。
What are some common alternative methods for collecting an array of files and then executing on them in a shell script?
推荐答案
您可以使用 -exec
和 {}
特征的找到
命令,所以你不需要任何管道都:
You can use -exec
and {}
features of the find
command so you don't need any pipes at all:
find -maxdepth 1 -type d -name "*.y" -mtime +`expr 2 \* 365` -exec mv "{}" "{}.old" \;
你也不必指定'。路径 - 这是默认找到
。而且你用多余的斜线在* Y
。当然,如果你的文件名真的不包含引号。
Also you don't need to specify '.' path - this is default for find
. And you used extra slashes in "*.y"
. Of course if your file names do not really contain quotes.
在公平,应该注意的是,版本而读
循环是最快的这里提出。下面是一些例子测量:
In fairness it should be noted, that version with while read
loop is the fastest of proposed here. Here are some example measurements:
$ cat measure
#!/bin/sh
case $2 in
1) find "$1" -print0 | xargs -0 -I file echo mv file file.old ;;
2) find "$1" -exec echo mv '{}' '{}.old' \; ;;
3) find "$1" | while read file; do
echo mv "$file" "$file.old"
done;;
esac
$ time ./measure android-ndk-r5c 1 | wc
6225 18675 955493
real 0m6.585s
user 0m18.933s
sys 0m4.476s
$ time ./measure android-ndk-r5c 2 | wc
6225 18675 955493
real 0m6.877s
user 0m18.517s
sys 0m4.788s
$ time ./measure android-ndk-r5c 3 | wc
6225 18675 955493
real 0m0.262s
user 0m0.088s
sys 0m0.236s
我想这是因为找到
和的xargs
调用额外的/ bin / sh的(实际上执行exec(3)
做的话)执行命令每一次,而壳,而
循环没有。
I think it's because find
and xargs
invokes additional /bin/sh (actually exec(3)
does it) every time for execute a command, while shell while
loop do not.
UPD:如果你的busybox版本没有 -exec
编为找到$ C $选项支持C>命令则
,而
环或的xargs
,建议在其他的答案(的one , 2 ),是你的方式。
Upd: If your busybox version was compiled without -exec
option support for the find
command then the while
loop or xargs
, suggested in the other answers (one, two), is your way.
这篇关于替代xargs的-l的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!