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 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:
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
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop












{ 5 comments… read them below or add one }
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!
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@192.168.0.1 “cd /;nice -n 10 tar cvpP –exclude={”/proc/*”,”/sys*”,”/tmp/*”,”/home/user/*”} /”>backup.tar.gz
and before “exclude” we have two “-” not one.
Using the exclude.txt file is the coolest thing since sliced bread. Thank you very much!!
will:
you can use the ‘T’ option with ‘ -T selectedfiles.txt’ where selectedfiles.txt contains paths to the files you want to backup only.
there’s a typo at the beginning of your article: change “execute” to “exclude”