我有许多嵌套在许多子文件夹中的 7z 存档,并且希望将所有这些存档提取到它们所在的文件夹中,然后删除原始存档。
我已经找到了如何执行此操作,同时提取到根目录。
while [ "`find . -type f -name '*.7z' | wc -l`" -gt 0 ]; do find -type f -name "*.7z" -exec 7za x -- '{}' \; -exec rm -- '{}' \;; done
Run Code Online (Sandbox Code Playgroud)
然而,所有档案都被解压到我执行命令的目录中——我想将所述档案提取到保留原始结构的原始位置,但不确定如何更改它来做到这一点。
使用-execdir而不是-exec;
find . -type f -name "*.7z" -execdir 7za x {} \; -exec rm -- {} \;
Run Code Online (Sandbox Code Playgroud)
-execdir在包含该文件的目录中运行。从man find:
-execdir command {} +
Like -exec, but the specified command is run from the
subdirectory containing the matched file, which is not normally
the directory in which you started find.
Run Code Online (Sandbox Code Playgroud)
其他的建议:
-quit在while检查中使用find,这样find在找到匹配项后就不会继续搜索(请参阅https://unix.stackexchange.com/a/13880/70524):
while [ -n "$(find . -type f -name '*.7z' -print -quit)" ]
do
find . -type f -name "*.7z" -execdir 7za x {} \; -exec rm -- {} \;
done
Run Code Online (Sandbox Code Playgroud)