Data compression, or sometimes referred to as archiving, is a service that helps you lower the original size of a folder or a file, ultimately reducing its 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 or simply bring down its disk space utilization. You may have heard or used compression methods before, especially if you are using (Windows) WinRAR or tar and gzip (Unix), and zip which usually is cross-platform compatible.
This knowledgebase article will go over the commands you can use to archive or compress files/folders in a Linux environment through the command-line interface. Let’s get started.
Gzip
The gzip 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)
Before using any Linux commands, you need to log inside your account using the SSH protocol. After login inside, you can use the Linux Command Line basics 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
The result is a new file, which has almost five times the reduced size. Note that the file you are archiving will disappear, and you will find the archive in its place(it will have the name of the original file, with a .gz extension at the end, in our case, nossl_wp18.sql.gz). It is not lost - the newly compressed version simply replaces it.
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.
Tar/Tar.gz
The tar/tar.gz method is suited for compression on files, folders, and multiple files/folders simultaneously. It keeps the original names of the inodes, the directory structure, file/folder permissions, and ownerships along with any relevant information about the file. This compression method is excellent for archiving entire directories where you need to keep the file structure the same (website directories, for example) and multiple files that you want to compress together.
Before using any Linux commands, you need to log inside your account using the SSH protocol. After login inside, you can use the Linux Command Line basics 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
When the process is over, you will see a compressed copy of the public_html folder, 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.
Same example with public_html:
tar -zxf public_html
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.
Zip
The last command we will be going over is the zip command. You can use it for single files, multiple files, and directories, and it is similar in functionality to the tar.gz command-line utility. The difference between the two is pretty minor, so their usage is up to personal preference.
Before using any Linux commands, you need to log inside your account using the SSH protocol. After login inside, you can use the Linux Command Line basics to navigate and locate the file or folder you wish to compress.
Upon reaching your desired file, 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, simply 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 file(depending on what you compressed) from the archive, please use the command:
unzip <file_name>.zip
This action will remove the respective file/folder’s compression and leave the archive intact if you want to move it.
Using a compression method is an essential aspect of managing 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!