5000+ Great Articles

Create and Edit Zip Files In Linux Using The Terminal

In the previous article, we covered in detail how to use the tar command to create archives. While tar is a very common compression scheme for Linux, it is not as popular with Windows and Mac OS X users who find that most of their archives are created using the zip format.

On Linux, it is easy to use Zip archives (for creation) and Unzip (for extension). In fact, most GUI archive management programs (such as Ark, File Roller, and Xarchiver) will act as an interface to almost any command line archiving program installed on your computer, and Zip is no exception. Of course, we can also use Zip from the Terminal. Here’s how.

As you may have guessed, the first step is to open the Terminal.

Then enter “sudo apt-get install zip unzip” (without quotes) to make sure zip and unzip are installed.

– /

Note. If these two programs are already installed, you will receive a message stating that it is, as shown above.

Once installed, we can use zip to create archives (or modify existing ones) and unzip to expand them to the originals. For this tutorial, we’ll create a new folder on the desktop called Stuff. In Terminal we can do this with a single command – mkdir / home / username / Desktop / Stuff (of course you will replace “username” with your own username as shown below, and if you already have a Stuff folder on your desktop you will want change name).

Now that we have a Stuff folder, we will use the “cd” command to make the Stuff folder our current working directory.

cd / home / username / Desktop / Stuff

Now enter touch doc1.txt doc2.txt doc3.txt && mkdir Files into your Terminal, which will create a folder named Files, as well as three documents – doc1.txt, doc2.txt and doc3.txt – inside the Stuff folder.

Another command, “cd” to the newly created cd Files folder, because we need other documents inside it.

cd files

Finally, enter touch doc4.txt doc5.txt doc6.txt to create three new documents.

Now enter cd ./ . to return the desktop to the working directory.

Our penultimate step before creating the zip file is to create a couple of “additional” documents on the desktop with the same names as the files we just created, so type touch doc2.txt doc3.txt to create them.

Finally, open each of the two “additional” text files and add text to them. It doesn’t have to be anything meaningful (or lengthy), just so we can see that these documents are indeed different from those already created in the Stuff and files folders.

Once that is done, we can start creating our ZIP files. The easiest way to use zip is to tell it the name of the zip archive you want to create, and then explicitly name each file that should be in it. So, assuming our working directory is the Desktop, we have to enter zip test Stuff / doc1.txt Stuff / doc2.txt Stuff / doc3.txt to create an archive named test.zip (we don’t need to use “.zip “” In the command, since it will be added automatically), which will contain the files doc1.txt, doc2.txt and doc3.txt located inside the Stuff folder.

You will see a small output that tells us that three documents (doc1.txt, doc2.txt, and doc3.txt) have been added to the archive.

We can check this by double-clicking the archive that should be on our desktop. This should open it in a standard archiving program (Ark in KDE, File Roller in GNOME, and Xarchiver in Xfce).

What about the Files folder? Assuming we want this, add the documents inside it to our archive, we could use the same command as above, but add Stuff / Files / * to the end of the command.

An asterisk means all contents of the folder are included. So if there was another folder inside the Files folder, it would also be added. However, if there are items in this folder, they will not be included. For this we need to add -r (which means recursive or recursive).

It should be noted that the above two commands are not intended to “add” files to a zip archive; they are created to create it. However, since the archive already exists, the command simply adds any new files to the existing archive. If we wanted to create this archive all at once (instead of the three steps we followed to gradually add files to it for educational purposes), we could simply enter zip -r test Stuff / * and create the same archive.

From the command and output, you will notice that there are three files inside the Stuff folder included, as well as three documents inside the Files folder, so everything was done with a nice and simple command.

What about those two “additional” documents that we created on our desktop? Zip works like this: if you try to add a file to an archive that already exists in the archive, the new files will overwrite the old ones. So, since the documents we created on our desktop (doc2.txt and doc3.txt) have content (we added hello world to doc2.txt and cheers to doc3.txt), we should have possibility to add these documents and then you can check it. First, we’ll drag the two “extra” documents into the Stuff folder.

You will probably be asked if you want new documents to replace existing ones (remember that this is in a folder, not a zip archive), so let that happen.

Now that this is done, let’s add them to the archive by typing zip test Stuff / doc2.txt Stuff / doc3.txt

You will notice that the above command now shows that files are being updated rather than added. If we check the archive now, we will notice that the files look the same, but when doc2.txt and doc3.txt are open, you will see that they now have content, not empty ones like in our original files. were.

Sometimes on Linux, you can see that some files are hidden by adding a period (“.”) To the beginning of the file name. This is especially true for configuration files that should exist but are often not visible (which reduces clutter and also reduces the chances of accidentally deleting the configuration file). We can add them to the zip file quite easily. First, let’s say we want to create a zip file named backup from every file in the directory. We can do this by typing zip backup * in Terminal.

This will add all files and folders, although no items in that folder will be included. To add them, we would add -r again so that zip -r backup * would be the command.

Now we are almost there. To add folders, files and hidden files recursively, the command is actually very simple: zip -r backup.

Now it’s pretty easy to unpack. However, before we do anything else, delete the documents on the desktop (doc2.txt and doc3.txt) as well as the Stuff folder. When they disappear by typing unzip test.zip, the contents of our original zipped archive will be moved to your current directory.

Note. If we hadn’t deleted the documents, we would have tried to unzip the contents of our zip file into an existing file, so you will be asked if we want to replace each document.

And it’s all! Compressing and unzipping is a fairly common task, and while there are certainly GUI options available, with practice you will find that performing the same tasks from Terminal is also not very difficult.

Exit mobile version