The tar command is one of the most useful tools in Linux systems. It helps you archive, compress, and extract files and directories. Whether you’re backing up data or transferring files, tar makes the process simple and efficient.
In this tutorial, we’ll look at how to use the tar command in Linux. Follow the steps below to archive, compress, extract, and manage files efficiently using the tar command.
What is the tar command?
We use the tar command to compress and extract files from the command line. The basic syntax is shown below:
tar [flags] destinationFileName sourceFileName
The tar command uses the following options to customize how it works:
| Option | Description | When to Use |
|---|---|---|
| -c | Create a new archive. | Use this when you want to make a new archive file. |
| -z | Use gzip compression. | Use this when you want the archive compressed with gzip. |
| -v | Provide verbose output. | The -v option shows details of the files being compressed or extracted. |
| -f | Archive file name. | Use this to specify the name of the archive file (e.g., -f backup.tar). |
| -x | Extract from an archive. | Use this when you need to extract files from an archive. |
1. Create a Tar Archive
tar -cvf backup.tar /home/user/files/
Explanation:
- -c: Creates a new archive
- -v: Shows the progress in the terminal
- -f: Specifies the name of the archive file
This command creates an archive named backup.tar containing the contents of /home/user/files/.
2. Compress an Archive Using Gzip
tar -czvf backup.tar.gz /home/user/files/
Explanation:
-z: Compresses the archive using gzip
Other options are the same as above
This creates a compressed archive named backup.tar.gz.
3. Compress an Archive Using Bzip2
tar -cjvf backup.tar.bz2 /home/user/files/
Explanation:
-j: Compresses the archive using bzip2
This creates a smaller archive using bzip2 compression.
4. Extract a Tar Archive
tar -xvf backup.tar
Explanation:
-x: Extracts files from the archive
This command extracts all files from backup.tar into the current directory.
5. Extract a Gzip Compressed Archive
tar -xzvf backup.tar.gz
Explanation:
-z: Tells tar to use gzip to decompress
This extracts files from a .tar.gz archive.
6. Extract a Bzip2 Compressed Archive
tar -xjvf backup.tar.bz2
Explanation:
-j: Tells tar to use bzip2 to decompress
This extracts files from a .tar.bz2 archive.
7. View Contents of an Archive Without Extracting
tar -tvf backup.tar
Explanation:
-t: Lists the contents of the archive
This shows you what’s inside the archive without extracting it.
8. Add a File to an Existing Archive
tar -rvf backup.tar newfile.txt
Explanation:
-r: Appends files to an existing archive
This adds newfile.txt to backup.tar. Note: This only works with uncompressed .tar files.
9. Delete a File from an Archive
tar --delete -f backup.tar unwanted.txt
Explanation:
–delete: Removes a file from the archive
This deletes unwanted.txt from backup.tar. This option may not work with compressed archives.
10. Estimate Archive Size Before Creating
tar -cf - /home/user/files/ | wc -c
Explanation:
-cf -: Creates an archive and sends it to standard output
wc -c: Counts the number of bytes
This helps you estimate how large the archive will be.
The tar command is a powerful and flexible tool for managing files in Linux. Whether you’re backing up data, compressing files, or extracting archives, these commands will help you handle your tasks efficiently. If you need help using the tar command or face any issues, feel free to contact our 24/7 support team.
Learn more tutorials on How to Extract or Unzip .tar.gz Files in Linux