Exclude Certain Files When Creating A Tarball Using Tar Command

by on October 18, 2006 · 6 comments· LAST UPDATED August 23, 2012

in , ,

How can I keep out certain files when creating a tarball? For example:


How do I exclude zyz and abc file while using a tar command?

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:

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


  • -X file.txt :exclude files matching patterns listed in FILE file.txt
Tweet itFacebook itG+ itDownload PDF versionFound an error/typo on this page?

{ 6 comments… read them below or add one }

1 will August 4, 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!


2 aceqbaceq March 13, 2009 at 1:30 pm

common style using “tar+exclude” suggested in internet sources is like:

tar \
– -exclude… \
– -exclude… \
– -exclude… \

– -exclude… \
-cvpzf home_bup.tgz /home/username

but i think it is a very annoying method.

I suggest next:

for fedora core

tar cvpzPf /tmp/backup.tar.gz –exclude={/proc/*,/sys/*,/tmp/*,/dev/*} /

for Debian

tar cvfpP /tmp/debian2.tar –exclude={”/proc/*”,”/sys*”,”/tmp/*”,”/home/user/*”} /

and more common if i need backup using ssh (ssh+nice+tar)

ssh root@ “cd /;nice -n 10 tar cvpP –exclude={”/proc/*”,”/sys*”,”/tmp/*”,”/home/user/*”} /”>backup.tar.gz

and before “exclude” we have two “-” not one.


3 Tokyo Joe June 24, 2009 at 9:02 am

Using the exclude.txt file is the coolest thing since sliced bread. Thank you very much!!


4 cmo-0 September 2, 2009 at 8:13 pm


you can use the ‘T’ option with ‘ -T selectedfiles.txt’ where selectedfiles.txt contains paths to the files you want to backup only.


5 chz January 13, 2012 at 7:41 pm

there’s a typo at the beginning of your article: change “execute” to “exclude”


6 Pavche January 15, 2015 at 2:35 pm

Thanks a lot for the exclude list.


Leave a Comment

Tagged as: , , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: