Tar - exclude files

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?

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.txt</code>Append file names:<br> <code>abc<br> xyz<br> *.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