Linux tar command: directory compression but subdirectory exclusion

Scenario: I have a directory on the server that hosts my site containing hundreds of images submitted by the user. I am backing up a script that extracts all the images from a directory and compresses them into a single .tar.gz file. The command I still have:

tar -czpf / path / to / backups / my_backup.tar.gz path / to / images /

Problem: Not inside my path / in / images / I have a directory tmp /. When I run the command, I get a .tar.gz file containing the entire image in the path / to / images / directory and the tmp / subdirectory.

Question . How can I make the command skip / not include the tmp / subdirectory in the .tar.gz file.

Thanks for the million in advance.

+5
source share
3 answers

You are looking for an argument --exclude.

--exclude=PATTERN

Some users find the excludeoptions confusing. Here are some common mistakes:

  • The main mode of operation tardoes not work by the path name is explicitly indicated on the command line if one of its components file name is excluded. In the above example, if you create an archive and exclude files ending in *.o, but explicitly called a file dir.o/fooafter all parameters have been specified, it dir.o/foowill be excluded from the archive.

  • --exclude=PATTERN --exclude-from=FILE-OF-PATTERNS (-X FILE-OF-PATTERNS). : --exclude=PATTERN, , , . --exclude-from=FILE-OF-PATTERNS , ; , .

  • --exclude=PATTERN, PATTERN , GNU tar , *. , *, , tar , . , .

, :

$ tar -c -f ARCHIVE.TAR --exclude '*.o' DIRECTORY

:

$ tar -c -f ARCHIVE.TAR --exclude *.o DIRECTORY
  • globbing, regexp tar. regexp , .
+12
tar -czpf /path/to/backups/my_backup.tar.gz --exclude path/to/images/tmp path/to/images/
+3

This should work (assuming GNU tar):

tar -czpf /path/to/backups/my_backup.tar.gz --exclude=tmp path/to/images/
0
source

All Articles