Last night I was creating some manual backups from a server including an entire web sites data files as well as the databases associated with that same web site. I decided to use the tar archiving utility to pack all of the files from the web site into a single file so it could be downloaded and stored easily. Use the information below to exclude a sub directory when creating a tar archive.
Exclude A Sub Directory When Creating A Tar Archive:
The syntax below shows an example of using the tar command while excluding a directory called backups and all of the files and sub directories located underneath the backups directory.
- [root@dev data]# tar --exclude backups* -czvf entire-site.tar.gz website/
Notice that he –exclude switch is added before the other switches. The file that will be generated is called entire-site.tar.gz and the directory that is being added to the tar archive is called website.
Explanation Of Tar Switches Used In Above Command:
- -c – create a new archive
- -z – filter the archive through gzip
- -v – verbose
- -f – use archive file or device F (default “-“, meaning stdin/stdout)
Also note that the star after backups in the above example command is a wildcard and will match directories or files such as backups1, backups1234, or backups-old. It will also exclude any files or sub directories within any of those directories.