-->

How to archive files and directories in Linux

Author Piyush Gupta

Archive files and directories In Linux, There are multiple tools available in Linux system for creating archive files. In this article we will see how to archive files and directories using tar, zip and gzip command.
Below is the most common programs to archive files and directories are;
  1. zip
  2. tar
  3. gzip

1 – Zip

zip is the most popular command line archiving utility for Linux systems. zip program comes pre-installed with some Linux distros. Just in case if it is not available, you can install it using the distribution’s default package manager.

On Arch Linux and variants:

$ sudo pacman -S zip unzip

On RHEL, CentOS, Fedora:

$ sudo yum install zip unzip

On Debian, Ubuntu, Linux Mint:

$ sudo apt-get install zip unzip

On SUSE/openSUSE:

$ sudo zypper install zip unzip
Now let us see some examples.

Create Archive of File

# zip output.zip /var/log/*.log

Create Archive of Directory

# zip -r output.zip /var/log

Extract Archive

# unzip output.zip

2 – Tar

Tar is an Unix command which stands for Tape Archive. It is used to combine or store multiple files (same or different size) into a single file. There are 4 main operating modes in tar utility.

  1. c – Create an archive from a file(s) or directory(s).
  2. x – Extract an archive.
  3. r – Append files to the end of an archive.
  4. t – List the contents of the archive.

Create Archive of File

# tar -cf output.tar /var/log/*.log

Create Archive of Directory

# zip -cf output.tar /var/log

Extract Archive

# tar -xf output.tar

3 – gzip

gzip is a utility to compress and decompress files using Lempel-Ziv coding (LZ77) algorithm.
gzip is one more tool for command line users for making archive files. gzip also supports to take input of data as standard input or through pipe and create zip file.

Create Archive of File

# gzip -k access.log

Create zip file of piped data

# cat /var/log/messages | gzip > messages.gz

Extract Archive

# gunzip access.log.gz

Combined – Tar + gzip

Tar also combined gzip program to enable higher compression level of files. Using tar with gzip created files extension will be .tar.gz.

Create Archive of File

# tar -czf output.tar.gz /var/log/*.log

Create Archive of Directory

# zip -czf output.tar.gz /var/log

Extract Archive

# tar -xzf output.tar.gz

No comments:

Post a Comment