Skip to content

2.6 Archiving and Compression

In Linux, “archiving” (bundling many files into one) and “compression” (making files smaller) are often two separate steps, though modern tools combine them.

tar (Tape ARchive) is the standard tool for bundling files.

  • Create an archive (-c):

    tar -cvf archive.tar file1 file2 dir/

    (Flags: create, verbose, file)

  • Extract an archive (-x):

    tar -xvf archive.tar

    (Flags: extract)

  • List contents (-t):

    tar -tvf archive.tar

Linux supports multiple compression formats. gzip is the most common, while xz offers better compression at the cost of speed.

ToolExtensionCompressDecompress
gzip.gzgzip filegunzip file.gz
bzip2.bz2bzip2 filebunzip2 file.bz2
xz.xzxz fileunxz file.xz
zip.zipzip -r arch.zip dirunzip arch.zip

You usually don’t compress distinct files; you compress a tarball.

  • gzip (-z): archive.tar.gz or .tgz

    tar -czvf archive.tar.gz folder/
    tar -xzvf archive.tar.gz
  • bzip2 (-j): archive.tar.bz2

    tar -cjvf archive.tar.bz2 folder/
  • xz (-J): archive.tar.xz

    tar -cJvf archive.tar.xz folder/

rsync is a powerful tool for synchronization and backups. It only copies changes.

# Sync contents of source to destination
# -a: archive mode (preserves permissions, times, etc.)
# -v: verbose
rsync -av source/ destination/

# Remote sync (over SSH)
rsync -avz source/ user@remote:/backup/DIR/