When creating TAR archives in Unix/Linux environments, we often need to exclude specific files matching certain patterns. The --exclude
flag in tar commands accepts glob patterns by default, but many developers need more powerful regular expression matching.
The command you tried:
tar cvf test.tar --exclude=[0-9]+x[0-9X]+\.jpg /data/foto
fails because tar's --exclude
parameter doesn't natively support full regex syntax. It uses shell globbing patterns instead.
Here are three reliable methods to achieve regex-based exclusion:
Method 1: Using find with tar
find /data/foto -type f ! -regex '.*/[0-9]+x[0-9X]+\.jpg' -print0 | \
tar --null -T - -cvf test.tar
Method 2: GNU tar with --exclude-vcs (for simple patterns)
tar cvf test.tar --exclude='*[0-9]x[0-9X]*.jpg' /data/foto
Method 3: Using grep with tar
tar cvf test.tar /data/foto | \
grep -vE '[0-9]+x[0-9X]+\.jpg'
For complex scenarios, combine multiple exclude patterns:
tar cvf archive.tar \
--exclude='*[0-9]x[0-9]*.jpg' \
--exclude='*test*.tmp' \
--exclude='backup*' \
/data
When dealing with large directories:
- The find+tar method is most efficient for complex regex
- Simple glob patterns execute faster with native tar --exclude
- Consider using
--exclude-from=FILE
for many patterns
To exclude all image files matching dimension patterns (e.g., 640x480.jpg, 1024X768.png):
find /var/www/uploads -type f ! -regex '.*/[0-9]+[xX][0-9]+\.[jp][pn]g' -print0 | \
tar --null -T - -cvzf webassets.tar.gz
When creating TAR archives in Linux, you might need to exclude specific files matching certain patterns. Regular expressions (regex) provide a powerful way to define these exclusion rules, but getting the syntax right can be tricky.
The key issue with your initial attempt is that --exclude
uses shell glob patterns by default, not full regex. To use proper regex, you need:
tar --exclude-regex='[0-9]+x[0-9X]+\.jpg' -cvf archive.tar /data/foto
Here are some common exclusion patterns:
# Exclude all temporary files
tar --exclude-regex='.*\.tmp$' -cvf backup.tar /var/www
# Exclude files with specific dimensions in name
tar --exclude-regex='[0-9]+x[0-9]+\.(jpg|png)' -cvf images.tar /photos
# Exclude hidden files and directories
tar --exclude-regex='^\..*' -cvf project.tar /home/user/project
- Use single quotes around the regex pattern to prevent shell expansion
- The
--exclude-regex
option requires GNU tar (common on Linux) - Test your patterns with
ls | grep -E 'pattern'
first - Multiple exclusions can be combined:
--exclude-regex='pattern1' --exclude-regex='pattern2'
If you're using an older tar version without regex support, consider:
# Using find with tar
find /data/foto -type f ! -regex '.*[0-9]+x[0-9X]+\.jpg' -print0 | \
tar --null -T - -cvf archive.tar
This method gives you more control over the file selection process.
For large directories, regex exclusions can slow down the archiving process. In such cases, it might be more efficient to:
- First copy all files to a temporary directory, excluding unwanted ones
- Then create the tar archive from the filtered directory