Whenever transferring the files from one server to another, it’s always good to compress them. Because when we transfer the files without compressing, it will take a long time to transfer it and sometimes it might be corrupted. But if we compress the files, the size of the files will be reduced as well as we can transfer it very quickly and safely.
There are several different kinds of compression, but in this blog, we will focus on the three most common compression file types: .zip, .tar, and .gz.
Create/Extract .zip Archive File
Examples to create .zip files,
If you intend to compress the selected files, the following command will help you,
zip singlefile.zip file1.txt file2.txt somejsfile.js phpfile.php
If you intend to compress an entire directory, the following command will help you,
zip filename.zip filename/
If the folder has the sub-directories, then use the below command to create a .zip file,
zip -r filename.zip filename/
In the above command, we used the option -r, which is used to compress an entire directory with its sub-directories.
Sometimes we may want to test a .zip file whether it’s valid or not. Here the command to do that,
unzip -tq filename.zip
The following command helps to list all the content from a .zip file,
unzip -l filename.zip
Examples to extract .zip files,
Here the command which helps to unzip a .zip file in the current directory,
If you want to unzip/extract the zipped files into certain directory, use the below command,
unzip filename.zip -d /directoryname
If you intend to extract single file from a .zip file, use the below command,
unzip folder.zip file.txt
Create/Extract .tar Archive File
Examples to create a .tar file,
tar -cvf filename.tar foldername/
- c – Creates a new .tar archive file.
- v – Verbosely show the .tar file progress.
- f – File name type of the archive file.
If you want to add files or directories to an existing .tar archive file, use the following command with the option -r,
tar -rvf filename.tar abc.txt
Here the command to test a .tar file whether it’s valid or not,
tar tvfW filename.tar
Here, W – Verify a archive file.
Here the command to list all the files from a .tar file,
tar -tvf filename.tar
Here, t – viewing content of archive file.
To check the size of .tar archive file, use the following command.
tar -czf - filename.tar | wc -c
The above command will display the size of archive file in Kilobytes (KB).
Example to extract a .tar file,
tar -xvf filename.tar
Here, x – Extract
tar -xvf filenamr.tar -C /home/public_html/
If you want to extract single file from a .tar file, use the below command,
tar -xvf filename.tar file.txt
Create/Extract .tar.gz Archive File
Example to create a .tar.gz archive file,
tar cvzf filename.tar.gz foldername/
Here, z – filter archive through gzip.
Note :We cannot do verification on a compressed *.tar.gz archive file.
Here the command to list all the files from a .tar.gz file,
tar -tvf filename.tar.gz
To check the size of .tar.gz archive file, use the following command,
tar -czf - filename.tar.gz | wc -c
Example to extract a .tar.gz file,
tar -xvf filename.tar.gz
To extract a single file from the .tar.gz archive file, use the command as follows.
tar -zxvf filename.tar.gz file.xml
Hope this helps.