提取并删除目录中的所有 .gz - Linux

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16038087/
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-08-06 22:43:37  来源:igfitidea点击:

Extract and delete all .gz in a directory- Linux

linuxgzipextractgunzip

提问by user2247643

I have a directory. It has about 500K .gz files.

我有一个目录。它有大约 500K .gz 文件。

How can I extract all .gz in that directory and delete the .gz files?

如何提取该目录中的所有 .gz 并删除 .gz 文件?

回答by Steven Penny

for foo in *.gz
do
  tar xf "$foo"
  rm "$foo"
done

回答by techedemic

There's more than one way to do this obviously.

显然有不止一种方法可以做到这一点。

    # This will find files recursively (you can limit it by using some 'find' parameters. 
    # see the man pages
    # Final backslash required for exec example to work
    find . -name '*.gz' -exec gunzip '{}' \;

    # This will do it only in the current directory
    for a in *.gz; do gunzip $a; done

I'm sure there's other ways as well, but this is probably the simplest.

我相信还有其他方法,但这可能是最简单的。

And to remove it, just do a rm -rf *.gzin the applicable directory

并删除它,只需rm -rf *.gz在适用目录中执行

回答by Adrian Dunn

This should do it:

这应该这样做:

gunzip *.gz

回答by Jokerius

Extract all gz files in current directory and its subdirectories:

提取当前目录及其子目录中的所有 gz 文件:

 find . -name "*.gz" | xargs gunzip 

回答by elhaj

@techedemic is correct but is missing '.' to mention the current directory, and this command go throught all subdirectories.

@techedemic 是正确的,但缺少 '.' 提及当前目录,此命令遍历所有子目录。

find . -name '*.gz' -exec gunzip '{}' \;

回答by user2921779

Try:

尝试:

ls -1 | grep -E "\.tar\.gz$" | xargs -n 1 tar xvfz

Then Try:

然后尝试:

ls -1 | grep -E "\.tar\.gz$" | xargs -n 1 rm

This will untar all .tar.gz files in the current directory and then delete all the .tar.gz files. If you want an explanation, the "|" takes the stdout of the command before it, and uses that as the stdin of the command after it. Use "man command" w/o the quotes to figure out what those commands and arguments do. Or, you can research online.

这将解压当前目录中的所有 .tar.gz 文件,然后删除所有 .tar.gz 文件。如果你想解释,“|” 获取之前命令的标准输出,并将其用作其后命令的标准输入。使用不带引号的“man command”来找出这些命令和参数的作用。或者,您可以在线研究。

回答by Neelam

If you want to extract a single file use:

如果要提取单个文件,请使用:

gunzip file.gz

枪压缩文件.gz

It will extract the file and remove .gz file.

它将提取文件并删除 .gz 文件。