loading...

linux : archiving & compression

zakiarsyad profile image Zaki Arrozi Arsyad Updated on ・2 min read

Archiving is the process of combining multiple files and directories (same or different sizes) into one file. Compression is the process of reducing the size of a file or directory.

Common type of compression:

  • Gzip : with extension .tar.gz. It has fastest compression time.
  • Bzip : with extension .tar.bz2
  • Xz : with extension .tar.xz. It has smallest size compression.

Common programs for archiving files:

  • tar
  • zip
  • gzip
  • bzip2
  • xz

We will talk about how we use tar command. It has a lot of option that we can use:

  • -c or --create : create a new file archive
  • -f or --file : filename
# create an archive from multiple files
tar -cf ARCHIVE.tar FILE1.txt FILE2.txt FILE3.txt

# create an archive from a directory
tar -cf ARCHIVE.tar DIRECTORY

# using long command
tar --create --file ARCHIVE.tar DIRECTORY
  • -X or --exclude : exclude a file
# exclude a file when creating an archive
tar -cf ARCHIVE.tar DIRECTORY --exclude=FILE.txt
  • -x or --extract : extract an archive
# extract an archive file
tar -xf ARCHIVE.tar

# using long command
tar --extract --file ARCHIVE.tar
  • -z : zip or unzip using gzip

create a compressed archive

# create an archive with gzip compression
tar -czf ARCHIVE.tar.gz DIRECTORY

# using gzip
gzip ARCHIVE.tar.gz DIRECTORY

extract a compressed archive

# extract an archive with gzip compression
tar -xzf ARCHIVE.tar.gz

# using gunzip --decompress
gunzip -d ARCHIVE.tar.gz
  • -j : zip or unzip using bzip2

create a compressed archive

# create an archive with bzip compression
tar -cjf ARCHIVE.tar.bz2 DIRECTORY

# using bzip2
bzip2 ARCHIVE.tar.bz2 DIRECTORY

extract a compressed archive

# extract an archive with bzip compression
tar -xjf ARCHIVE.tar.bz2

# using bunzip2 --decompress
bunzip2 -d ARCHIVE.tar.bz2
  • -J : zip or unzip using xz

create a compressed archive

# create an archive with xz compression
tar -cJf ARCHIVE.tar.xz DIRECTORY

# using xz
xz ARCHIVE.tar.xz DIRECTORY

extract a compressed archive

# extract an archive with xz compression
tar -xJf ARCHIVE.tar.xz

# using unxz --decompress
unxz -d ARCHIVE.tar.xz
  • zip and unzip

create a compressed archive

# create using zip
zip ARCHIVE.zip DIRECTORY

extract a compressed archive

# extract using unzip
unzip ARCHIVE.zip
  • -t or --list : list the contents of an archive without extracting it
# display contents of an archive
tar -tf ARCHIVE.tar

# display contents using gzip
gzip -l ARCHIVE.tar.gz

# display contents using unzip
unzip -l ARCHIVE.zip
  • -r or --append : append files to the end of an archive
# append files to an archive
tar -rf ARCHIVE.tar FILE.txt

# using --append
tar --append -file ARCHIVE.tar FILE.txt
  • -u or --update : Append files which are newer than the corresponding copy in the archive
# append files to an archive
tar -uf ARCHIVE.tar FILE.txt

# using --append
tar --update -file ARCHIVE.tar FILE.txt
  • delete : delete files inside an archive
# delete from the archive
tar --delete --file ARCHIVE.tar FILE.txt
  • -C or --directory : change the directory
# change location of unarchived files
tar -xf ARCHIVE.tar -C DIRECTORY
  • -v or --verbose : see the detailed output
    We can combine this options with all others

  • --wildcards : matching on the filenames within an archive

# extract a particular type of file
tar -xf ARCHIVE.tar --wildcards "*.txt"

Posted on by:

Discussion

pic
Editor guide