加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – xargs -l的替代品

发布时间:2020-12-16 01:18:45 所属栏目:安全 来源:网络整理
导读:我想将一堆dirs从DIR重命名为DIR.OLD.理想情况下,我会使用以下内容: find . -maxdepth 1 -type d -name "*.y" -mtime +`expr 2 * 365` -print0 | xargs -0 -r -I file mv file file.old 但我想要执行此操作的机器已安装BusyBox,而BusyBox xargs不支持“-
我想将一堆dirs从DIR重命名为DIR.OLD.理想情况下,我会使用以下内容:
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”选项.

有哪些常用的替代方法可以收集文件数组,然后在shell脚本中执行它们?

您可以使用find命令的-exec和{}功能,因此您根本不需要任何管道:
find -maxdepth 1 -type d -name "*.y" -mtime +`expr 2 * 365` -exec mv "{}" "{}.old" ;

您也不需要指定’.’ path – 这是find的默认值.你在“* .y”中使用了额外的斜杠.当然,如果您的文件名实际上不包含引号.

公平地说,应该注意的是,具有while循环的版本是此处提出的最快版本.以下是一些示例测量:

$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

我认为这是因为find和xargs每次执行命令都会调用额外的/ bin / sh(实际上是exec(3)执行它),而shell while循环则不会.

更新:如果您的busybox版本是在没有-exec选项支持的情况下编译的,那么在其他答案(one,two)中建议的while循环或xargs就是您的方式.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读