Data compression helps lower the original size of a folder or a file, ultimately reducing disk space usage. It uses encoding technology, and the purpose is to reduce the targeted inode’s bit structure and prep it for faster transfer.

You may be familiar with compression methods. Primarily used are (Windows) WinRAR or tar and gzip (Unix), and zip which usually is cross-platform compatible.

Here we will go over the commands used to archive or compress files/folders in a Linux environment through the command-line interface. Let’s begin.

#1: Gzip

The gzip is a file format and a software application used for file compression and decompression. The program was created by Jean-loup Gailly and Mark Adler as free software used in early Unix systems and intended for use by GNU. This method of compression is only usable on single files. It is handy for text files, SQL files, or archived files via a different compression type (zip, tar, tar.gz)

Log into your account using the SSH, now use any Linux commands to navigate and locate the file or folder you wish to compress. Upon reaching your desired file or folder, please type in the following command:

gzip <file_name>

Here is an example of compressing an SQL file via the command line:

gzip nossl_wp18.sql

Note, the file you are archiving will disappear, and you will find the archive in its place.

To remove the compression and get the original file back, type the following command:

gunzip <file_name>.gz

We will use the same example as above:

gunzip nossl_wp18.sql.gz

This action will leave you with the original file without any form of compression.

#2: Tar/Tar.gz

The most suitable method for compressing files, folders, and multiple files/folders is Tar/Tar.gz. It keeps the original names of the inodes, the directory structure, file/folder permissions, ownerships, and any relevant information about the file. This compression method is excellent for archiving entire directories.

You need to keep the file structure the same and multiple files that you want to compress together. Log into your account using the SSH, now use any Linux commands to navigate and locate the file or folder you wish to compress.

Upon reaching your desired file or folder, please type in the following command:
tar -zcf <file_name>.tar.gz <file_name>
z - This flag will filter the command through gzip
c - The “c” flag stands for “create” and tells tar to create the archive.
f - Using this option uses the archive file or device ARCHIVE
v(optional) - You can add this flag so that the command will output the compression process.

Here is an example of us compression the root directory of a website:

tar -zcf public_html.tar.gz public_html

You will see a compressed copy of the public_html folder when the process is over, unlike the gzip process, which replaces the original file.

To remove the compression and extract the files from the archive, please use the command:

tar -zxf <file_name>

x - Stands for “extract”, which tells “tar” to remove the compression.

The above action will remove the compression, restore the original folder; however, the archive will remain. Another significant difference between the tar and the gzip command.

#3: Zip

You can use the zip command for single files, multiple files, and directories, and it is similar in functionality to the tar.gz command-line utility. Log into your account using the SSH, now use any Linux commands to navigate and locate the file or folder you wish to compress.

Upon reaching your desired file or folder, please type in the following command:

zip <file_name>.zip <file_name>

We will use the same example as with the gzip method and compress an SQL file.

zip nossl_wp18.sql.zip nossl_wp18.sql

Upon finishing its task, you will see the SQL file compressed with the zip extension. If you want to compress multiple files, list them after defining the archive’s name:

zip archive.zip file1 file2 file3

If you want to archive a whole directory, you need to use the “-r” flag, which stands for “recursively”. Let’s use the above example with the public_html:

zip -r public_html.zip public_html

After the process finishes, you will see your folder along with a compressed version of it. If you want to remove files or files (depending on what you compressed) from the archive, please use the command:

unzip <file_name>.zip

This action will eliminate the respective file/folder’s compression and leave the archive intact if you want to move it.

Using a compression method is essential for handling your disk space or transferring folders/files across servers. Hopefully, with the above tools at your disposal, you can find solutions to problems you typically could not solve!

Was this answer helpful? 0 Users Found This Useful (0 Votes)