Mastering tar: Create, Extract, List, and Manage Linux Archives
This guide explains how to use the Linux tar command to create uncompressed, gzip‑compressed, and bzip2‑compressed archives, extract whole or individual files, list archive contents, append files, verify archives, and check archive sizes, with clear examples for each operation.
1. Create a tar archive
Use the c option to create a new .tar file. Example:
# tar -cvf rumenz-14-09-12.tar /home/rumenz/ c– create a new archive v – verbose output f – specify the archive file name
2. Create a tar.gz archive
Add the z option to compress with gzip. Example:
# tar -cvzf MyImages-14-09-12.tar.gz /home/MyImages
# tar -cvzf MyImages-14-09-12.tgz /home/MyImagesNote: tar.gz and tgz are equivalent.
3. Create a tar.bz2 archive
Use the j option for bzip2 compression, which yields smaller files but slower processing. Example:
# tar -cvfj Phpfiles-org.tar.bz2 /home/php
# tar -cvfj Phpfiles-org.tar.tbz /home/php
# tar -cvfj Phpfiles-org.tar.tb2 /home/php4. Extract a tar archive
Use the x option to extract. Example: # tar -xvf public_html-14-09-12.tar To extract to a specific directory, add -C followed by the target path.
# tar -xvf public_html-14-09-12.tar -C /home/public_html/videos/5. Extract a tar.gz archive
# tar -xvf thumbnails-14-09-12.tar.gzUse -C to specify a different destination directory.
6. Extract a tar.bz2 archive
# tar -xvf videos-14-09-12.tar.bz27. List contents of a tar archive
Use the t option to list files without extracting.
# tar -tvf uploadprogress.tar8. List contents of a tar.gz archive
# tar -tvf staging.rumenz.com.tar.gz9. List contents of a tar.bz2 archive
# tar -tvf Phpfiles-org.tar.bz210. Extract a single file from a tar archive
# tar -xvf cleanfiles.sh.tar cleanfiles.sh
# tar --extract --file=cleanfiles.sh.tar cleanfiles.sh11. Extract a single file from a tar.gz archive
# tar -zxvf rumenzbackup.tar.gz rumenzbackup.xml
# tar --extract --file=rumenzbackup.tar.gz rumenzbackup.xml12. Extract a single file from a tar.bz2 archive
# tar -jxvf Phpfiles-org.tar.bz2 /home/php/index.php
# tar --extract --file=Phpfiles-org.tar.bz2 /home/php/index.php13. Extract multiple files from any archive type
# tar -xvf rumenz-14-09-12.tar "file1" "file2"
# tar -zxvf MyImages-14-09-12.tar.gz "file1" "file2"
# tar -jxvf Phpfiles-org.tar.bz2 "file1" "file2"14. Extract a group of files using wildcards
# tar -xvf Phpfiles-org.tar --wildcards '*.php'
# tar -zxvf Phpfiles-org.tar.gz --wildcards '*.php'
# tar -jxvf Phpfiles-org.tar.bz2 --wildcards '*.php'15. Append files or directories to an existing tar archive
# tar -rvf rumenz-14-09-12.tar xyz.txt
# tar -rvf rumenz-14-09-12.tar php16. Append to compressed archives (tar.gz / tar.bz2)
Appending directly to .tar.gz or .tar.bz2 is not supported and results in errors such as “This does not look like a tar archive”.
17. Verify tar archives
Use the W (verify) option for uncompressed tar files only.
# tar tvfW rumenz-14-09-12.tar18. Check archive size
Pipe the archive to wc -c to get its size in bytes.
# tar -czf - rumenz-14-09-12.tar | wc -c
# tar -czf - MyImages-14-09-12.tar.gz | wc -c
# tar -czf - Phpfiles-org.tar.bz2 | wc -cSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
