so, you want to make a tar file that contain everyting inside /home/ftp/mysite (to move the site to a new server), but file3 is just junk, and everything in folder3 is also not needed, so we will skip those two.
we use the format
tar -czvf <name of tar file> <what to tar> <any excludes>
where the c = create, z = zip, and v = verbose (you can see the files as they are entered, usefull to make sure none of the files you exclude are being added). and f= file.
so, my command would look like this
cd /home/ftp/
tar -czvf mysite.tar.gz mysite --exclude='file3' --exclude='folder3'
note the files/folders excluded are relatively to the root of your tar (I have tried full path here relative to / but I can not make that work).
hope this will help someone (and me next time I google it)
我想展示另一个选项,我用来获得与之前提供的答案相同的结果,我有一个类似的情况,我想将 android studio 项目全部备份在 tar 文件中以上传到 media fire,使用 du 命令来查找大文件,我发现我不需要一些目录,例如:
构建、linux e .dart_tools
使用 Charles_ma 的第一个答案,我对其进行了一些修改,以便能够从 Android 目录的父目录运行命令。
tar --exclude='*/build' --exclude='*/linux' --exclude='*/.dart_tool' -zcvf androidProjects.tar Android/
它就像一个魅力。
诗。 抱歉,如果不允许这种答案,如果是这种情况,我将删除。
I'd like to show another option I used to get the same result as the answers before provide, I had a similar case where I wanted to backup android studio projects all together in a tar file to upload to media fire, using the du command to find the large files, I found that I didn't need some directories like:
build, linux e .dart_tools
Using the first answer of Charles_ma I modified it a little bit to be able to run the command from the parent directory of the my Android directory.
tar --exclude='*/build' --exclude='*/linux' --exclude='*/.dart_tool' -zcvf androidProjects.tar Android/
It worked like a charm.
Ps. Sorry if this kind of answer is not allowed, if this is the case I will remove.
我经历过这种情况,至少在我使用的 Cygwin 版本的 tar 中 ("CYGWIN_NT-5.1 1.7.17(0.262/5/3) 2012-10-19 14:39 i686 Cygwin “在 Windows XP Home Edition SP3 计算机上),选项的顺序很重要。
虽然这个结构对我有用:
tar cfvz target.tgz --exclude='<dir1>' --exclude='<dir2>' target_dir
那个没有工作:
tar cfvz --exclude='<dir1>' --exclude='<dir2>' target.tgz target_dir
这个,而tar --help揭示了以下内容:
tar [OPTION...] [FILE]
所以,第二个命令也应该工作,但显然情况似乎并非如此...
最好的rgds,
I've experienced that, at least with the Cygwin version of tar I'm using ("CYGWIN_NT-5.1 1.7.17(0.262/5/3) 2012-10-19 14:39 i686 Cygwin" on a Windows XP Home Edition SP3 machine), the order of options is important.
While this construction worked for me:
tar cfvz target.tgz --exclude='<dir1>' --exclude='<dir2>' target_dir
that one didn't work:
tar cfvz --exclude='<dir1>' --exclude='<dir2>' target.tgz target_dir
This, while tar --help reveals the following:
tar [OPTION...] [FILE]
So, the second command should also work, but apparently it doesn't seem to be the case...
不同的 tar 版本需要以不同的顺序使用此选项:例如,@Andrew 的回答 表明在 GNU tar v 1.26 和 1.28 中排除项排在最后,而在我的情况下,对于 GNU tar 1.29,则相反。
尾部斜杠很重要:至少在 GNU tar 1.29 中,不应该有任何斜杠。
就我而言,对于 Debian Stretch 上的 GNU tar 1.29,有效的命令是
tar --exclude="/home/user/.config/chromium" --exclude="/home/user/.cache" -cf file.tar /dir1/ /home/ /dir3/
引号并不重要,有或没有它们都可以。
我希望这对某人有用。
After reading all this good answers for different versions and having solved the problem for myself, I think there are very small details that are very important, and rare to GNU/Linux general use, that aren't stressed enough and deserves more than comments.
So I'm not going to try to answer the question for every case, but instead, try to register where to look when things doesn't work.
IT IS VERY IMPORTANT TO NOTICE:
THE ORDER OF THE OPTIONS MATTER: it is not the same put the --exclude before than after the file option and directories to backup. This is unexpected at least to me, because in my experience, in GNU/Linux commands, usually the order of the options doesn't matter.
Different tar versions expects this options in different order: for instance, @Andrew's answer indicates that in GNU tar v 1.26 and 1.28 the excludes comes last, whereas in my case, with GNU tar 1.29, it's the other way.
THE TRAILING SLASHES MATTER: at least in GNU tar 1.29, it shouldn't be any.
In my case, for GNU tar 1.29 on Debian stretch, the command that worked was
tar --exclude="/home/user/.config/chromium" --exclude="/home/user/.cache" -cf file.tar /dir1/ /home/ /dir3/
The quotes didn't matter, it worked with or without them.
If you are trying to exclude Version Control System (VCS) files, tar already supports two interesting options about it! :)
Option : --exclude-vcs
This option excludes files and directories used by following version control systems: CVS, RCS, SCCS, SVN, Arch, Bazaar, Mercurial, and Darcs.
As of version 1.32, the following files are excluded:
CVS/, and everything under it
RCS/, and everything under it
SCCS/, and everything under it
.git/, and everything under it
.gitignore
.gitmodules
.gitattributes
.cvsignore
.svn/, and everything under it
.arch-ids/, and everything under it
{arch}/, and everything under it
=RELEASE-ID
=meta-update
=update
.bzr
.bzrignore
.bzrtags
.hg
.hgignore
.hgrags
_darcs
Option : --exclude-vcs-ignores
When archiving directories that are under some version control system (VCS), it is often convenient to read exclusion patterns from this VCS' ignore files (e.g. .cvsignore, .gitignore, etc.) This option provide such possibility.
Before archiving a directory, see if it contains any of the following files: cvsignore, .gitignore, .bzrignore, or .hgignore. If so, read ignore patterns from these files.
The patterns are treated much as the corresponding VCS would treat them, i.e.:
.cvsignore
Contains shell-style globbing patterns that apply only to the directory where this file resides. No comments are allowed in the file. Empty lines are ignored.
.gitignore
Contains shell-style globbing patterns. Applies to the directory where .gitfile is located and all its subdirectories.
Any line beginning with a # is a comment. Backslash escapes the comment character.
.bzrignore
Contains shell globbing-patterns and regular expressions (if prefixed with RE:(16). Patterns affect the directory and all its subdirectories.
Any line beginning with a # is a comment.
.hgignore
Contains posix regular expressions(17). The line syntax: glob switches to shell globbing patterns. The line syntax: regexp switches back. Comments begin with a #. Patterns affect the directory and all its subdirectories.
Example
tar -czv --exclude-vcs --exclude-vcs-ignores -f path/to/my-tar-file.tar.gz path/to/my/project/
我在其他地方发现了这个,所以我不会相信,但对于我的 mac 特定问题,它比上面的任何解决方案都有效(即使它已关闭):
tar zc --exclude __MACOSX --exclude .DS_Store -f <archive> <source(s)>
I found this somewhere else so I won't take credit, but it worked better than any of the solutions above for my mac specific issues (even though this is closed):
tar zc --exclude __MACOSX --exclude .DS_Store -f <archive> <source(s)>
如果使用 < code>--exclude='data.txt' 存档将不包含 EITHER data.txt 文件。 如果存档第三方库(例如 node_modules 目录),这可能会导致意外结果。
要避免此问题,请确保提供完整路径,例如 --exclude='./dirA/data.txt'
I agree the --exclude flag is the right approach.
$ tar --exclude='./folder_or_file' --exclude='file_pattern' --exclude='fileA'
A word of warning for a side effect that I did not find immediately obvious:
The exclusion of 'fileA' in this example will search for 'fileA' RECURSIVELY!
Example:A directory with a single subdirectory containing a file of the same name (data.txt)
If using --exclude='data.txt' the archive will not contain EITHER data.txt file. This can cause unexpected results if archiving third party libraries, such as a node_modules directory.
To avoid this issue make sure to give the entire path, like --exclude='./dirA/data.txt'
避免由于使用 find ... | 可能出现的 'xargs: Argument list too long' 错误 xargs ... 当处理数万个文件时,您可以使用 find ... -print0 将 find 的输出直接通过管道传输到 tar | tar --null ...。
# archive a given directory, but exclude various files & directories
# specified by their full file paths
find "$(pwd -P)" -type d \( -path '/path/to/dir1' -or -path '/path/to/dir2' \) -prune \
-or -not \( -path '/path/to/file1' -or -path '/path/to/file2' \) -print0 |
gnutar --null --no-recursion -czf archive.tar.gz --files-from -
#bsdtar --null -n -czf archive.tar.gz -T -
To avoid possible 'xargs: Argument list too long' errors due to the use of find ... | xargs ... when processing tens of thousands of files, you can pipe the output of find directly to tar using find ... -print0 | tar --null ....
# archive a given directory, but exclude various files & directories
# specified by their full file paths
find "$(pwd -P)" -type d \( -path '/path/to/dir1' -or -path '/path/to/dir2' \) -prune \
-or -not \( -path '/path/to/file1' -or -path '/path/to/file2' \) -print0 |
gnutar --null --no-recursion -czf archive.tar.gz --files-from -
#bsdtar --null -n -czf archive.tar.gz -T -
Use the find command in conjunction with the tar append (-r) option. This way you can add files to an existing tar in a single step, instead of a two pass solution (create list of files, create tar).
You can use cpio(1) to create tar files. cpio takes the files to archive on stdin, so if you've already figured out the find command you want to use to select the files the archive, pipe it into cpio to create the tar file:
gnu tar v 1.26 --exclude 需要位于存档文件和备份目录参数之后,不应有前导或尾随斜杠,并且最好不使用引号(单引号或双引号)。 因此相对于要备份的 PARENT 目录,它是:
tar cvfz /path_to/mytar.tgz ./dir_to_backup --exclude=some_path/to_exclude
gnu tar v 1.26 the --exclude needs to come after archive file and backup directory arguments, should have no leading or trailing slashes, and prefers no quotes (single or double). So relative to the PARENT directory to be backed up, it's:
tar cvfz /path_to/mytar.tgz ./dir_to_backup --exclude=some_path/to_exclude
tar -cvzf destination_folder source_folder -X /home/folder/excludes.txt
-X indicates a file which contains a list of filenames which must be excluded from the backup. For Instance, you can specify *~ in this file to not include any filenames ending with ~ in the backup.
It seems to be impossible to exclude directories with absolute paths.
As soon as ANY of the paths are absolute (source or/and exclude) the exclude command will not work. That's my experience after trying all possible combinations.
tar --exclude='.git' -zcvf /tmp/angular.23.12.19.tar.gz /var/lib/tomcat7/webapps/angular/
I want to have fresh front-end version (angular folder) on localhost.
Also, git folder is huge in my case, and I want to exclude it.
I need to download it from server, and unpack it in order to run application.
Compress angular folder from /var/lib/tomcat7/webapps, move it to /tmp folder with name angular.23.12.19.tar.gz
Command :
tar --exclude='.git' -zcvf /tmp/angular.23.12.19.tar.gz /var/lib/tomcat7/webapps/angular/
tar --exclude ./usr --exclude ./mnt --create --file - . (cd /mnt && tar xvd -)
我的全部观点是,有必要(通过放置 ./)指定来 tar 排除的目录所在的部分正在复制的更大目录。
我的 €0.02
Possible redundant answer but since I found it useful, here it is:
While a FreeBSD root (i.e. using csh) I wanted to copy my whole root filesystem to /mnt but without /usr and (obviously) /mnt. This is what worked (I am at /):
tar --exclude ./usr --exclude ./mnt --create --file - . (cd /mnt && tar xvd -)
My whole point is that it was necessary (by putting the ./) to specify to tar that the excluded directories where part of the greater directory being copied.
I had no luck getting tar to exclude a 5 Gigabyte subdirectory a few levels deep. In the end, I just used the unix Zip command. It worked a lot easier for me.
So for this particular example from the original post
(tar --exclude='./folder' --exclude='./upload/folder2' -zcvf /backup/filename.tgz . )
The equivalent would be:
zip -r /backup/filename.zip . -x upload/folder/**\* upload/folder2/**\*
eval and single quotes around {} are used for special names (containing spaces, for example). If your folders do not have special chars in the name, you can skip the eval and the single quotes.
发布评论
评论(30)
您可以为 tar 设置多个排除选项,
这样等就可以了。 确保确保将
--exclude
放在源和目标项目之前。You can have multiple exclude options for tar so
etc will work. Make sure to put
--exclude
before the source and destination items.您可以使用 tar 的
--exclude
排除目录。如果您想存档除
/usr
之外的所有内容,您可以使用:在您的情况下,也许类似
You can exclude directories with
--exclude
for tar.If you want to archive everything except
/usr
you can use:In your case perhaps something like
使用 tar 从备份中排除文件/目录的可能选项:
使用多个模式排除文件
使用填充模式列表的排除文件
排除文件 通过将标记文件放置在任何应跳过的目录中来排除使用标记的文件
Possible options to exclude files/directories from backup using tar:
Exclude files using multiple patterns
Exclude files using an exclude file filled with a list of patterns
Exclude files using tags by placing a tag file in any directory that should be skipped
老问题有很多答案,但我发现没有一个对我来说足够清楚,所以我想添加我的尝试。
如果您具有以下结构
和以下文件/文件夹
,那么您想要制作一个包含 /home/ftp/mysite 内所有内容的 tar 文件(将站点移动到新服务器),但是
file3
只是垃圾,并且folder3
中的所有内容也都不需要,因此我们将跳过这两个。我们使用以下格式
:c = create、z = zip 和 v = verbose(您可以在输入文件时查看文件,这有助于确保没有添加您排除的文件)。 和 f= 文件。
所以,我的命令看起来像这样,
注意排除的文件/文件夹是相对于 tar 的根目录的(我已经尝试了相对于 / 的完整路径,但我无法使其工作)。
希望这会对某人有所帮助(下次我用谷歌搜索时也会对我有所帮助)
old question with many answers, but I found that none were quite clear enough for me, so I would like to add my try.
if you have the following structure
with following file/folders
so, you want to make a tar file that contain everyting inside /home/ftp/mysite (to move the site to a new server), but
file3
is just junk, and everything infolder3
is also not needed, so we will skip those two.we use the format
where the c = create, z = zip, and v = verbose (you can see the files as they are entered, usefull to make sure none of the files you exclude are being added). and f= file.
so, my command would look like this
note the files/folders excluded are relatively to the root of your tar (I have tried full path here relative to / but I can not make that work).
hope this will help someone (and me next time I google it)
您可以使用标准的“ant 表示法”来排除相对目录。
这对我有用,并且排除任何 .git 或 node_module 目录:
myInputFile.txt 包含:
/dev2/java
/dev2/javascript
You can use standard "ant notation" to exclude directories relative.
This works for me and excludes any .git or node_module directories:
myInputFile.txt contains:
/dev2/java
/dev2/javascript
此排除模式处理文件名后缀(如 png 或 mp3)以及目录名称(如 .git 和 node_modules)
This exclude pattern handles filename suffix like png or mp3 as well as directory names like .git and node_modules
我想展示另一个选项,我用来获得与之前提供的答案相同的结果,我有一个类似的情况,我想将 android studio 项目全部备份在 tar 文件中以上传到 media fire,使用 du 命令来查找大文件,我发现我不需要一些目录,例如:
构建、linux e .dart_tools
使用 Charles_ma 的第一个答案,我对其进行了一些修改,以便能够从 Android 目录的父目录运行命令。
它就像一个魅力。
诗。 抱歉,如果不允许这种答案,如果是这种情况,我将删除。
I'd like to show another option I used to get the same result as the answers before provide, I had a similar case where I wanted to backup android studio projects all together in a tar file to upload to media fire, using the du command to find the large files, I found that I didn't need some directories like:
build, linux e .dart_tools
Using the first answer of Charles_ma I modified it a little bit to be able to run the command from the parent directory of the my Android directory.
It worked like a charm.
Ps. Sorry if this kind of answer is not allowed, if this is the case I will remove.
我经历过这种情况,至少在我使用的 Cygwin 版本的 tar 中 ("CYGWIN_NT-5.1 1.7.17(0.262/5/3) 2012-10-19 14:39 i686 Cygwin “在 Windows XP Home Edition SP3 计算机上),选项的顺序很重要。
虽然这个结构对我有用:
那个没有工作:
这个,而
tar --help
揭示了以下内容:所以,第二个命令也应该工作,但显然情况似乎并非如此...
最好的rgds,
I've experienced that, at least with the Cygwin version of tar I'm using ("CYGWIN_NT-5.1 1.7.17(0.262/5/3) 2012-10-19 14:39 i686 Cygwin" on a Windows XP Home Edition SP3 machine), the order of options is important.
While this construction worked for me:
that one didn't work:
This, while
tar --help
reveals the following:So, the second command should also work, but apparently it doesn't seem to be the case...
Best rgds,
在阅读了不同版本的所有这些好答案并为自己解决了问题之后,我认为有一些非常重要的小细节,对于 GNU/Linux 的一般用途来说很少见,这些细节没有被强调足够了,值得更多的评论。
因此,我不会尝试回答每种情况的问题,而是尝试注册当事情不起作用时在哪里查找。
非常重要的是要注意:
就我而言,对于 Debian Stretch 上的 GNU tar 1.29,有效的命令是
引号并不重要,有或没有它们都可以。
我希望这对某人有用。
After reading all this good answers for different versions and having solved the problem for myself, I think there are very small details that are very important, and rare to GNU/Linux general use, that aren't stressed enough and deserves more than comments.
So I'm not going to try to answer the question for every case, but instead, try to register where to look when things doesn't work.
IT IS VERY IMPORTANT TO NOTICE:
In my case, for GNU tar 1.29 on Debian stretch, the command that worked was
The quotes didn't matter, it worked with or without them.
I hope this will be useful to someone.
如果您尝试排除版本控制系统 (VCS) 文件,tar 已经支持两个有趣的选项! :)
此选项排除以下版本控制系统使用的文件和目录:
CVS
、RCS
、SCCS
、SVN
、Arch
、Bazaar
、Mercurial
和Darcs
>。从版本 1.32 开始,排除以下文件:
CVS/
及其RCS/
下的所有内容以及SCCS/
下的所有内容,以及 下的所有内容.git/
.gitignore
.gitmodules
.gitattributes
.cvsignore 下的所有内容
.svn/
及其下的所有内容.arch-ids/
及其下的所有内容{arch}/
以及 其下的所有内容在其下=RELEASE-ID
=meta-update
=update
.bzr
.bzrignore
>.bzrtags
.hg
.hgignore
.hgrags
_darcs
当归档某些版本控制系统 (VCS) 下的目录时,读取排除模式通常很方便来自此 VCS 的忽略文件(例如
.cvsignore
、.gitignore
等)此选项提供了这种可能性。在归档目录之前,请查看它是否包含以下任何文件:
cvsignore
、.gitignore
、.bzrignore
或.hgignore
。 如果是这样,请从这些文件中读取忽略模式。这些模式的处理方式与相应的 VCS 的处理方式大致相同,即:
.cvsignore
包含仅适用于该文件所在目录的 shell 样式通配模式。 文件中不允许发表任何评论。 空行将被忽略。
.gitignore
包含 shell 风格的通配模式。 适用于
.gitfile
所在目录及其所有子目录。任何以
#
开头的行都是注释。 反斜杠转义注释字符。.bzrignore
包含 shell 通配模式和正则表达式(如果以
RE:(16) 为前缀。模式影响目录及其所有子目录。
以
开头的任何行>#
是注释.hgignore
包含 posix 正则表达式(17)。
syntax: glob
行切换到 shell 通配模式。 注释模式会影响目录及其所有子目录。:regexp 切换回来。以
#
开头的 忽略 -f path/to/my-tar-file.tar.gz path/to/my/project/If you are trying to exclude Version Control System (VCS) files, tar already supports two interesting options about it! :)
This option excludes files and directories used by following version control systems:
CVS
,RCS
,SCCS
,SVN
,Arch
,Bazaar
,Mercurial
, andDarcs
.As of version 1.32, the following files are excluded:
CVS/
, and everything under itRCS/
, and everything under itSCCS/
, and everything under it.git/
, and everything under it.gitignore
.gitmodules
.gitattributes
.cvsignore
.svn/
, and everything under it.arch-ids/
, and everything under it{arch}/
, and everything under it=RELEASE-ID
=meta-update
=update
.bzr
.bzrignore
.bzrtags
.hg
.hgignore
.hgrags
_darcs
When archiving directories that are under some version control system (VCS), it is often convenient to read exclusion patterns from this VCS' ignore files (e.g.
.cvsignore
,.gitignore
, etc.) This option provide such possibility.Before archiving a directory, see if it contains any of the following files:
cvsignore
,.gitignore
,.bzrignore
, or.hgignore
. If so, read ignore patterns from these files.The patterns are treated much as the corresponding VCS would treat them, i.e.:
.cvsignore
Contains shell-style globbing patterns that apply only to the directory where this file resides. No comments are allowed in the file. Empty lines are ignored.
.gitignore
Contains shell-style globbing patterns. Applies to the directory where
.gitfile
is located and all its subdirectories.Any line beginning with a
#
is a comment. Backslash escapes the comment character..bzrignore
Contains shell globbing-patterns and regular expressions (if prefixed with
RE:
(16). Patterns affect the directory and all its subdirectories.Any line beginning with a
#
is a comment..hgignore
Contains posix regular expressions(17). The line
syntax: glob
switches to shell globbing patterns. The linesyntax: regexp
switches back. Comments begin with a#
. Patterns affect the directory and all its subdirectories.tar -czv --exclude-vcs --exclude-vcs-ignores -f path/to/my-tar-file.tar.gz path/to/my/project/
我在其他地方发现了这个,所以我不会相信,但对于我的 mac 特定问题,它比上面的任何解决方案都有效(即使它已关闭):
I found this somewhere else so I won't take credit, but it worked better than any of the solutions above for my mac specific issues (even though this is closed):
对于 Mac OSX,我必须执行
tar -zcv --exclude='folder' -f theOutputTarFile.tarfolderToTar
请注意
--exclude= 之后的
-f
For Mac OSX I had to do
tar -zcv --exclude='folder' -f theOutputTarFile.tar folderToTar
Note the
-f
after the--exclude=
对于那些有问题的人来说,某些版本的 tar 只有在排除值中没有“./”的情况下才能正常工作。
有效的命令语法:
这些无效:
For those who have issues with it, some versions of tar would only work properly without the './' in the exclude value.
Command syntax that work:
These will not work:
我同意 --exclude 标志是正确的方法。
对于我没有立即发现明显的副作用的警告:
本例中排除“fileA”将递归地搜索“fileA”!
示例:具有单个子目录的目录,其中包含同名文件 (data.txt)
如果使用 < code>--exclude='data.txt' 存档将不包含 EITHER data.txt 文件。 如果存档第三方库(例如 node_modules 目录),这可能会导致意外结果。
要避免此问题,请确保提供完整路径,例如
--exclude='./dirA/data.txt'
I agree the --exclude flag is the right approach.
A word of warning for a side effect that I did not find immediately obvious:
The exclusion of 'fileA' in this example will search for 'fileA' RECURSIVELY!
Example:A directory with a single subdirectory containing a file of the same name (data.txt)
If using
--exclude='data.txt'
the archive will not contain EITHER data.txt file. This can cause unexpected results if archiving third party libraries, such as a node_modules directory.To avoid this issue make sure to give the entire path, like
--exclude='./dirA/data.txt'
读完这篇文章后,我在 RHEL 5 上做了一些测试,以下是我对 abc 目录进行压缩的结果:
这将排除目录错误和日志以及目录下的所有文件:
在排除的目录后添加通配符将排除文件但保留目录:
After reading this thread, I did a little testing on RHEL 5 and here are my results for tarring up the abc directory:
This will exclude the directories error and logs and all files under the directories:
Adding a wildcard after the excluded directory will exclude the files but preserve the directories:
避免由于使用
find ... | 可能出现的
当处理数万个文件时,您可以使用'xargs: Argument list too long'
错误 xargs ...find ... -print0 将
。find
的输出直接通过管道传输到tar
| tar --null ...To avoid possible
'xargs: Argument list too long'
errors due to the use offind ... | xargs ...
when processing tens of thousands of files, you can pipe the output offind
directly totar
usingfind ... -print0 | tar --null ...
.将 find 命令与 tar 附加 (-r) 选项结合使用。 这样,您可以一步将文件添加到现有 tar,而不是两遍解决方案(创建文件列表,创建 tar)。
Use the find command in conjunction with the tar append (-r) option. This way you can add files to an existing tar in a single step, instead of a two pass solution (create list of files, create tar).
您可以使用 cpio(1) 创建 tar 文件。 cpio 将文件归档到标准输入上,因此,如果您已经找到要用于选择归档文件的 find 命令,请将其通过管道传输到 cpio 以创建 tar 文件:
You can use cpio(1) to create tar files. cpio takes the files to archive on stdin, so if you've already figured out the find command you want to use to select the files the archive, pipe it into cpio to create the tar file:
您还可以根据需要使用“--exclude-tag”选项之一:
托管指定文件的文件夹文件将被排除。
You can also use one of the "--exclude-tag" options depending on your needs:
The folder hosting the specified FILE will be excluded.
gnu tar v 1.26 --exclude 需要位于存档文件和备份目录参数之后,不应有前导或尾随斜杠,并且最好不使用引号(单引号或双引号)。 因此相对于要备份的 PARENT 目录,它是:
tar cvfz /path_to/mytar.tgz ./dir_to_backup --exclude=some_path/to_exclude
gnu tar v 1.26 the --exclude needs to come after archive file and backup directory arguments, should have no leading or trailing slashes, and prefers no quotes (single or double). So relative to the PARENT directory to be backed up, it's:
tar cvfz /path_to/mytar.tgz ./dir_to_backup --exclude=some_path/to_exclude
-X 表示包含必须从备份中排除的文件名列表的文件。 例如,您可以在此文件中指定 *~,以便备份中不包含任何以 ~ 结尾的文件名。
-X indicates a file which contains a list of filenames which must be excluded from the backup. For Instance, you can specify *~ in this file to not include any filenames ending with ~ in the backup.
成功案例:
1) 如果给出完整路径进行备份,则在排除中也应使用完整路径。
tar -zcvf /opt/ABC/BKP_27032020/backup_27032020.tar.gz --exclude='/opt/ABC/csv/' --exclude='/opt/ABC/log/' /opt /ABC
2) 如果给当前路径进行备份,则排除中也应仅使用当前路径。
tar -zcvf backup_27032020.tar.gz --exclude='ABC/csv/' --exclude='ABC/log/' ABC
失败案例:
如果提供当前路径目录进行备份并要忽略的完整路径,则不起作用
tar -zcvf /opt/ABC/BKP_27032020/backup_27032020.tar.gz --exclude='/opt/ABC/csv/' --exclude='/opt/ABC/log/ ' ABC
注意:在备份目录之前/之后提及排除即可。
Success Case:
1) if giving full path to take backup, in exclude also should be used full path.
tar -zcvf /opt/ABC/BKP_27032020/backup_27032020.tar.gz --exclude='/opt/ABC/csv/' --exclude='/opt/ABC/log/' /opt/ABC
2) if giving current path to take backup, in exclude also should be used current path only.
tar -zcvf backup_27032020.tar.gz --exclude='ABC/csv/' --exclude='ABC/log/' ABC
Failure Case:
if giving currentpath directory to take backup and full path to ignore,then wont work
tar -zcvf /opt/ABC/BKP_27032020/backup_27032020.tar.gz --exclude='/opt/ABC/csv/' --exclude='/opt/ABC/log/' ABC
Note: mentioning exclude before/after backup directory is fine.
似乎不可能排除具有绝对路径的目录。
一旦任何路径是绝对路径(源或/和排除),排除命令将不起作用。 这是我尝试了所有可能的组合后的经验。
It seems to be impossible to exclude directories with absolute paths.
As soon as ANY of the paths are absolute (source or/and exclude) the exclude command will not work. That's my experience after trying all possible combinations.
一探究竟
Check it out
我想在本地主机上有新的前端版本(角度文件夹)。
另外,就我而言,git 文件夹很大,我想排除它。
我需要从服务器下载它,并将其解压才能运行应用程序。
从 /var/lib/tomcat7/webapps 压缩 Angular 文件夹,将其移动到名为 angular.23.12.19.tar.gz 的 /tmp 文件夹
命令:
I want to have fresh front-end version (angular folder) on localhost.
Also, git folder is huge in my case, and I want to exclude it.
I need to download it from server, and unpack it in order to run application.
Compress angular folder from /var/lib/tomcat7/webapps, move it to /tmp folder with name angular.23.12.19.tar.gz
Command :
最好的选择是通过 xargs 将 find 与 tar 结合使用(以处理大量参数)。 例如:
Your best bet is to use find with tar, via xargs (to handle the large number of arguments). For example:
可能是多余的答案,但因为我发现它有用,所以它是:
虽然 FreeBSD 根(即使用 csh)我想将整个根文件系统复制到 /mnt 但没有 /usr 和(显然)/mnt。 这就是有效的(我在 / ):
我的全部观点是,有必要(通过放置 ./)指定来 tar 排除的目录所在的部分正在复制的更大目录。
我的 €0.02
Possible redundant answer but since I found it useful, here it is:
While a FreeBSD root (i.e. using csh) I wanted to copy my whole root filesystem to /mnt but without /usr and (obviously) /mnt. This is what worked (I am at /):
My whole point is that it was necessary (by putting the ./) to specify to tar that the excluded directories where part of the greater directory being copied.
My €0.02
我没有运气让 tar 排除几层深度的 5 GB 子目录。 最后,我只是使用了unix Zip命令。 这对我来说容易多了。
因此,对于原始帖子中的这个特定示例
(tar --exclude='./folder' --exclude='./upload/folder2' -zcvf /backup/filename.tgz 。)
等价的是:
(注意:这是我最初使用的帮助我的帖子 https://superuser.com/questions/312301/unix-zip-directory-but-excluded-specific-subdirectories-and-everything -在-t内)
I had no luck getting tar to exclude a 5 Gigabyte subdirectory a few levels deep. In the end, I just used the unix Zip command. It worked a lot easier for me.
So for this particular example from the original post
(tar --exclude='./folder' --exclude='./upload/folder2' -zcvf /backup/filename.tgz . )
The equivalent would be:
(NOTE: Here is the post I originally used that helped me https://superuser.com/questions/312301/unix-zip-directory-but-excluded-specific-subdirectories-and-everything-within-t)
我从来没有让
tar --exclude
选项对我有用。 在我的例子中,使用 rsync 将文件夹树复制到新位置,然后使用标准 tar 工作。I've never made
tar --exclude
option work for me. In my case usingrsync
to copy folder tree to new location and then using standardtar
worked.也可能使用 cat/xargs/echo 来模拟 --exclude-from=file 选项作为 rsync:
eval
和 {} 周围的单引号用于特殊名称(例如包含空格)。 如果您的文件夹名称中没有特殊字符,您可以跳过 eval 和单引号。Possibly also use cat/xargs/echo to emulate the --exclude-from=file option as rsync:
eval
and single quotes around {} are used for special names (containing spaces, for example). If your folders do not have special chars in the name, you can skip the eval and the single quotes.