Exclude certain files when creating a tarball using tar command
Q. How can I keep out certain files when creating a tarball? For example
/home/me/file1
/home/me/dir1
/home/me/dir2
/home/me/abc
/home/me/xyz
How do I execute zyz and abc file while using a tar command?
A. The GNU version of the tar archiving utility has --exclude and -X options. So to exclude abc and xyz file you need to type the command as follows:
$ tar -zcvf /tmp/mybackup.tar.gz --exclude='abc' --exclude='xyz' /home/me
If you have more than 2 files use -X option to specify multiple file names. It reads list of exclude file names from a text file. For example create a file called exclude.txt:
$ vi exclude.txtAppend file names:
abc
xyz
*.bak
Save and close the file. This lists the file patterns that need to be excluded. Now type the command:
$ tar -zcvf /tmp/mybackup.tar.gz -X exclude.txt /home/me
Where,
- -X file.txt :exclude files matching patterns listed in FILE file.txt
E-mail this to a friend
Printable version
Related Other Helpful FAQs:
- How to install Linux / UNIX *.tar.gz tarball files
- Force yum update Command To Exclude Certain Packages
- Linux yum command skip updating packages
- How To Open .TBZ ( tar.bz2 ) File Under Linux / UNIX
- How To Extract a Single File / Directory from Tarball Archive
Discussion on This FAQ
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!



August 4th, 2008 at 8:13 pm
Is there a way to do the opposite? That is, tar up only files of a certain type and not everything else? Would like to preserve directory structure when doing so. Thanks!