创建 .tar.gz 文件时排除目录

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

Excluding directory when creating a .tar.gz file

linuxgziptarcompression

提问by suresh

I have a /public_html/folder, in that folder there's a /tmp/folder that has like 70gb of files I don't really need.

我有一个/public_html/文件夹,在那个文件夹中有一个/tmp/文件夹,里面有 70gb 的文件,我真的不需要。

Now I am trying to create a .tar.gzof /public_html/excluding /tmp/

现在,我想创建一个.tar.gz/public_html/排除/tmp/

This is the command I ran:

这是我运行的命令:

tar -pczf MyBackup.tar.gz /home/user/public_html/ --exclude "/home/user/public_html/tmp/" 

The tar is still being created, and by doing an ls -shI can see that MyBackup.tar.gzalready has about 30gb, and I know for sure that /public_html/without /tmp/doesn't have more than 1GB of files.

tar 仍在创建中,通过执行ls -sh我可以看到它MyBackup.tar.gz已经有大约 30GB,而且我确信/public_html/没有/tmp/超过 1GB 的文件。

What did I do wrong?

我做错了什么?

采纳答案by Victor Parmar

Try removing the last / at the end of the directory path to exclude

尝试删除要排除的目录路径末尾的最后一个 /

tar -pczf MyBackup.tar.gz /home/user/public_html/ --exclude "/home/user/public_html/tmp" 

回答by Martin Algesten

Try moving the --excludeto before the include.

尝试将--excludeto移动到包含之前。

tar -pczf MyBackup.tar.gz --exclude "/home/user/public_html/tmp/" /home/user/public_html/ 

回答by user

Yes, remove the trailing /and (at least in ubuntu 11.04) all the paths given must be relative or full path. You can't mix absolute and relative paths in the same command.

是的,删除尾随/并且(至少在 ubuntu 11.04 中)所有给定的路径必须是相对路径或完整路径。您不能在同一命令中混合使用绝对路径和相对路径。

sudo tar -czvf 2011.10.24.tar.gz ./start-directory --exclude "home/user/start-directory/logs"

will not exclude logs directory but

不会排除日志目录,但

sudo tar -czvf 2011.10.24.tar.gz ./start-directory --exclude "./start-directory/logs"

will work

将工作

回答by Yogesh

Try this

尝试这个

tar -pczvf advancedarts.tar.gz /home/user/public_html/ --exclude /home/user/public_html/tmp

回答by Ivan Avery Frey

The exclude option needs to include the =sign and "are not required.

exclude 选项需要包含=符号并且"不是必需的。

--exclude=/home/user/public_html/tmp

回答by oussaka

The correct command for exclude directory from compression is :

从压缩中排除目录的正确命令是:

tar --exclude='./folder' --exclude='./upload/folder2' -zcvf backup.tar.gz backup/

Make sure to put --exclude before the source and destination items.

确保将--exclude放在源和目标项目之前

and you can check the contents of the tar.gz file without unzipping :

您可以在不解压缩的情况下检查 tar.gz 文件的内容:

tar -tf backup.tar.gz

回答by LozanoMatheus

You can also exclude more than one using only one --exclude. Like this example:

您还可以仅使用一个来排除多个--exclude。像这个例子:

tar -pczf MyBackup.tar.gz --exclude={"/home/user/public_html/tmp","/home/user/public_html/data"} /home/user/public_html/

In --exclude=you must finish the directory name without /and must in between MyBackup.tar.gzand /home/user/public_html/

MyBackup.tar.gz/home/user/public_html/之间--exclude=你必须完成目录名而不/必须

The syntax is:

语法是:

tar <OPTIONS> <TARBALL_WILL_CREATE> <ARGS> <PATH_TO_COMPRESS>

回答by user2081279

The accepted answer did not work for me, running unxutils tar on windows10. Instead, I had to put the files/dirs to archive as the lastparameter, like this:

接受的答案对我不起作用,在 windows10 上运行 unxutils tar。相反,我不得不将文件/目录作为最后一个参数归档,如下所示:

tar -pczf MyBackup.tar.gz --exclude "/home/user/public_html/tmp/" /home/user/public_html/

Then it worked.

然后它起作用了。

回答by Black

This worked for me:

这对我有用:

tar -zcvf target.tar.gz target/ --exclude="target/backups" --exclude="target/cache"

回答by scegg

tar -pczf <target_file.tar.gz> --exclude /path/to/exclude --exclude /another/path/to/exclude/* /path/to/include/ /another/path/to/include/*

tar -pczf <target_file.tar.gz> --exclude /path/to/exclude --exclude /another/path/to/exclude/* /path/to/include/ /another/path/to/include/*

Tested in Ubuntu 19.10.

在 Ubuntu 19.10 中测试。

  1. The =after excludeis optional. You can use =instead of space after keyword excludeif you like.
  2. Parameter excludemust be placed before the source.
  3. The difference between use folder name (like the 1st) or the * (like the 2nd) is: the 2nd one will include an empty folder in package but the 1st will not.
  1. =exclude是可选的。如果您愿意,您可以=在关键字后使用空格代替exclude
  2. 参数exclude必须放在源之前。
  3. 使用文件夹名称(如第一个)或 *(如第二个)之间的区别是:第二个将在包中包含一个空文件夹,但第一个不会。