The `tar` command bundles multiple files into a single archive for storage or transfer. If you are new to Linux, you might be wondering what is tar linux and how it can help you manage files efficiently. This command is a staple for system administrators and developers alike.
Tar stands for “tape archive,” a name from the days when data was backed up onto magnetic tapes. Today, it is used to create compressed archives, often called tarballs. You will see files with extensions like `.tar`, `.tar.gz`, or `.tar.bz2`.
In this guide, you will learn everything about tar on Linux. We will cover basic usage, common options, and practical examples. By the end, you will feel confident using tar for backups and file transfers.
What Is Tar Linux
At its core, tar is an archiving utility. It combines multiple files and directories into a single file. This makes it easier to store or send large sets of data. Tar does not compress files by default—it just bundles them. Compression is added using external tools like gzip or bzip2.
Think of tar as a digital suitcase. You pack your files into it, and then you can optionally squeeze the suitcase to make it smaller. The result is a portable archive that preserves file permissions, ownership, and timestamps.
Many Linux distributions include tar pre-installed. You can check by typing `tar –version` in your terminal. If it is missing, install it using your package manager (e.g., `sudo apt install tar` on Ubuntu).
Why Use Tar Over Other Tools
Tar is lightweight and universal. Almost every Unix-like system supports it. Unlike zip or rar, tar does not require additional software to create or extract archives. It also handles symbolic links, hard links, and special files gracefully.
Another advantage is streaming. You can pipe tar data directly to another command or over a network. This makes it ideal for backups and remote transfers. For example, you can create a backup and send it to a remote server in one line.
Tar is also script-friendly. You can automate backups with cron jobs or shell scripts. Its output is predictable and easy to parse.
Basic Tar Syntax And Options
The basic syntax for tar is:
tar [options] [archive-file] [files or directories]
Options are usually specified without a dash, though some versions accept dashes. Common options include:
- -c : Create a new archive.
- -x : Extract files from an archive.
- -t : List the contents of an archive.
- -f : Specify the archive file name.
- -v : Verbose mode (show files being processed).
- -z : Filter the archive through gzip (compress or decompress).
- -j : Filter through bzip2.
- -J : Filter through xz.
You combine options like this: tar -cvf archive.tar /path/to/dir. This creates a verbose archive named archive.tar from the specified directory.
Creating A Tar Archive
To create a simple tar archive, use the -c and -f options. For example:
tar -cf myfiles.tar file1.txt file2.txt folder1/
This creates myfiles.tar containing the specified files and folder. Add -v to see the progress:
tar -cvf myfiles.tar file1.txt file2.txt folder1/
If you want compression, add -z for gzip:
tar -czvf myfiles.tar.gz file1.txt file2.txt folder1/
For bzip2 compression, use -j:
tar -cjvf myfiles.tar.bz2 file1.txt file2.txt folder1/
For xz compression (smallest size but slower), use -J:
tar -cJvf myfiles.tar.xz file1.txt file2.txt folder1/
Extracting A Tar Archive
To extract an archive, use the -x option. For example:
tar -xf myfiles.tar
This extracts the contents into the current directory. To extract to a specific location, use -C:
tar -xf myfiles.tar -C /target/directory
For compressed archives, tar automatically detects the compression type if you use the correct options. But you can also specify:
tar -xzvf myfiles.tar.gz
tar -xjvf myfiles.tar.bz2
tar -xJvf myfiles.tar.xz
Listing Archive Contents
To see what is inside an archive without extracting, use -t:
tar -tf myfiles.tar
This lists all files and directories. Add -v for more details like permissions and sizes:
tar -tvf myfiles.tar
Advanced Tar Usage
Tar offers many advanced features. You can exclude certain files, update archives, or handle large datasets. Let us explore some practical scenarios.
Excluding Files And Directories
Use the --exclude option to skip specific files or patterns. For example:
tar -czvf backup.tar.gz /home/user --exclude="*.mp4" --exclude="temp/"
This creates a backup of /home/user but excludes all MP4 files and the temp directory. You can also use --exclude-from=file to read exclusion patterns from a text file.
Appending Files To An Existing Archive
To add files to an existing tar archive, use the -r option. Note that this works only for uncompressed archives:
tar -rf myfiles.tar newfile.txt
This appends newfile.txt to myfiles.tar. For compressed archives, you must recreate them.
Using Tar With Compression
Compression reduces file size but takes more CPU time. Gzip is fast and widely used. Bzip2 compresses better but is slower. Xz offers the best compression ratio but is the slowest. Choose based on your needs.
You can also combine tar with other compression tools using pipes. For example:
tar -cf - /path/to/dir | gzip > archive.tar.gz
This is equivalent to tar -czf archive.tar.gz /path/to/dir.
Creating Incremental Backups
Tar supports incremental backups using snapshot files. First, create a full backup:
tar -czvf backup_full.tar.gz --listed-incremental=backup.snap /home/user
Then, later, create an incremental backup:
tar -czvf backup_inc1.tar.gz --listed-incremental=backup.snap /home/user
This only archives files that changed since the last snapshot. To restore, extract the full backup first, then the incremental ones in order.
Common Tar Examples
Let us look at some real-world examples. These will help you understand how to use tar in daily tasks.
Backing Up A Directory
To back up your home directory with compression:
tar -czvf home_backup_$(date +%Y%m%d).tar.gz /home/user
This creates a dated backup file. You can automate this with a cron job.
Transferring Files Over SSH
Combine tar with SSH to transfer files securely:
tar -czvf - /path/to/dir | ssh user@remote "cat > /remote/path/backup.tar.gz"
This creates a compressed archive on the fly and sends it to a remote server. No temporary files are needed.
Extracting Specific Files
To extract only certain files from an archive, specify them at the end:
tar -xvf archive.tar.gz file1.txt folder2/file3.txt
This extracts only those files. You can also use wildcards with --wildcards:
tar -xvf archive.tar.gz --wildcards "*.txt"
Viewing Archive Size
To check the size of files inside an archive without extracting:
tar -tvf archive.tar.gz | awk '{sum+=$3} END {print sum}'
This prints the total size in bytes. You can convert to MB or GB manually.
Troubleshooting Common Tar Issues
Sometimes tar may give errors. Here are common problems and solutions.
Archive Is Corrupted
If you see “Unexpected EOF in archive,” the archive is incomplete. Try downloading again or check the source. You can attempt to recover data with tar -xvf archive.tar --ignore-zeros.
Permission Denied
When extracting, you might get permission errors. Use sudo or extract to a directory you own. For example:
sudo tar -xvf archive.tar -C /opt
File Path Too Long
Tar archives can have long paths. If extraction fails, try using --strip-components=1 to remove the top-level directory:
tar -xvf archive.tar --strip-components=1
Frequently Asked Questions
What Is The Difference Between Tar And Gzip?
Tar is an archiver that bundles files, while gzip is a compressor that reduces file size. They are often used together: tar creates the archive, and gzip compresses it. The result is a .tar.gz file.
How Do I Extract A Tar Gz File In Linux?
Use the command tar -xzvf file.tar.gz. This extracts the compressed archive. You can also use tar -xf file.tar.gz if your tar version auto-detects compression.
Can Tar Handle Large Files?
Yes, tar can handle files larger than 4GB. It supports POSIX.1-2001 (pax) format, which removes the 8GB limit of older formats. Use --format=posix for large archives.
What Is A Tarball?
A tarball is simply a tar archive, often compressed. The term comes from “tar ball.” It is a common way to distribute source code or backups in Linux.
How Do I Create A Tar Archive Without Compression?
Use tar -cf archive.tar /path/to/dir. This creates an uncompressed archive with the .tar extension. You can later compress it with gzip or bzip2 if needed.
Best Practices For Using Tar
To get the most out of tar, follow these tips. They will save you time and prevent data loss.
- Always verify archives after creation. Use
tar -tf archive.tarto list contents and check integrity. - Use meaningful names for archives. Include dates and descriptions, like
project_backup_20231015.tar.gz. - Test extraction in a temporary directory before overwriting important files.
- Compress only when needed. For local backups, compression saves space but takes time. For network transfers, it is often worth it.
- Keep backups separate from original files. Store archives on a different drive or remote server.
Automating Tar Backups
You can automate backups with a simple script. Create a file called backup.sh:
#!/bin/bash
tar -czvf /backups/home_$(date +%Y%m%d_%H%M%S).tar.gz /home/user
echo "Backup completed."
Make it executable with chmod +x backup.sh. Then add it to cron with crontab -e. For example, to run daily at 2 AM:
0 2 * * * /path/to/backup.sh
Conclusion
Now you know what is tar linux and how to use it effectively. Tar is a powerful tool for archiving, compressing, and transferring files. With practice, you will find it indespensable for backups and data management.
Start by creating a simple archive of a test directory. Experiment with different compression methods. Soon, you will be using tar with confidence in your daily workflow.
Remember to check the tar manual (man tar) for more options. The command line is your friend. Happy archiving!