About Linux FAQ

Browse More FAQs:

Exclude certain files when creating a tarball using tar command

Posted by Vivek Gite [Last updated: October 18, 2006]

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:

Discussion on This FAQ

  1. will Says:

    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!

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!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Copyright © 2006-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.