How To Tar Multiple Files In Linux – Using Wildcard Characters

Bundling multiple files together in Linux is accomplished by passing each filename as a separate argument to the `tar` command. If you’re wondering how to tar multiple files in linux, the process is straightforward once you understand the basic syntax. This guide will walk you through every step, from simple file bundling to advanced compression techniques.

Tar stands for “tape archive,” and it’s one of the oldest and most reliable tools in Unix-like systems. You can use it to combine several files into one archive file, which makes storage and transfer much easier. Let’s get started with the core commands and options.

How To Tar Multiple Files In Linux

To tar multiple files, you list them all after the archive name. The basic command structure is: tar -cf archive.tar file1 file2 file3. The -c flag creates a new archive, and -f specifies the archive filename. Here’s a simple example:

tar -cf myfiles.tar document.txt image.jpg script.sh

This command creates an archive called myfiles.tar containing the three specified files. You can add as many files as you need, just separate them with spaces. The original files remain untouched on your system.

Using Wildcards To Select Multiple Files

When you have many files with similar names, wildcards save time. The asterisk (*) matches any number of characters, while the question mark (?) matches a single character. For example:

tar -cf logs.tar *.log

This archives all files ending with .log in the current directory. You can combine wildcards with directories too:

tar -cf projects.tar project_*

This grabs every file or folder starting with “project_”. Wildcards are powerful but be careful—they expand to all matching files, which might be more than you intended.

Including Files From Multiple Directories

Tar can handle files from different directories without moving them. Just provide the full or relative paths:

tar -cf backup.tar /home/user/docs/report.txt /var/log/syslog

When you extract this archive, the files will restore with their original directory structure. If you want to strip the leading path, use the --strip-components option during extraction.

Adding Files To An Existing Archive

You can append new files to an existing tar archive using the -r flag. This is useful for incremental backups or adding late files:

tar -rf myfiles.tar newfile.txt

Note that this only works with uncompressed archives. If your archive is compressed (like .tar.gz), you’ll need to decompress it first, add files, then recompress.

Compressing The Archive

Tar archives are often compressed to save space. The most common compression methods are gzip (-z), bzip2 (-j), and xz (-J). Here’s how to create a gzip-compressed archive:

tar -czf myfiles.tar.gz file1 file2 file3

The -z flag pipes the archive through gzip. For bzip2 compression, use -j:

tar -cjf myfiles.tar.bz2 file1 file2 file3

And for xz compression, which offers the best compression ratio but takes longer:

tar -cJf myfiles.tar.xz file1 file2 file3

Compressed archives have different extensions, but tar handles them automatically when extracting.

Excluding Files From The Archive

Sometimes you want to archive most files but skip certain ones. The --exclude option lets you do that:

tar -cf backup.tar --exclude='*.tmp' --exclude='cache/' /home/user

This creates an archive of /home/user but excludes all .tmp files and the cache directory. You can use multiple --exclude flags, and they support wildcards.

Using A File List Instead Of Command-Line Arguments

If you have a long list of files, typing them all can be tedious. The -T option reads filenames from a text file:

tar -cf archive.tar -T filelist.txt

Each line in filelist.txt should contain one filename or path. This is perfect for automated scripts or when you need to archive the same set of files repeatedly.

Archiving An Entire Directory

To archive everything inside a directory, just specify the directory name:

tar -cf project.tar /home/user/myproject

This recursively includes all files and subdirectories. You can combine this with --exclude to skip certain subdirectories or file types.

Verifying The Archive Contents

Before extracting, you can list the contents of a tar archive without extracting it. Use the -t flag:

tar -tf myfiles.tar

This shows all files and directories inside the archive. For compressed archives, tar automatically detects the compression and lists the contents:

tar -tf myfiles.tar.gz

Extracting Specific Files From An Archive

You don’t have to extract the entire archive. To extract only certain files, list them at the end of the command:

tar -xf archive.tar file1.txt file2.txt

This extracts just file1.txt and file2.txt. The files must match exactly as they appear in the archive (check with -tf first).

Preserving File Permissions And Ownership

When creating archives for backup or transfer, you might want to preserve file permissions, ownership, and timestamps. The -p flag does this:

tar -cpf backup.tar /important/data

During extraction, use --same-permissions to restore them. For full preservation including ACLs and extended attributes, add --acls and --xattrs.

Handling Large Numbers Of Files

If you’re archiving thousands of files, the command line might get too long. In that case, use the -T option with a file list generated by find:

find /home/user -name "*.txt" > filelist.txt
tar -cf textfiles.tar -T filelist.txt

This finds all .txt files under /home/user and archives them. You can also pipe directly:

find /home/user -name "*.txt" -print0 | tar -cf textfiles.tar --null -T -

The -print0 and --null options handle filenames with spaces or special characters safely.

Common Mistakes And How To Avoid Them

One frequent error is forgetting the -f flag. Without it, tar tries to use a tape device. Another is using relative paths when you meant absolute paths, which can cause extraction issues. Always test your archive with -tf before distributing it.

Also, be aware that tar archives don’t compress by default. If you want compression, you must add the appropriate flag. And remember, the order of flags matters: -czf works, but -cfz might not.

Automating Tar With Scripts

You can wrap tar commands in bash scripts for regular backups. Here’s a simple example:

#!/bin/bash
DATE=$(date +%Y%m%d)
tar -czf "backup_$DATE.tar.gz" /home/user/documents

This creates a dated archive each time you run the script. Add it to cron for automatic backups.

Performance Considerations

For very large archives, compression can take significant time and CPU. Use -J (xz) for best compression but slowest speed, -j (bzip2) for a balance, and -z (gzip) for speed. If you don’t need compression, skip it entirely for faster archiving.

Network transfers benefit from compression, but local backups might not. Test different methods to find what works best for your workload.

Using Tar With Pipes

Tar can send archives directly to other programs via pipes. For example, to create an archive and encrypt it with GPG:

tar -cf - file1 file2 | gpg -c > archive.tar.gpg

The dash (-) tells tar to output to stdout. This is useful for streaming or further processing.

Recovering Corrupted Archives

If a tar archive gets damaged, you might still recover some files. Use the --ignore-zeros option to skip corrupted blocks:

tar -xif archive.tar

This ignores zeroed blocks and tries to extract whatever is readable. It’s not perfect, but it can save partial data.

Tar And Symlinks

By default, tar archives the files that symlinks point to, not the symlinks themselves. To archive the symlinks, use --dereference or -h:

tar -chf archive.tar /path/to/symlinks

This follows symlinks and archives the target files. Without it, tar archives the symlink as a symlink.

Cross-Platform Compatibility

Tar archives are widely compatible across Linux, macOS, and even Windows (with tools like 7-Zip). However, be cautious with special characters in filenames and long paths. Use --format=posix for maximum compatibility.

Advanced: Incremental Backups

Tar supports incremental backups using snapshot files. First, create a full backup with a snapshot:

tar -cg snapshot.file -f full.tar /home/user

Then, for subsequent backups, use the same snapshot file:

tar -cg snapshot.file -f incremental.tar /home/user

This only archives files that changed since the last snapshot. It’s efficient for regular backups but requires careful management of snapshot files.

FAQ: How To Tar Multiple Files In Linux

Can I tar files from different directories into one archive?

Yes, just list the full or relative paths to each file. They will be stored with their directory structure.

How do I exclude a specific file type when tarring?

Use the --exclude option with a wildcard, like --exclude='*.mp4'. You can use multiple exclude flags.

What’s the difference between tar.gz and tar.bz2?

Both are compressed archives, but tar.bz2 uses bzip2 compression, which offers better compression ratios at the cost of slower speed. tar.gz uses gzip and is faster.

Can I add files to an existing tar archive without recreating it?

Yes, use the -r flag to append files. This works only for uncompressed archives. For compressed ones, you must decompress, add files, and recompress.

How do I tar multiple files and compress them in one command?

Combine the -c flag with a compression flag like -z (gzip), -j (bzip2), or -J (xz). For example: tar -czf archive.tar.gz file1 file2.

Now you have a solid understanding of how to tar multiple files in linux. Practice with different options and soon you’ll be archiving like a pro. Remember to always verify your archives and test extraction before relying on them for backups.