Operations 15 min read

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.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Mastering tar: Create, Extract, List, and Manage Linux Archives

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/MyImages

Note: 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/php

4. 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.gz

Use -C to specify a different destination directory.

6. Extract a tar.bz2 archive

# tar -xvf videos-14-09-12.tar.bz2

7. List contents of a tar archive

Use the t option to list files without extracting.

# tar -tvf uploadprogress.tar

8. List contents of a tar.gz archive

# tar -tvf staging.rumenz.com.tar.gz

9. List contents of a tar.bz2 archive

# tar -tvf Phpfiles-org.tar.bz2

10. Extract a single file from a tar archive

# tar -xvf cleanfiles.sh.tar cleanfiles.sh
# tar --extract --file=cleanfiles.sh.tar cleanfiles.sh

11. Extract a single file from a tar.gz archive

# tar -zxvf rumenzbackup.tar.gz rumenzbackup.xml
# tar --extract --file=rumenzbackup.tar.gz rumenzbackup.xml

12. 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.php

13. 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 php

16. 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.tar

18. 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 -c
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxBackupcompressioncommand-linearchivingtar
Liangxu Linux
Written by

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.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.