Operations 12 min read

Master Linux File Archiving with tar: Create, Compress, and Extract Efficiently

Learn how to use the Linux tar command to archive files and directories, explore options for creating, listing, and extracting archives, and compare gzip, bzip2, and xz compression methods with performance metrics and practical examples.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Linux File Archiving with tar: Create, Compress, and Extract Efficiently

File archiving means grouping a set of files or directories with the same attributes into a single file and storing it in a dedicated location for later retrieval, such as preserving old system logs.

Using tar command to archive files or directories

tar can pack multiple files into one archive for transmission or storage, and the archive can be compressed with gzip , bzip2 or xz .

To archive the logs in /var/log/ , the c option creates an archive and the f option specifies the archive name:

[root@websvr ~]# tar -cf log.tar /var/log/
tar: Removing leading `/' from member names
[root@websvr ~]# ls
log.tar
[root@websvr ~]#

If the archived path starts with "/", tar automatically removes the leading slash to avoid overwriting files when extracting.

Adding the v option shows detailed progress:

# tar -cvf log1.tar /var/log/
... (output omitted) ...

Use the t option to list the contents of an archive:

# tar -tf log.tar
var/log/...

Use the x option to extract files from the archive:

# mkdir log
# cd log
# tar -xf ~/log.tar
var

Compressing archive files

When the archive is large or needs to be transferred, compress it to reduce bandwidth.

tar supports three compression methods:

gzip : fastest compression speed, widely used, but lower compression ratio.

bzip2 : better compression than gzip, less commonly used.

xz : newest, highest compression ratio among the three.

Compression options:

-z creates a gzip archive (*.tar.gz or *.tgz).

-j creates a bzip2 archive (*.tar.bz2).

-J creates an xz archive (*.tar.xz).

Creating a gzip compressed archive:

# tar -czf log.tar.gz /var/log/
... (output omitted) ...

The compressed file is smaller than the original tar.

Resource usage during gzip compression:

top - 16:54:46 up 16:18, 2 users, load average: 0.80, 0.59, 0.30
%Cpu(s): 35.3 us, 11.0 sy, 43.1 id, 10.4 st
MiB Mem : 1829.0 total, 80.7 free, 179.5 used, 1568.8 buff/cache

Time taken to compress 30 files (each 100 MB) with gzip:

# time tar -czf test.tar2.gz test_file*
real 0m29.983s
user 0m23.198s
sys 0m7.041s

Creating a bzip2 compressed archive:

# tar -cjf log.tar.bz2 /var/log/
... (output omitted) ...

Compression ratio is higher than gzip but not dramatically.

Resource usage during bzip2 compression:

top - 16:52:56 up 16:16, 2 users, load average: 0.44, 0.46, 0.22
%Cpu(s): 43.7 us, 7.2 sy, 43.8 id, 5.2 st
MiB Mem : 1829.0 total, 71.2 free, 186.4 used, 1571.4 buff/cache

Time taken to compress the same set with bzip2:

# time tar -cjf test.tar3.bz2 test_file*
real 1m10.511s
user 1m1.819s
sys 0m10.762s

Creating an xz compressed archive:

# tar -cJf log.tar.xz /var/log/
... (output omitted) ...

xz provides the highest compression ratio but requires the most time and resources.

Resource usage during xz compression:

top - 16:51:36 up 16:15, 2 users, load average: 0.94, 0.53, 0.22
%Cpu(s): 45.2 us, 5.4 sy, 44.0 id, 5.4 st
MiB Mem : 1829.0 total, 62.9 free, 259.0 used, 1507.1 buff/cache

Time taken to compress the same set with xz:

# time tar -cJf test.tar3.xz test_file*
real 1m49.429s
user 1m40.588s
sys 0m16.924s

Summary

Among the three compression formats, xz achieves the highest compression ratio but consumes the most time and resources; gzip is the fastest and uses the least resources, though its compression ratio is lower; bzip2 offers moderate compression and performance, making it suitable for scenarios where a balance is needed.

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.

LinuxGzipSystem Administrationcompressionxzfile archivingtarbzip2
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.