【问题标题】:Finding and moving all files while keeping directories intact查找和移动所有文件,同时保持目录完整
【发布时间】:2014-07-14 17:39:33
【问题描述】:
read source
read destination 
destination=$(cd -- "$destination" && pwd)
cd -- "$source" &&
  find . -name '*.ext1' -o -name '*.ext2' -exec sh -c '
    mkdir -p "$0/${1%/*}"
    mv "$1" "$0/$1"
  ' "$destination" {} \;

我有上面的代码来查找文件,然后尝试保留目录结构。但问题是它没有找到并移动我请求类型的所有文件 - 有什么问题?它似乎错过了不在最低目录级别的文件。

Source/
\->File (misses this)
\->Folder/
   \->File (finds/moves this)

【问题讨论】:

    标签: bash unix mkdir mv


    【解决方案1】:

    -o 的优先级低于相邻表达式之间隐含的-a,因此

    find . -name '*.ext1' -o -name '*.ext2' -exec blah

    将被解析为

    find . '(' -name '*.ext1' ')' -o '(' -name '*.ext2' -exec blah ')'

    要得到你想要的,做:

    find . '(' -name '*.ext1' -o -name '*.ext2' ')' -exec sh -c '
      mkdir -p "$0/${1%/*}"
      mv "$1" "$0/$1"
    ' "$destination" {} \;
    

    【讨论】:

      猜你喜欢
      • 2013-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多