Compressing files into a single archive on Linux requires using the `tar` command with the appropriate flags. If you are wondering how to tar in linux, you have come to the right place. This guide walks you through everything from basic archiving to advanced compression techniques, all with simple, practical examples.
The `tar` command, short for “tape archive,” is a powerful tool for bundling files together. It does not compress by default, but you can add compression with options like `-z` for gzip or `-j` for bzip2. Let’s start with the fundamentals.
How To Tar In Linux
Before we dive into the commands, make sure you have a terminal open. You can use any Linux distribution, and the steps will be the same. The basic syntax is: `tar [options] [archive-name] [files-or-directories]`.
Creating A Basic Tar Archive
To create a tar file, use the `-c` (create) and `-f` (file) flags. For example, to archive a folder named “documents”:
tar -cf documents.tar documents/
This creates a file called `documents.tar`. The `-c` tells tar to create, and `-f` specifies the archive name. You can list multiple files or directories:
tar -cf backup.tar file1.txt file2.txt folder1/
Adding Compression To Your Archive
Most users want compressed archives to save space. Here are the common compression options:
- Gzip compression: Use `-z` flag. Creates `.tar.gz` or `.tgz` files.
- Bzip2 compression: Use `-j` flag. Creates `.tar.bz2` files. Better compression but slower.
- Xz compression: Use `-J` flag. Creates `.tar.xz` files. Highest compression ratio.
Example with gzip:
tar -czf documents.tar.gz documents/
For bzip2:
tar -cjf documents.tar.bz2 documents/
Extracting Tar Archives
To extract an archive, use the `-x` (extract) flag. Always include `-f` to specify the file:
tar -xf documents.tar
For compressed archives, tar automatically detects the compression type in most modern versions:
tar -xf documents.tar.gz
You can extract to a specific directory using `-C` (capital C):
tar -xf documents.tar.gz -C /home/user/extracted/
Listing Archive Contents Without Extracting
Use the `-t` flag to see what is inside an archive:
tar -tf documents.tar
This shows all files and folders. For compressed archives, it works the same way:
tar -tf documents.tar.gz
Verbose Output For Progress
Add the `-v` flag to see files being processed. This is helpful for large archives:
tar -cvzf documents.tar.gz documents/
You will see each file name printed as it is added. The same works for extraction:
tar -xvzf documents.tar.gz
Common Tar Operations With Examples
Appending Files To An Existing Archive
Use the `-r` flag to add files to an existing tar archive. This only works with uncompressed archives:
tar -rf documents.tar newfile.txt
You cannot append to compressed archives directly. You would need to extract, add the file, and re-compress.
Excluding Specific Files Or Directories
Use the `–exclude` option to skip certain files. For example, exclude all `.log` files:
tar -czf backup.tar.gz /home/user/ --exclude="*.log"
You can also exclude entire directories:
tar -czf backup.tar.gz /home/user/ --exclude="temp/"
Using Wildcards With Tar
Tar supports wildcards for selecting files. Use the `–wildcards` flag:
tar -czf textfiles.tar.gz --wildcards "*.txt"
This archives all `.txt` files in the current directory. You can combine this with exclude patterns.
Preserving File Permissions And Ownership
Use the `-p` flag to preserve permissions when extracting. This is crucial for system backups:
tar -xpf backup.tar
To preserve ownership, you may need root privileges. Use `sudo`:
sudo tar -xpf backup.tar
Compressing Multiple Directories Into One Archive
You can list several directories:
tar -czf combined.tar.gz /var/log/ /etc/ /home/user/
This creates a single archive containing all three directories. Be careful with large amounts of data.
Advanced Tar Techniques
Incremental Backups With Tar
Tar can perform incremental backups using snapshot files. First, create a full backup with a snapshot:
tar -czg snapshot.file -f full_backup.tar.gz /home/user/
For subsequent backups, use the same snapshot file to only archive changed files:
tar -czg snapshot.file -f incremental_backup.tar.gz /home/user/
This saves time and space. Restore by extracting the full backup first, then the incremental ones.
Archiving Over SSH
You can create archives remotely using SSH. Pipe the tar output through SSH:
tar -czf - /home/user/ | ssh user@remote "cat > backup.tar.gz"
This sends the archive directly to a remote server. You can also extract remotely:
ssh user@remote "tar -czf - /home/user/" > local_backup.tar.gz
Using Tar With Find Command
Combine `find` with `tar` to archive specific files. For example, archive all files modified in the last 7 days:
find /home/user/ -mtime -7 -type f -print0 | tar -czf recent_files.tar.gz --null -T -
The `-print0` and `–null` handle filenames with spaces. The `-T -` reads filenames from stdin.
Compressing With Different Levels
Gzip and bzip2 allow compression levels from 1 (fast) to 9 (best). Use the `–gzip` or `–bzip2` option with a number:
tar -czf --gzip --fast archive.tar.gz files/
Or for maximum compression:
tar -czf --gzip --best archive.tar.gz files/
Note that some tar versions use `-I` for custom compressors. Check your man page.
Common Tar Errors And Solutions
“Cannot Open: No Such File Or Directory”
This usually means the archive name or path is wrong. Double-check your spelling. Use absolute paths if needed:
tar -czf /tmp/backup.tar.gz /home/user/documents/
“Cannot Stat: No Such File Or Directory”
This error means a file in the list does not exist. Verify the file paths. Use wildcards carefully.
“Archive Contains Entries With Leading ‘/’ ” Warning
Tar warns when you use absolute paths. This is normal but can cause issues on extraction. Use relative paths or strip leading slashes with `–strip-components`:
tar -czf backup.tar.gz --strip-components=1 /home/user/documents/
“Gzip: Stdin: Not In Gzip Format”
This happens when you try to decompress a file that is not gzip compressed. Check the file extension. Use `file` command to identify the type:
file archive.tar.gz
Best Practices For Using Tar
- Always verify archives: Use `-t` to list contents after creation.
- Use compression wisely: Gzip is fast, bzip2 compresses better, xz is best but slow.
- Keep backups separate: Store archives on different drives or cloud storage.
- Test extraction: Extract to a temporary directory to ensure the archive is valid.
- Use verbose mode for large jobs: The `-v` flag helps track progress.
Automating Tar With Cron Jobs
Schedule regular backups using cron. Edit your crontab with `crontab -e` and add a line:
0 2 * * * tar -czf /backups/daily_$(date +\%Y\%m\%d).tar.gz /home/user/
This runs daily at 2 AM. Adjust the path and schedule as needed.
Frequently Asked Questions
What is the difference between tar and gzip?
Tar bundles files into one archive, while gzip compresses a single file. They are often used together: tar creates the archive, gzip compresses it. The `-z` flag combines them.
Can I tar a directory without including its parent path?
Yes, use the `-C` option to change directory before archiving. For example: tar -czf archive.tar.gz -C /home/user/ documents/ will store only the “documents” folder without the full path.
How do I extract a single file from a tar archive?
Specify the file name after the archive. Use `-x` and list the file: tar -xf archive.tar.gz file1.txt. You can also use wildcards: tar -xf archive.tar.gz --wildcards "*.txt".
Is it possible to update an existing tar archive?
Yes, but only for uncompressed archives. Use the `-u` flag to update files that are newer than those in the archive: tar -uf archive.tar file1.txt. For compressed archives, you must recreate them.
What does the `-v` flag do in tar?
The `-v` flag enables verbose mode, which prints each file name as it is processed. This is useful for monitoring progress and verifying operations.
Conclusion
Mastering how to tar in linux is a valuable skill for any system administrator or power user. The command is flexible, reliable, and essential for backups, file transfers, and archiving. Start with simple commands, then explore advanced features like incremental backups and remote archiving. Practice with test files to build confidence. Remember to always verify your archives and keep your data safe. With these techniques, you can efficiently manage your files and backups on any Linux system.
Now you have a solid understanding of tar. Go ahead and try creating your first archive. You will find it becomes second nature after a few uses. Happy archiving!