简体   繁体   English

Shell 命令到 tar 目录,不包括某些文件/文件夹

[英]Shell command to tar directory excluding certain files/folders

Is there a simple shell command/script that supports excluding certain files/folders from being archived?是否有一个简单的 shell 命令/脚本支持从归档中排除某些文件/文件夹?

I have a directory that need to be archived with a sub directory that has a number of very large files I do not need to backup.我有一个目录需要与一个子目录一起存档,该子目录包含许多我不需要备份的非常大的文件。

Not quite solutions:不完全解决方案:

The tar --exclude=PATTERN command matches the given pattern and excludes those files, but I need specific files & folders to be ignored (full file path), otherwise valid files might be excluded. tar --exclude=PATTERN命令匹配给定的模式并排除那些文件,但我需要忽略特定文件和文件夹(完整文件路径),否则可能会排除有效文件。

I could also use the find command to create a list of files and exclude the ones I don't want to archive and pass the list to tar, but that only works with for a small amount of files.我还可以使用 find 命令创建文件列表并排除我不想存档的文件并将列表传递给 tar,但这只适用于少量文件。 I have tens of thousands.我有几万个。

I'm beginning to think the only solution is to create a file with a list of files/folders to be excluded, then use rsync with --exclude-from=file to copy all the files to a tmp directory, and then use tar to archive that directory.我开始认为唯一的解决方案是创建一个包含要排除的文件/文件夹列表的文件,然后使用 rsync 和--exclude-from=file将所有文件复制到 tmp 目录,然后使用 tar归档该目录。

Can anybody think of a better/more efficient solution?有人能想到更好/更有效的解决方案吗?

EDIT: Charles Ma 's solution works well.编辑: Charles Ma的解决方案效果很好。 The big gotcha is that the --exclude='./folder' MUST be at the beginning of the tar command.最大的问题是--exclude='./folder'必须位于 tar 命令的开头。 Full command (cd first, so backup is relative to that directory):完整命令(首先是 cd,因此备份是相对于该目录的):

cd /folder_to_backup
tar --exclude='./folder' --exclude='./upload/folder2' -zcvf /backup/filename.tgz .

You can have multiple exclude options for tar so您可以为 tar 设置多个排除选项,因此

$ tar --exclude='./folder' --exclude='./upload/folder2' -zcvf /backup/filename.tgz .

etc will work.等会起作用。 Make sure to put --exclude before the source and destination items.确保--exclude源和目标项目之前

You can exclude directories with --exclude for tar.您可以使用--exclude for tar 排除目录。

If you want to archive everything except /usr you can use:如果要存档除/usr之外的所有内容,可以使用:

tar -zcvf /all.tgz / --exclude=/usr

In your case perhaps something like在你的情况下,也许像

tar -zcvf archive.tgz arc_dir --exclude=dir/ignore_this_dir

Possible options to exclude files/directories from backup using tar:使用 tar 从备份中排除文件/目录的可能选项:

Exclude files using multiple patterns使用多种模式排除文件

tar -czf backup.tar.gz --exclude=PATTERN1 --exclude=PATTERN2 ... /path/to/backup

Exclude files using an exclude file filled with a list of patterns使用填充了模式列表的排除文件排除文件

tar -czf backup.tar.gz -X /path/to/exclude.txt /path/to/backup

Exclude files using tags by placing a tag file in any directory that should be skipped通过将标记文件放置在应跳过的任何目录中来排除使用标记的文件

tar -czf backup.tar.gz --exclude-tag-all=exclude.tag /path/to/backup

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如果你有以下结构

/home/ftp/mysite/

with following file/folders带有以下文件/文件夹

/home/ftp/mysite/file1
/home/ftp/mysite/file2
/home/ftp/mysite/file3
/home/ftp/mysite/folder1
/home/ftp/mysite/folder2
/home/ftp/mysite/folder3

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.因此,您想制作一个包含 /home/ftp/mysite 中所有内容的 tar 文件(将站点移动到新服务器),但file3只是垃圾,并且folder3所有内容也都不需要,因此我们将跳过这些二。

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).其中 c = create、z = zip 和 v = verbose(您可以看到输入的文件,这有助于确保没有添加您排除的文件)。 and f= file.和 f= 文件。

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).请注意,排除的文件/文件夹相对于您的 tar 的根目录(我在这里尝试了相对于 / 的完整路径,但我无法使其工作)。

hope this will help someone (and me next time I google it)希望这会帮助某人(还有我下次谷歌搜索时)

You can use standard "ant notation" to exclude directories relative.您可以使用标准的“蚂蚁符号”来排除相对目录。
This works for me and excludes any .git or node_module directories:这对我有用,不包括任何 .git 或 node_module 目录:

tar -cvf myFile.tar --exclude=**/.git/* --exclude=**/node_modules/*  -T /data/txt/myInputFile.txt 2> /data/txt/myTarLogFile.txt

myInputFile.txt contains: myInputFile.txt 包含:

/dev2/java /dev2/java
/dev2/javascript /dev2/javascript

这个排除模式处理像 png 或 mp3 这样的文件名后缀以及像 .git 和 node_modules 这样的目录名

tar --exclude={*.png,*.mp3,*.wav,.git,node_modules} -Jcf ${target_tarball}  ${source_dirname}

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.我已经经历过,至少在我使用的Cygwin版本的 tar 中 ("CYGWIN_NT-5.1 1.7.17(0.262/5/3) 2012-10-19 14:39 i686 Cygwin" on Windows XP Home Edition SP3 机器),选项的顺序很重要。

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 --help显示以下内容:

tar [OPTION...] [FILE]

So, the second command should also work, but apparently it doesn't seem to be the case...所以,第二个命令也应该有效,但显然情况并非如此......

Best rgds,最好的 rgds,

我在其他地方发现了这个,所以我不会相信,但对于我的 mac 特定问题,它比上面的任何解决方案都更好(即使这是关闭的):

tar zc --exclude __MACOSX --exclude .DS_Store -f <archive> <source(s)>

For Mac OSX I had to do对于 Mac OSX 我必须做

tar -zcv --exclude='folder' -f theOutputTarFile.tar folderToTar

Note the -f after the --exclude=注意--exclude=后面的-f

For those who have issues with it, some versions of tar would only work properly without the './' in the exclude value.对于那些遇到问题的人来说,某些版本的 tar 只能在排除值中没有“./”的情况下才能正常工作。

Tar --version

tar (GNU tar) 1.27.1焦油(GNU 焦油)1.27.1

Command syntax that work:有效的命令语法:

tar -czvf ../allfiles-butsome.tar.gz * --exclude=acme/foo

These will not work:这些将不起作用:

$ tar -czvf ../allfiles-butsome.tar.gz * --exclude=./acme/foo
$ tar -czvf ../allfiles-butsome.tar.gz * --exclude='./acme/foo'
$ tar --exclude=./acme/foo -czvf ../allfiles-butsome.tar.gz *
$ tar --exclude='./acme/foo' -czvf ../allfiles-butsome.tar.gz *
$ tar -czvf ../allfiles-butsome.tar.gz * --exclude=/full/path/acme/foo
$ tar -czvf ../allfiles-butsome.tar.gz * --exclude='/full/path/acme/foo'
$ tar --exclude=/full/path/acme/foo -czvf ../allfiles-butsome.tar.gz *
$ tar --exclude='/full/path/acme/foo' -czvf ../allfiles-butsome.tar.gz *

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.在阅读了所有这些不同版本的好答案并为我自己解决了问题之后,我认为有一些非常重要的小细节,对于 GNU/Linux 的一般使用来说是很少见的,没有足够的压力,值得更多的评论。

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:请务必注意:

  1. THE ORDER OF THE OPTIONS MATTER: it is not the same put the --exclude before than after the file option and directories to backup.选项的顺序很重要:在文件选项和要备份的目录之前和之后放置 --exclude 是不一样的。 This is unexpected at least to me, because in my experience, in GNU/Linux commands, usually the order of the options doesn't matter.这至少对我来说是出乎意料的,因为根据我的经验,在 GNU/Linux 命令中,选项的顺序通常无关紧要。
  2. 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.不同的 tar 版本期望此选项的顺序不同:例如, @Andrew 的回答表明,在 GNU tar v 1.26 和 1.28 中,排除项排在最后,而在我的情况下,对于 GNU tar 1.29,则相反。
  3. THE TRAILING SLASHES MATTER : at least in GNU tar 1.29, it shouldn't be any .尾随斜线很重要:至少在 GNU tar 1.29 中,它不应该是任何.

In my case, for GNU tar 1.29 on Debian stretch, the command that worked was就我而言,对于 Debian 伸展上的 GNU tar 1.29,有效的命令是

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.引号无关紧要,无论有没有它们都可以使用。

I hope this will be useful to someone.我希望这对某人有用。

If you are trying to exclude Version Control System (VCS) files, tar already supports two interesting options about it!如果您试图排除版本控制系统 (VCS) 文件,tar 已经支持两个有趣的选项! :) :)

  1. Option : --exclude-vcs选项:-- exclude-vcs

This option excludes files and directories used by following version control systems: CVS , RCS , SCCS , SVN , Arch , Bazaar , Mercurial , and Darcs .此选项不包括以下版本控制系统使用的文件和目录: CVSRCSSCCSSVNArchBazaarMercurialDarcs

As of version 1.32, the following files are excluded:从 1.32 版开始,以下文件被排除在外:

  • CVS/ , and everything under it CVS/及其下的所有内容
  • RCS/ , and everything under it RCS/及其下的所有内容
  • SCCS/ , and everything under it SCCS/及其下的所有内容
  • .git/ , and everything under it .git/及其下的所有内容
  • .gitignore
  • .gitmodules
  • .gitattributes
  • .cvsignore
  • .svn/ , and everything under it .svn/及其下的所有内容
  • .arch-ids/ , and everything under it .arch-ids/及其下的所有内容
  • {arch}/ , and everything under it {arch}/ ,以及它下面的所有内容
  • =RELEASE-ID
  • =meta-update
  • =update
  • .bzr
  • .bzrignore
  • .bzrtags
  • .hg
  • .hgignore
  • .hgrags
  • _darcs

    1. Option : --exclude-vcs-ignores选项:-- 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 (eg .cvsignore , .gitignore , etc.) This option provide such possibility.在归档某个版本控制系统 (VCS) 下的目录时,从该 VCS 的忽略文件(例如.cvsignore.gitignore等)中读取排除模式通常很方便。此选项提供了这种可能性。

Before archiving a directory, see if it contains any of the following files: cvsignore , .gitignore , .bzrignore , or .hgignore .在归档目录之前,请查看它是否包含以下任何文件: cvsignore.gitignore.bzrignore.hgignore If so, read ignore patterns from these files.如果是这样,请从这些文件中读取忽略模式。

The patterns are treated much as the corresponding VCS would treat them, ie:模式的处理方式与相应的 VCS 处理方式相同,即:

.cvsignore

Contains shell-style globbing patterns that apply only to the directory where this file resides.包含仅适用于此文件所在目录的 shell 样式的通配模式。 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.适用于.gitfile所在目录及其所有子目录。

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.包含 shell globbing-patterns 和正则表达式(如果前缀为RE: (16)。模式影响目录及其所有子目录。

Any line beginning with a # is a comment.任何以#开头的行都是注释。

.hgignore

Contains posix regular expressions(17).包含 posix 正则表达式 (17)。 The line syntax: glob switches to shell globbing patterns.syntax: glob切换到 shell globbing 模式。 The line syntax: regexp switches back.syntax: regexp切换回来。 Comments begin with a # .注释以#开头。 Patterns affect the directory and all its subdirectories.模式影响目录及其所有子目录。

  1. Example例子

tar -czv --exclude-vcs --exclude-vcs-ignores -f path/to/my-tar-file.tar.gz path/to/my/project/

I agree the --exclude flag is the right approach.我同意 --exclude 标志是正确的方法。

$ 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!对我没有立即发现的副作用的警告:在此示例中排除 'fileA' 将重复搜索 'fileA'

Example:A directory with a single subdirectory containing a file of the same name (data.txt)示例:具有单个子目录的目录,其中包含一个同名文件 (data.txt)

data.txt
config.txt
--+dirA
  |  data.txt
  |  config.docx
  • If using --exclude='data.txt' the archive will not contain EITHER data.txt file.如果使用--exclude='data.txt'存档将不包含EITHER的data.txt文件。 This can cause unexpected results if archiving third party libraries, such as a node_modules directory.如果归档第三方库(例如 node_modules 目录),这可能会导致意外结果。

  • To avoid this issue make sure to give the entire path, like --exclude='./dirA/data.txt'为避免此问题,请确保提供完整路径,例如--exclude='./dirA/data.txt'

After reading this thread, I did a little testing on RHEL 5 and here are my results for tarring up the abc directory:阅读完这篇文章后,我对 RHEL 5 进行了一些测试,以下是我对 abc 目录进行 tar 压缩的结果:

This will exclude the directories error and logs and all files under the directories:这将排除目录错误和日志以及目录下的所有文件:

tar cvpzf abc.tgz abc/ --exclude='abc/error' --exclude='abc/logs'

Adding a wildcard after the excluded directory will exclude the files but preserve the directories:在排除目录后添加通配符将排除文件但保留目录:

tar cvpzf abc.tgz abc/ --exclude='abc/error/*' --exclude='abc/logs/*'

To avoid possible 'xargs: Argument list too long' errors due to the use of find ... | xargs ...为了避免由于使用find ... | xargs ...可能出现的'xargs: Argument list too long'错误find ... | xargs ... find ... | xargs ... when processing tens of thousands of files, you can pipe the output of find directly to tar using find ... -print0 | tar --null ... find ... | xargs ...文件的处理时,几万,你可以管的输出find直接tar使用find ... -print0 | tar --null ... find ... -print0 | tar --null ... . 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 -

You can also use one of the "--exclude-tag" options depending on your needs:您还可以根据需要使用“--exclude-tag”选项之一:

  • --exclude-tag=FILE --exclude-tag=文件
  • --exclude-tag-all=FILE --exclude-tag-all=文件
  • --exclude-tag-under=FILE --exclude-tag-under=文件

The folder hosting the specified FILE will be excluded.将排除托管指定 FILE 的文件夹。

Use the find command in conjunction with the tar append (-r) option.将 find 命令与 tar append (-r) 选项结合使用。 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).通过这种方式,您可以一步将文件添加到现有的 tar,而不是两遍的解决方案(创建文件列表,创建 tar)。

find /dir/dir -prune ... -o etc etc.... -exec tar rvf ~/tarfile.tar {} \;

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). gnu tar v 1.26 --exclude 需要在归档文件和备份目录参数之后,应该没有前导或尾部斜杠,并且不喜欢引号(单引号或双引号)。 So relative to the PARENT directory to be backed up, it's:所以相对于要备份的 PARENT 目录,它是:

tar cvfz /path_to/mytar.tgz ./dir_to_backup --exclude=some_path/to_exclude

You can use cpio(1) to create tar files.您可以使用 cpio(1) 创建 tar 文件。 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: cpio 将文件归档到标准输入中,因此如果您已经找到了要用于选择归档文件的 find 命令,请将其通过管道传输到 cpio 以创建 tar 文件:

find ... | cpio -o -H ustar | gzip -c > archive.tar.gz
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. -X 表示包含必须从备份中排除的文件名列表的文件。 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 cvpzf zip_folder.tgz . --exclude=./public --exclude=./tmp --exclude=./log --exclude=fileName

Success Case: 1) if giving full path to take backup, in exclude also should be used full path.成功案例: 1)如果给全路径备份,在排除也应该使用全路径。

tar -zcvf /opt/ABC/BKP_27032020/backup_27032020.tar.gz --exclude='/opt/ABC/csv/ ' --exclude='/opt/ABC/log/ ' /opt/ABC 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. 2) 如果给当前路径做备份,在排除中也应该只使用当前路径。

tar -zcvf backup_27032020.tar.gz --exclude='ABC/csv/ ' --exclude='ABC/log/ ' ABC tar -zcvf backup_27032020.tar.gz --exclude='ABC/csv/ ' --exclude='ABC/log/ ' ABC

Failure Case:失败案例:

  1. 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 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.注意:在备份目录之前/之后提到排除是可以的。

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.我想展示另一个选项,我用来获得与提供之前的答案相同的结果,我有一个类似的案例,我想使用du命令将 android studio 项目全部备份在一个 tar 文件中以上传到 media fire为了找到大文件,我发现我不需要一些目录,例如:build, linux e .dart_tools 使用 Charles_ma 的第一个答案我稍微修改了它,以便能够从我的父目录运行命令安卓目录。

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.对不起,如果不允许这种答案,如果是这种情况我会删除。

Possible redundant answer but since I found it useful, here it is:可能是多余的答案,但因为我发现它很有用,所以这里是:

While a FreeBSD root (ie using csh) I wanted to copy my whole root filesystem to /mnt but without /usr and (obviously) /mnt.虽然是 FreeBSD 根目录(即使用 csh),但我想将整个根文件系统复制到 /mnt 但没有 /usr 和(显然)/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.我的重点是有必要(通过放置./指定tar 排除目录,其中部分目录被复制。

My €0.02我的 0.02 欧元

I had no luck getting tar to exclude a 5 Gigabyte subdirectory a few levels deep.我没有运气让 tar 排除几级深的 5 GB 子目录。 In the end, I just used the unix Zip command.最后,我只是使用了 unix Zip 命令。 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 . ) (tar --exclude='./folder' --exclude='./upload/folder2' -zcvf /backup/filename.tgz .)

The equivalent would be:相当于:

zip -r /backup/filename.zip . zip -r /backup/filename.zip 。 -x upload/folder/**\\* upload/folder2/**\\* -x 上传/文件夹/**\\* 上传/文件夹 2/**\\*

(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 ) (注意:这是我最初使用的帮助我的帖子https://superuser.com/questions/312301/unix-zip-directory-but-excluded-specific-subdirectories-and-everything-within-t

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.另外,在我的情况下,git 文件夹很大,我想排除它。 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从 /var/lib/tomcat7/webapps 压缩 angular 文件夹,将其移动到名为 angular.23.12.19.tar.gz 的 /tmp 文件夹

Command :命令 :

tar --exclude='.git' -zcvf /tmp/angular.23.12.19.tar.gz /var/lib/tomcat7/webapps/angular/

Your best bet is to use find with tar, via xargs (to handle the large number of arguments).最好的办法是通过 xargs 将 find 与 tar 一起使用(以处理大量参数)。 For example:例如:

find / -print0 | xargs -0 tar cjf tarfile.tar.bz2

The following bash script should do the trick.以下 bash 脚本应该可以解决问题。 It uses the answer given here by Marcus Sundman.它使用了 Marcus Sundman 在此处给出的答案。

#!/bin/bash

echo -n "Please enter the name of the tar file you wish to create with out extension "
read nam

echo -n "Please enter the path to the directories to tar "
read pathin

echo tar -czvf $nam.tar.gz
excludes=`find $pathin -iname "*.CC" -exec echo "--exclude \'{}\'" \;|xargs`
echo $pathin

echo tar -czvf $nam.tar.gz $excludes $pathin

This will print out the command you need and you can just copy and paste it back in. There is probably a more elegant way to provide it directly to the command line.这将打印出您需要的命令,您只需将其复制并粘贴回即可。可能有一种更优雅的方式将其直接提供给命令行。

Just change *.CC for any other common extension, file name or regex you want to exclude and this should still work.只需将 *.CC 更改为您想要排除的任何其他常见扩展名、文件名或正则表达式,这应该仍然有效。

EDIT编辑

Just to add a little explanation;只是补充一点解释; find generates a list of files matching the chosen regex (in this case *.CC). find 生成与所选正则表达式匹配的文件列表(在本例中为 *.CC)。 This list is passed via xargs to the echo command.该列表通过 xargs 传递给 echo 命令。 This prints --exclude 'one entry from the list'.这会打印 --exclude 'one entry from the list'。 The slashes () are escape characters for the ' marks.斜线 () 是 ' 标记的转义字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM