bash - 查找和移动所有文件,同时保持目录完整

标签 bash unix mkdir mv

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)

最佳答案

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

找到 . -name '*.ext1' -o -name '*.ext2' -exec blah

将被解析为

找到 . '(' -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" {} \;

关于bash - 查找和移动所有文件,同时保持目录完整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23861797/

相关文章:

bash - 如何修剪从 bash 上的标准输入读取的行?

linux - 将所有数据保存到变量 tcl

bash - bash 脚本中的 mkdir 问题

linux - Shell,For循环进入目录和子目录未按预期工作

linux - 为什么 "A=3; A=4 echo $A"在 Bash 中产生 3(而不是 4)?

bash - for 循环在控制台中工作但不作为 shell 脚本运行

c - 如何正确地接受来自另一个终端(比如 pts/1)的输入?

unix - 在Linux中使用sed过滤html标签

变量路径 "cannot create directory : No such file or directory"上的 bash mkdir 和 cp 错误

android - 无法在 SD 卡上创建文件夹 - mkdir 总是返回 false