DEV Community

Zaki Arrozi Arsyad
Zaki Arrozi Arsyad

Posted on • Updated on

linux : archiving & compression

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
Enter fullscreen mode Exit fullscreen mode
  • -X or --exclude : exclude a file
# exclude a file when creating an archive
tar -cf ARCHIVE.tar DIRECTORY --exclude=FILE.txt
Enter fullscreen mode Exit fullscreen mode
  • -x or --extract : extract an archive
# extract an archive file
tar -xf ARCHIVE.tar

# using long command
tar --extract --file ARCHIVE.tar
Enter fullscreen mode Exit fullscreen mode
  • -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
Enter fullscreen mode Exit fullscreen mode

extract a compressed archive

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

# using gunzip --decompress
gunzip -d ARCHIVE.tar.gz
Enter fullscreen mode Exit fullscreen mode
  • -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
Enter fullscreen mode Exit fullscreen mode

extract a compressed archive

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

# using bunzip2 --decompress
bunzip2 -d ARCHIVE.tar.bz2
Enter fullscreen mode Exit fullscreen mode
  • -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
Enter fullscreen mode Exit fullscreen mode

extract a compressed archive

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

# using unxz --decompress
unxz -d ARCHIVE.tar.xz
Enter fullscreen mode Exit fullscreen mode
  • zip and unzip

create a compressed archive

# create using zip
zip ARCHIVE.zip DIRECTORY
Enter fullscreen mode Exit fullscreen mode

extract a compressed archive

# extract using unzip
unzip ARCHIVE.zip
Enter fullscreen mode Exit fullscreen mode
  • -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
Enter fullscreen mode Exit fullscreen mode
  • -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
Enter fullscreen mode Exit fullscreen mode
  • -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
Enter fullscreen mode Exit fullscreen mode
  • delete : delete files inside an archive
# delete from the archive
tar --delete --file ARCHIVE.tar FILE.txt
Enter fullscreen mode Exit fullscreen mode
  • -C or --directory : change the directory
# change location of unarchived files
tar -xf ARCHIVE.tar -C DIRECTORY
Enter fullscreen mode Exit fullscreen mode
  • -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"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)