Menu Close

Using Terminal Commands to Compress and Decompress Files in Ubuntu Linux

This article talks about how to compress and decompress files using the graphical interface in Ubuntu Linux.

For more on this subject, please refer to the SOC Table of Contents.

 

 

Compressing using the zip command

We can tell from the name that this command specifies in dealing with .zip files. It can be used to compress one or multiple files into a single .zip file. The formatting is as follows:

zip [option] [compressed file name.zip] [file to be compressed]

The most common options are as follows:

-b<working directory>           Specifies a temporary directory for storing files.

-d                                                  Deletes a specified file from the zip file.

-F                                                  Attempts to fix and recover damaged zip files.

-g                                                  Compresses the file into an existing zip file without creating a new one.

-h                                                  Get help documentation.

-j                                                   Only save the name of the file and not the directory.

-m                                                 Delete the original file after compression.

-n<file extension>                    Doesn’t compress file types that have the specified file extension.

-q                                                   Doesn’t show the compression process in the terminal.

-r                                                    Recursively compresses all the folders and files under the specified location

-v                                                    Show the compression process in the terminal.

-[number]                                     A number between 1 and 0 that defines the compression rate.

As an example, we will now compress the folder test2 using the command:

zip -rv test2.zip test2

 

 

Decompressing using the unzip command

The formatting is as follows:

unzip [option] [compressed file name.zip]

The main options are as follows:

-l                                 Shows all of the files that the compressed file contains.

-t                                 Checks if the compressed file is damaged and doesn’t decompress.

-v                                Show the process in the terminal.

-Z                                Only show the annotations for the compressed files.

-C                                Distinguish upper- and lower-case characters in the file names of the compressed files.

-j                                 Doesn’t process all of the original paths of the compressed files.

-L                                Change all of the compressed file names to lower-case characters. 

-n                                Doesn’t overwrite the original files upon decompression.

-P<password>         Decompression password.

-q                                Run quietly without displaying any information.

-x<list of files>        Specify which files to ignore in the .zip file.

-d<directory>          Decompress the contents of the file to the set directory.

As an example, we will decompress the test2.zip file that we just compressed.

Note how the first line deletes the original test2 folder to prevent it from interfering with the decompression process.

 

 

Using the tar command

The zip and unzip commands are only applicable to .zip files, and cannot be used with other file types, such as the most commonly used .bz2 and .gz for Linux.

This is where the tar command comes in. The formatting is as follows:

tar [option] [compressed file name] [name of the file to be compressed]

The most common options are as follows:

-c                                               Create a new compressed file.

-C<target directory>           Switch to the specified directory.

-f<backup file>                     Specify the compressed file.

-j                                               Use tar to create the compressed file and use bzip2 to compress it.

-k                                              Don’t overwrite the existing file when decompressing backup files.

-m                                            Doesn’t change the date last changed when recovering files.

-r                                              Add file to the end of an existing backup file.

-t                                              List the contents of the backup file.

-v                                             Display the process in the terminal.

-w                                            Ask the user first when it detects a problem.

-x                                             Release files from the backup file, which is the decompressed file.

-z                                             Uses tar to create a compressed file and uses gzip to compress it.

-Z                                             Uses tar to create a compressed file and uses compress to compress it.

As an example, we will be using the tar command to compress a .zip file as well as a .gz file.

Thus, we will be using the following two commands to compress the test1 folder into .bz2 and .gz formats.

tar -vcjf test1.tar.bz2 test1
tar -vczf test1.tar.gz test1

We will now use the following two commands to unzip the .bz2 and .gz files:

tar -vxjf test1.tar.bz2
tar -vxzf test1.tar.gz

Note that -vxjf is used to unzip the .bz2 file, while -vxzf is used to unzip the .gz file.

Posted in Textbook and Training Project

Related Articles

Leave a Reply

Your email address will not be published.

Leave the field below empty!