bash 递归解压缩文件,然后删除原始文件,将解压缩的文件留在 shell 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5590375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:23:51  来源:igfitidea点击:

Recursively unzip files and then delete original file, leaving unzipped files in place from shell

bashunziprm

提问by Ben

I've so far figured out how to use find to recursively unzip all the files:

到目前为止,我已经弄清楚如何使用 find 递归解压缩所有文件:

find . -depth -name `*.zip` -exec /usr/bin/unzip -n {} \; 

But, I can't figure out how to remove the zip files one at a time after the extraction. Adding rm *.zip in an -a -exec ends up deleting most of the zip files in each directory before they are extracted. Piping through a script containing the rm command (with -i enabled for testing) causes find to not find any *.zips (or at least that's what it complains). There is, of course, whitespace in many of the filenames but at this point syntaxing in a sed command to add _'s is a bit beyond me. Thank for your help!

但是,我不知道如何在提取后一次删除一个 zip 文件。在 -a -exec 中添加 rm *.zip 最终会在解压缩每个目录中删除大多数 zip 文件。通过包含 rm 命令的脚本(启用 -i 进行测试)导致 find 找不到任何 *.zips(或者至少这是它抱怨的)。当然,许多文件名中都有空格,但此时在 sed 命令中添加 _ 的语法有点超出我的范围。感谢您的帮助!

回答by ggiroux

have you tried:

你有没有尝试过:

find . -depth -name '*.zip' -exec /usr/bin/unzip -n {} \; -exec rm {} \;

or

或者

find . -depth -name '*.zip' -exec /usr/bin/unzip -n {} \; -delete

or running a second find after the unzip one

或在解压缩后运行第二个查找

find . -depth -name '*.zip' -exec rm {} \;   

回答by Nordmann

thx for the 2nd command with -delete! helped me a lot.. just 2 (maybe helpful) remarks from my side:

谢谢 -delete 的第二个命令!帮了我很多..我身边只有2个(可能有帮助)评论:

-had to use '.zip'instead of `.zip`on my debian system

-had到使用.ZIP”而不是`.zip`我的Debian系统

-use -execdirinstead of -exec> this will extract each zip file within its current folder, otherwise you end up with all extracted content in the dir you invoked the find cmd.

-use -execdir而不是-exec> 这将在其当前文件夹中提取每个 zip 文件,否则您最终会在调用 find cmd 的目录中获得所有提取的内容。

find . -depth -name '*.zip' -execdir /usr/bin/unzip -n {} \; -delete

找 。-depth -name '*.zip' -execdir /usr/bin/unzip -n {} \; -删除

THX & Regards, Nord

THX 和问候,北

回答by John D.

As mentioned above, this should work.

如上所述,这应该有效。

find . -depth -name '*.zip' -execdir unzip -n {} \; -delete

找 。-depth -name '*.zip' -execdir unzip -n {} \; -删除

However, note two things:

但是,请注意两点:

  • The -noption instructs unzip to not overwrite existing files. You may not know if the zip files differ from the similarly named target files. Even so, the -deletewill remove the zip file.
  • If unzip can't unzip the file--say because of an error--it might still delete it. The command will certainly remove it if -exec rm {} \;is used in place of -delete.
  • -n选项指示解压缩到不覆盖现有文件。您可能不知道 zip 文件是否与名称相似的目标文件不同。即便如此,-delete也会删除 zip 文件。
  • 如果 unzip 无法解压缩文件——比如因为错误——它可能仍会删除它。如果-exec rm {} \;命令肯定会删除它 用于代替-delete

A safer solution might be to move the files following the unzip to a separate directory that you can trash when you're sure you have extracted all the files successfully.

更安全的解决方案可能是将解压缩后的文件移动到一个单独的目录中,当您确定已成功解压缩所有文件时,您可以将其删除。

回答by cmcginty

Unzip archives in subdir based on the file name (../file.zip -> ../file/..):

根据文件名(../file.zip -> ../file/..)解压缩子目录中的档案:

for F in $(find . -depth -name *.zip); do unzip "$F" -d "${F%.*}/" && rm "$F"; done

回答by Minkymorgan

I have a directory filling up with zipped csv files. External processes are writing new zipped files to it often. I wish to bulk unzip and remove the originals as you do.

我有一个目录,里面装满了压缩的 csv 文件。外部进程经常向它写入新的压缩文件。我希望像您一样批量解压缩并删除原件。

To do that I use:

为此,我使用:

    unzip '*.zip'
    find . | sed 's/$/\.zip/g' | xargs -n 1 rm 

It works by searching and expanding all zip files presently in the directory. Later, after it finishes there are potentially new unzipped new files mixed in there too that are not to be deleted yet.

它的工作原理是搜索和展开目录中当前的所有 zip 文件。稍后,在它完成后,可能还会有新的解压缩新文件混入其中,但尚未删除。

So I delete by finding successfullyunzipped *.csv files, and using sed to regenerate the original filenames for deletion which is then fed to rm via the xargs command.

所以我通过找到成功解压缩的 *.csv 文件来删除,并使用 sed 重新生成原始文件名以进行删除,然后通过 xargs 命令将其提供给 rm。