Scenario: I have a directory on a server that hosts my website that contains hundreds of user-submitted images. I am creating a backup script that takes all the images from the directory and compresses them into one .tar.gz file. The command I have so far is:
tar -czpf /path/to/backups/my_backup.tar.gz path/to/images/
Problem: No inside my path/to/images/ I have a directory called tmp/. When I run the command, I get a .tar.gz file containing all the image in the path/to/images/ directory and a subdirectory called tmp/.
Question: How can I get the command to skip/not include the tmp/ subdirectory in the .tar.gz file.
Thanks a million in advance.
You are looking for the
--excludeargument.Some users find
excludeoptions confusing. Here are some common pitfalls:The main operating mode of
tardoes not act on a path nameexplicitly listed on the command line if one of its file name
components is excluded. In the example above, if you create an
archive and exclude files that end with
*.o, but explicitly namethe file
dir.o/fooafter all the options have been listed,dir.o/foowill be excluded from the archive.You can sometimes confuse the meanings of
--exclude=PATTERNand--exclude-from=FILE-OF-PATTERNS(-X FILE-OF-PATTERNS). Becareful: use
--exclude=PATTERNwhen files to be excluded aregiven as a pattern on the command line. Use
--exclude-from=FILE-OF-PATTERNSto introduce the name of a filewhich contains a list of patterns, one per line; each of these
patterns can exclude zero, one, or many files.
When you use
--exclude=PATTERN, be sure to quote the PATTERNparameter, so GNU
tarsees wildcard characters like*. If youdo not do this, the shell might expand the
*itself using filesat hand, so
tarmight receive a list of files instead of onepattern, or none at all, making the command somewhat illegal.
This might not correspond to what you want.
For example, write:
rather than:
regexpsyntax, when using exclude options in
tar. If you try to useregexpsyntax to describe files to be excluded, your commandmight fail.