Linux Tip: Compression – tar and exclude

A lot of times I have to recursively compress a bunch of files and folders so I can ftp them to my local for perusal.

When I compress, I do this:


tar -cvzf localfolder.tar.gz localfolder

And that gives me a tar file of all the zipped stuff in localfolder.

To decompress it on the command line I do this:

tar -xvzf localfolder.tar.gz

But usually I am doing this in Windows with 7-Zip or Iceows in a GUI.

A little more tricky thing to do is to EXCLUDE something. Let’s say you have a folder tree like below:


/etc/localfolder
--|folder1
--|folder2
--|folder3

But you want to exclude folder2. The trick is to use full pathnames in the exclusion command or the exlclude won’t work:


tar -cvzf localfolder.tar.gz /etc/localfolder --exclude="/etc/localfolder/folder2"
Multiple Exlcudes:
tar -cvzf localfolder.tar.gz /etc/localfolder --exclude="/etc/localfolder/folder1" --exclude="/etc/localfolder/folder2"
With GNU tar, swap the exclude and the source directory:
tar -cvzf localfolder.tar.gz --exclude="/etc/localfolder/folder2" /etc/localfolder

Comments are closed.