【问题标题】:moving files recurisively on linux在linux上递归移动文件
【发布时间】:2011-01-02 15:12:06
【问题描述】:

find ./dir -type f -iname "*.t[argz]*[bz2]" -print | xargs mv --target-directory=dir 似乎在名称中有空格的文件上失败。 如何改进它?还是替代品?

感谢下面的回答:我的 mv 不支持 --null 或 -0,我使用的是 cygwin:

$ mv --帮助 用法: mv [OPTION]... [-T] SOURCE DEST 或: mv [OPTION]... SOURCE... DIRECTORY 或: mv [选项]... -t 目录源... 将 SOURCE 重命名为 DEST,或将 SOURCE(s) 移动到 DIRECTORY。 强制性论据 .

【问题讨论】:

    标签: linux find xargs


    【解决方案1】:

    find 命令和xargs -0(或--null)选项上使用-print0 代替-print - 然后NUL 将用作分隔符而不是换行符和空格。

    find ./dir -type f -iname "*.t[argz]*[bz2]" -print0 | xargs --null mv --target-directory=dir
    

    【讨论】:

    • $ find ./ -type f -iname "*.xls" -print0 | xargs mv --null --target-directory=x ls xargs: 警告:输入中出现 NUL 字符。它不能在参数列表中传递。您的意思是使用 --null 选项吗? mv:无法识别的选项--null' Try mv --help' 了解更多信息。
    • 抱歉 - 应该是 xargs 选项,而不是 mv 选项!固定。
    【解决方案2】:

    您是否查看过 find 的 -exec 选项?

    find ./dir -type f -iname "*.t[argz][bz2]" -exec mv {} --target-directory=dir ';' 
    

    -exec 选项将执行它后面的任何选项作为命令,直到它看到用引号括起来的分号。这样您就不必处理间距问题。

    【讨论】:

    • 这会为每个文件生成一个子进程,其中 xargs 变体将数百个文件批处理到同一命令行上。对于像单个 mv 调用这样的小型操作,这可能是一个显着的性能差异。尽管您必须使用 -print0 和 --null ,正如马丁在上面指出的那样,除非您绝对确定文件名没有空格。
    【解决方案3】:

    GNU 查找

    find ./dir -type f -iname "*.t[argz]*[bz2]" -exec mv "{}" /destination +;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-19
      • 2020-11-22
      • 1970-01-01
      • 2017-03-09
      • 1970-01-01
      • 1970-01-01
      • 2020-11-23
      • 2013-04-05
      相关资源
      最近更新 更多