Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
608 views
in Technique[技术] by (71.8m points)

linux - 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 .
  ask by deepwell translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...