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/