Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

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.

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. 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.

Can anybody think of a better/more efficient solution?

EDIT: Charles Ma's solution works well. The big gotcha is that the --exclude='./folder' MUST be at the beginning of the tar command. Full command (cd first, so backup is relative to that directory):

cd /folder_to_backup tar --exclude='./folder' --exclude='./upload/folder2' -zcvf /backup/filename.tgz . 
like image 451
deepwell Avatar asked Jun 11 '09 22:06

deepwell


People also ask

How do I tar exclude a folder?

You can exclude directories with --exclude for tar. To clarify, you can use full path for --exclude. Beware that --exclude=dir/ignore_this_dir will match in any subtree as well! You'll end up missing files you didn't expect to be excluded.

How does tar -- exclude work?

The ' --exclude= pattern ' option prevents any file or member whose name matches the shell wildcard ( pattern ) from being operated on. For example, to create an archive with all the contents of the directory 'src' except for files whose names end in '.o', use the command ' tar -cf src. tar --exclude='*.o' src '.


2 Answers

You can have multiple exclude options for tar so

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

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

like image 120
Charles Ma Avatar answered Sep 20 '22 17:09

Charles Ma


You can exclude directories with --exclude for tar.

If you want to archive everything except /usr you can use:

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

In your case perhaps something like

tar -zcvf archive.tgz arc_dir --exclude=dir/ignore_this_dir 
like image 40
Johan Soderberg Avatar answered Sep 23 '22 17:09

Johan Soderberg