Linux 使用 Gzip 单独压缩多个文件

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

Compress multiple files individually with Gzip

linuxunixcompressiongzip

提问by neversaint

I have several directories that look like this:

我有几个看起来像这样的目录:

dir1/
  |_foo.txt
  |_bar.txt
dir2/
  |_qux.txt
  |_bar.txt

For each of this directory I want to compress the files inside it into *.gz format then delete the uncompressed ones. So finally we hope to get something like this:

对于这个目录中的每一个,我想将其中的文件压缩为 *.gz 格式,然后删除未压缩的文件。所以最后我们希望得到这样的东西:

 dir1/
   |_foo.gz
   |_bar.gz
 dir2/
   |_qux.gz
   |_bar.gz

Is there a simple Unix way to do it?

有没有简单的 Unix 方法来做到这一点?

采纳答案by Juliano

gzip */*.txt

But the extension for each file will be .txt.gz, as gzip uses it to know the original filename.

但是每个文件的扩展名都是 .txt.gz,因为 gzip 使用它来知道原始文件名。

回答by vovick

gzip -r dir1 dir2

gzip -r dir1 dir2

回答by dogbane

The following will work even if you have sub-directories. E.g. dir1/dir2/.../foo.txt

即使您有子目录,以下内容也将起作用。例如 dir1/dir2/.../foo.txt

find . -type f -name "*.txt" -exec gzip {} \;