Sometimes, when you need to create a .zip archive on a Linux machine, it is not that much easy as it is in windows. Every time, you need to download a larger number of files or numerous tasks, or even when you need a backup for a website, you need to learn few basic commands for zipping and unzipping the folders in Linux. For compression of archive files, we happen to use the zip command. It is a utility for compression and file packaging. We also have a program called unzip that is used to unpack the zip archives. This tutorial will help you learn to compress and manage a folder on Ubuntu Linux using the command line.
Zip or compress the folder
For the purpose of compression, we need to install the zip command at first, for which we will use “apt command” or “apt-get command”. So, now we will open the terminal to install the zip command.
After opening the terminal, write the command, “sudo apt install zip unzip” to install the zip command.
Enter the required credentials.
The installation begins and the command line looks like this. After a few moments, it will be done.
The next step is to compress the required files using the zip command. We want to compress the folder named TestDir here whose current directory is Desktop.
So, therefore in command line in Linux, we will also change our current directory to the desktop.
We can see the currently present folders on our Desktop by using ls command.
The file we want to compress is also in the list.
We use the command “zip –r folderName folder” to which we want to compress our folder.
Eventually, the compressed TestDirTerminal.zip is created, which we wanted to create by compressing our folder TestDir, so as we can see it has been created.
The folder we wanted to compress has been compressed.
We can also compress the files using the command line in Linux. We have chosen a folder, TestFolder for this purpose and a file TestFile as you can see. Both are again located on the Desktop. We will compress a file and a folder respectively in this step.
We will move our current directory to Desktop if it is not selected earlier. After doing that we see the elements of the directory using “dir” command. We can find anything of our interest enlisted where TestFolder and TestFile are also listed.
For the sake of zipping the folder, we will use the following command.
This will zip the folder.
Similarly, we want to zip the file we will use the following command as follows.
This will zip our TestFile.
Once created, both the TestFile and Testfolder.zip can be seen at the Desktop.
Using gzip command
We can also use gzip command to compress a file as we did with the GroceryList.
The outcome shows that a GroceryList.gz has been created at the Desktop directory.
Using tar command
For too many files we will use tar command. Here we have GroceryList so, for this we will be using tar cvf
V: to display the output in the terminal (used for verification)
F: Just for fire options
We can always use the ls command for the verification Here, as you can see the GroceryList.tar has been created.
Unzip the compressed folder:
To unzip the file, we will be using the command, unzip. But at first, we need to install the unzip code as we did earlier for zipping. We use the command sudo apt-get install unzip for this purpose. The command is used as
Enter the credentials:
This will be installed eventually. You may see the following commands on the screen while you run it on our own end.
For zip command
At first, we will extract our files for zip files. So as in the above list you can the first example we compressed is listed there as compressed file “TestDirTerminal.zip”, we will now compress this file by using the unzip command. Here we write unzip and the name of zipped file we want to unzip.
We will see that
For tar file
The tar file will be having a varying methodology, we will be using a different method as we did while compressing so it is quite similar to that but here, for a tar file extraction, we will be using tar xvf,
Where x: means extract
V: for verification as we saw earlier
F: is for fire
Here we had a tar file named “GroceryList.tar” so we extracted it by using the following command.
The command helps us to extract the files and once they have been extracted we can also view the outcome using the ls command for the currently running directory. So, here is the extracted file.
For verification and viewing the list of contents in a tar file we use the command tar –tvf and add the name of tar file whose contents we want to see. As a result, we will get the list of archive contents to our screens. So, following command will be used to view the archived content in a particular tar file.
In this tutorial, we have seen how to create compressed folders/ files in Linux command line. We have also learned how to extract the original files from the compressed ones in Linux command line. We also observed that for side by side verification we use ls command whether the file has been created or not the file extension makes clear to us whether it has been compressed or not if it is then, is it zip, tar or gz. At last, we saw how we can view the contents that are archived in a particular tar file.