Exclude large files from your tar with --exclude

If you do pretty much anything with data, learning the tar command is going to be worth your while. But suppose you do things with lots and lots of heavy data?

Well, one trick you’ll want to add to your arsenal is the –-exclude option.

Let’s say you want to package up a website with a ton of images located in an image directory. But that there are so many images located in that image directory that it would take all night to tar it, so we’re talking REALLY big.

Where there’s a will, there’s a way. You can simply exclude the unwanted baggage and tar the rest by using --exclude.

It works like this…

Let’s say you want to tar an entire website located in /httpdocs, but you want to exclude the directory located at httpdocs/files/images

It’s really just this simple…

tar -pczf mysitename.tar.gz /httpdocs --exclude "/files/images"

This will tar the whole site (which is located in /httpdocs) into a tar.gz file called mysitename.tar.gz but won’t include the directory /httpdocs/files/images.

The same works for individual files. To exclude more than one file or directory, just add more -- excludes.

For reference, here’s a bit more about some of the more common .tar commands.

  • c – create a new archive.
  • t - list the contents of an archive.
  • x - extract the contents of an archive.
  • f - the archive file name is given on the command line (required whenever the tar output is going to a file)
  • p – preserves the permissions.
  • v - print verbose output (lists file names as they are processed).
  • u - add files to the archive if they are newer than the copy in the tar file.
  • z - compress or decompress files automatically.