Archive Compression Linux Command

Archive compression is a process of compressing one or multiple files and directories into a single archive file with a reduced size. The archive file can then be compressed using various algorithms such as gzip, tar, bzip2, etc. to save disk space and to make it easier to transfer files over the network. The archive file can be later decompressed and the original files can be restored.


gzip

gzip is a widely used compression tool in Linux and Unix systems that compresses a single file into a smaller file with a .gz extension. It uses the DEFLATE algorithm to achieve lossless data compression. The compressed file is smaller in size than the original file, making it easier to transfer or store. The original file can be decompressed to its original state using the gzip -d command.

The syntax for the gzip command is:

gzip [OPTIONS] [FILE_NAME]


Options:
  •  -1 to -9: compression level, 1 is the fastest and less compression, 9 is slowest with the most compression 
  • -d: decompress the file 
  • -f: force compression/decompression, even if the file is compressed/decompressed already 
  • -h: display help/usage information 
  • -k: keep the original file, do not delete it 
  • -r: process directories recursively -t: test compressed file for integrity 
  • -v: display verbose information 
  • -#: display the compression ratio for each file

Example: 

You can compress a file using the gzip compression protocol named LZ77 using the gzip command. Here's the simplest usage:

username@Technoscience:~$ gzip newfile2.txt
username@Technoscience:~$ ls
newfile2.txt.gz
username@Technoscience:~$

This will create a compressed file "newfile.txt.gz" and delete the original file.

To prevent this, you can use the -c option and use output redirection to write the output to the "newfile.gzfile:

username@Technoscience:~$ gzip -c newfile.txt newfile2.txt > newfile.gz
username@Technoscience:~$ ls
newfile.gz    newfile.txt    newfile2.txt
username@Technoscience:~$

The -c option specifies that output will go to the standard output stream, leaving the original file intact, or you can use the -k option:

username@Technoscience:~$ gzip -k newfile4.txt
username@Technoscience:~$ ls
newfile4.txt    newfile4.txt.gz
username@Technoscience:~$

There are various levels of compression. The more the compression, the longer it will take to compress
(and decompress)
Levels range from 1 (fastest, worst compression) to 9 (slowest, better compression), and the default is 6.

You can choose a specific level with the -<NUMBER> option:

username@Technoscience:~$ gzip -4 newfile newfile4.txt newfile.txt > newfile.gz
username@Technoscience:~$


tar

"tar" is a file archiving utility in Linux used for packaging multiple files into a single archive file (tar file). The tar archive file format is commonly used for backup purposes and for the distribution of multiple files as a single archive. Tar files can be compressed using gzip, bzip2, or other compression utilities. The basic syntax to create a tar archive is:

username@Technoscience:~$ touch file1 file2 file3
username@Technoscience:~$ ls
file1  file2 file3
username@Technoscience:~$ tar -cvf archive.tar file1 file2 file3
file1
file2
file3
username@Technoscience:~$ ls
archive.tar  file1    file2    file3
username@Technoscience:~$ rm file1 file2 file3
username@Technoscience:~$

[OPTIONS] used in the above command:

  • -c: create an archive
  • -v: verbose output
  • -f: file, used to specify the name of the archive file

To extract the contents of a tar archive:

username@Technoscience:~$ tar -xvf archive.tar
file1
file2
file3
username@Technoscience:~$ ls
file1      file2    file3
username@Technoscience:~$

tar -xvf archive.tar

Options used in the above command:
  • -x: extract files
  • -v: verbose output
  • -f: file, used to specify the name of the archive file

To extract them to a specific directory, use:

username@Technoscience:~$ tar -xf archive.tar -C Desktop/
username@Technoscience:~$ ls Desktop/
file1  file2  file3
username@Technoscience:~$

You can also just list the files contained in an archive:

username@Technoscience:~$ ls
archive.tar
username@Technoscience:~$ tar -tf archive.tar
file1
file2
file3
username@Technoscience:~$

'taris often used to create a compressed archive, gzipping the archive. This is done using the -z option:

username@Technoscience:~$ tar -czf archive.tar.gz file1 file2
username@Technoscience:~$ ls
file1    archive.tar.gz    file2
username@Technoscience:~$

This is just like creating a tar archive and then running gzip on it. 

To unarchive a gzipped archive, you can use gunzip, or gzip -d, and then unarchive it, but tar -xf will recognize it's a gzipped archive and do it for you:

username@Technoscience:~$ tar -xf archive.tar.gz
username@Technoscience:~$


bzip2

'bzip2' is a lossless compression tool in Linux used to compress and decompress files. The compression method used by bzip2 is known as the Burrows-Wheeler algorithm and it provides better compression ratios than gzip. To use bzip2, one can run the following command in the terminal:

Syntax: 

bzip2 [options] filename

Options: 

  • -z: Compress the file 
  • -d: Decompress the file 
  • -k: Keep the original file after compression 
  • -v: Display the progress of compression 
  • -f: Force compression or decompression 
  • -t: Test the integrity of the compressed file

Example: 

username@Technoscience:~$ bzip2 -z file1
username@Technoscience:~$ ls
file1.bz2
username@Technoscience:~$

This will compress the file "file.txt" and create "file.bz2".

username@Technoscience:~$ bzip2 -d file1.bz2
username@Technoscience:~$ ls
file1 
username@Technoscience:~$

This will decompress the file "file.txt.bz2" and create "file.txt".


zip

The zip command is used to compress and archive files and directories in a zip archive. It works by compressing each file individually, then combining all the compressed files into a single archive file. The syntax for using the zip command is:

zip [-options] archive.zip file1 [file2 ...]

Where:

  • options are various optional switches that can modify the behavior of the zip command.
  • archive.zip is the name of the archive file that will be created.
  • file1 [file2 ...] are the names of the files and/or directories to be included in the archive.

Some common options used with the zip command include:-r (or --recurse-paths)
  • include all files and subdirectories within a specified directory.
  • -9 (or --best): use the highest level of compression.
  • -u (or --update): update an existing archive file, adding or replacing files as necessary.

For example:

username@Technoscience:~$ zip archive.zip file1 file2
  adding: file1 (stored 0%)
  adding: file2 (stored 0%)
username@Technoscience:~$ ls
archive.zip  file2    file1
username@Technoscience:~$

To extract files from a zip archive, use the unzip command.


unzip

"unzip" is a command line utility in Unix-based systems that allows you to extract compressed archive files (with .zip extension) and restore the original files. The syntax is:

unzip [options] archive.zip [file(s) ...] [destination]
Some common options include:
  • -l: List the contents of an archive without actually extracting the files.
  • -o: Overwrite files without asking for confirmation.
  • -d: Specify the destination directory for the extracted files.
  • -p: Extract files and preserve the original file permissions.
  • -q: Quiet mode, do not display any messages or error messages.

If the [file(s) ...] argument is not specified, "unzip" will extract all the files in the archive. If an [destination] argument is not specified, "unzip" will extract the files to the current working directory.

username@Technoscience:~$ unzip -l archive.zip    # List the contents of an archive
Archive:  archive.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2023-02-02 22:54   file1
        0  2023-02-02 22:54   file2
---------                     -------
        0                     2 files
username@Technoscience:~$ unzip archive.zip -d Desktop/
Archive:  archive.zip
 extracting: Desktop/file2
username@Technoscience:~$


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Ok, Go it!