递归提取所有 7z

递归提取所有 7z

我有许多 7z 档案嵌套在许多子文件夹中,并且想要将所有这些档案提取到它们所在的文件夹中,然后删除原始档案。

我已经找到了如何在提取到根目录时执行此操作。

while [ "`find . -type f -name '*.7z' | wc -l`" -gt 0 ]; do find -type f -name "*.7z" -exec 7za x -- '{}' \; -exec rm -- '{}' \;; done

但是,所有档案都被解压到了我执行命令的目录中——我想将所述档案提取到它们的原始位置并保留原始结构,但不确定如何更改它来做到这一点。

答案1

使用-execdir而不是-exec

find . -type f -name "*.7z" -execdir 7za x {} \; -exec rm -- {} \;

-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. 

其他的建议:

-quitwhile检查中使用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

相关内容