查找并移动所有文件,同时保持目录不变

Mor*_*rki 3 unix bash mv mkdir

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" {} \;
Run Code Online (Sandbox Code Playgroud)

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

Source/
\->File (misses this)
\->Folder/
   \->File (finds/moves this)
Run Code Online (Sandbox Code Playgroud)

jwo*_*der 5

-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" {} \;
Run Code Online (Sandbox Code Playgroud)