Linux 如何在bash中将所有子目录中的所有文件gzip到一个压缩文件中

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

How to gzip all files in all sub-directories into one compressed file in bash

linuxbashshellgzip

提问by Doug

Possible Duplicate:
gzipping up a set of directories and creating a tar compressed file

可能的重复:
gzip 压缩一组目录并创建一个 tar 压缩文件

This postdescribes how to gzip each file individually within a directory structure. However, I need to do something slightly different. I need to produce one big gzip file for all files under a certain directory. I also need to be able to specify the output filename for the compressed file (e.g., files.gz) and overwrite the old compressed file file if one already exists.

这篇文章描述了如何在目录结构中单独 gzip 每个文件。但是,我需要做一些稍微不同的事情。我需要为某个目录下的所有文件生成一个大的 gzip 文件。我还需要能够指定压缩文件的输出文件名(例如,files.gz)并覆盖旧的压缩文件(如果已经存在)。

采纳答案by amitchhajer

tar -zcvf compressFileName.tar.gz folderToCompress

everything in folderToCompress will go to compressFileName

folderToCompress 中的所有内容都将转到 compressFileName

Edit: After review and comments I realized that people may get confused with compressFileName without an extension. If you want you can use .tar.gz extension(as suggested) with the compressFileName

编辑:经过和评论,我意识到人们可能会对没有扩展名的 compressFileName 感到困惑。如果你愿意,你可以使用 .tar.gz 扩展名(如建议)和 compressFileName

回答by Jeremy J Starcher

@amitchhajer 's post works for GNU tar. If someone finds this post and needs it to work on a NON GNUsystem, they can do this:

@amitchhajer 的帖子适用于 GNU tar。如果有人发现这篇文章并需要它在非GNU系统上工作,他们可以这样做:

tar cvf - folderToCompress | gzip > compressFileName

To expand the archive:

要展开存档:

zcat compressFileName | tar xvf -

回答by V H

there are lots of compression methods that work recursively command line and its good to know who the end audience is.

有很多压缩方法可以递归命令行工作,知道最终受众是谁很高兴。

i.e. if it is to be sent to someone running windows then zip would probably be best:

即如果要发送给运行 Windows 的人,那么 zip 可能是最好的:

zip -r file.zip folder_to_zip

unzip filenname.zip

for other linux users or your self tar is great

对于其他 linux 用户或您的 self tar 很棒

tar -cvzf filename.tar.gz folder

tar -cvjf filename.tar.bz2 folder  # even more compression

#change the -c to -x to above to extract

One must be careful with tar and how things are tarred up/extracted, for example if I run

人们必须小心使用 tar 以及如何将事情焦油化/提取,例如,如果我运行

cd ~
tar -cvzf passwd.tar.gz /etc/passwd
tar: Removing leading `/' from member names
/etc/passwd


pwd

/home/myusername

/家/我的用户名

tar -xvzf passwd.tar.gz

this will create /home/myusername/etc/passwd

这将创建 /home/myusername/etc/passwd

unsure if all versions of tar do this:

不确定所有版本的 tar 是否都这样做:

 Removing leading `/' from member names