How to Manage Zip Files in Linux

choubertsprojects

The Best WordPress plugins!

1. WP Reset

2. WP 301 Redirects

3. WP Force SSL

To manage zip files in Linux, we can use the unzip command. It has a lot of options and uses the tar tool to extract individual files from a single archive file.

The “gzip command in linux” is a command-line tool that allows users to manage zip files. The gzip command can be used on all files, but it is most commonly used for compressing and decompressing the contents of .ZIP archives.

How to Manage Zip Files in Linux

Have you ever been curious about how to unzip files on Linux? Or how to make a zip archive and extract it? Then this guide is all you’ll need to efficiently handle your zip files.

One of the most effective and widely used methods for compressing files and lowering their size without sacrificing quality is zipping. In addition, you’ll learn how to compress and decompress zip files in Linux in this article, as well as other useful tips and techniques. Ready?

Dive in and at the Conclusion of this course, you’ll be a “zip master”!

Prerequisites

All of the exercises in this lesson were performed on Kali Linux 2021.2. Other Linux distributions, such as Debian-based (like Unbuntu or Kali Linux), CentOS, or Fedora, might also be used.

Installing and Uninstalling the Zip and Unzip Packages

You’ll need a pair of Linux programs named zip and unzip to compress and decompress ZIP files. Let’s look at how to use the apt package manager to install these items.

To install the zip and unzip packages, open a terminal window on your desktop and perform the instructions shown below, which may vary depending on the Linux distribution you’re using.

To handle higher permissions, each of the tasks following starts with the sudo command.

For Debian, see

The apt package manager is used to install and unzip the zip and unzip packages in the scripts below.

# Installs the zip package using sudo apt install zip # Installs the unzip package using sudo apt install unzip

Fedora and CentOS are two of the most popular Linux distributions.

To install the zip and unzip packages, run the lines below using the yum package management tool.

yum install zip sudo # This command installs the zip package. # Installs the unzip package using sudo yum install unzip

Using the Zip Package to Compress Files

It’s time to learn how to compress files now that you’ve installed all of the essential software. To do so, use the zip command, which lets you compress files into ZIP packages from both the command line and the graphical user interface. Let’s go through each approach one by one.

Using the Command Line to Zip Files

Because you’ll be utilizing the command line a lot with Linux, let’s look at how to compress files from the command line first.

Take a look at the command syntax below. You’ll see that outpufile is the name of the zip file following the zip command. Then the names of the files and folders to include in the zip file (file1 file2 folder).

outputfile file1 file2 folder zip [option]

Open a terminal and create a directory named /mydir to illustrate compressing files on the command line. Then, under /mydir, create two files and a folder called test1, test2, and myfolder.

To create the mydir directory, use the mkdir command.

2. The following commands Make the test1.txt and test2.txt files (touch them). Then create the myfolder directory (mkdir).

/mydir/test1.txt touch mkdir /mydir/test2.txt touch myfolder

3. To list (ls) the contents of the working directory (/mydir), use the command below.

List all of the files in the working directory.List all of the files in the working directory.

After you’ve generated the files and directories, use the command below to compress the /mydir folder.

The zip command below searches the mydir directory recursively (-r) for all files and subdirectories. It then compresses all of those files into a ZIP file named newdir.zip after it’s finished.

The zip command is used to compress a single file.The zip command is used to compress a single file.

Maybe you simply want to compress the files in /mydir/test1.txt, /mydir/test2.txt, and /mydir/myfolder. Run the command below to do this.

The zip command below searches recursively (-r) for all files and folders named test1.txt, test2.txt, and myfolder in the working directory (mydir) and creates a ZIP file called newfiles.zip.

myfolder cd mydir zip -r newfiles test1.txt test2.txt

The.zip suffix for the zip file is optional since it is inserted automatically when compression is finished. Instead of merely newfiles, the zip command creates a ZIP file named newfiles.zip in the example above.

combining numerous files into a single zip filecombining numerous files into a single zip file

After Archiving, Delete the Original File

You learnt how to compress files into a ZIP file in the previous section, but you may have observed that the original files were not erased. To delete the original files, use the -m option in the zip command, as shown below.

Launch a terminal after generating the ZIP file and enter the following command to destroy the original files automatically. The zip command below zips the test1.txt, test2.txt, and myfolder files and folders to the newsfiles directory. The original files are deleted (-m) when the zip ZIP file is created.

You’ll note that the zip command deletes the original files once you execute it.

newfiles.zip zip -m -r myfolder test1.txt test2.txt

Adding New Files to a Zip File that Already Exists

While compressing data to a ZIP file, you may have forgotten to include a file. What is the best way to add a new file to an existing zip file? You could remove the zip file and start again, but there’s a better method to do it.

Run the following command to add a new file to the current ZIP file (newfiles.zip). By recursively (-r) scanning through all files in the /mydir directory and adding them to the ZIP package, the program below updates (-u) the existing ZIP file package (newfiles.zip).

test3 zip -u -r newfiles.zip

Adding New Files to a Zip File that Already ExistsAdding New Files to a Zip File that Already Exists

Taking Files Out of a Zip File

You already learnt how to add files to compressed files that already existed. But what if you mistakenly added a file to a zip file? Obviously, you’d want to delete that file from the zip file, and adding the -d option to the zip command would do that.

Run the zip command, removing (-d) the test3 file from the (newfiles.zip) ZIP file to remove a file from the ZIP file.

test3 zip -d newfiles.zip

Taking a file out of a zip fileTaking a file out of a zip file

Using the GUI to Zip Files

You should know how to compress files using the command line by now. However, if you prefer to work using a graphical interface, you’re in luck. With a few clicks, the GNU Network Object Model Environment (GNOME) environment allows you to generate a zip archive. GNOME is a desktop user interface that runs on top of your operating system. So, let’s get this party started!

1. Open the file manager after switching to the GNOME environment.

Using the File ManagerUsing the File Manager

2. Navigate to the /mydir directory and look for the files to compress.

3. To make a Bundle package, first pick all of the files you want to zip, then right-click on any of them and choose Compress from the context menu.

Choosing a compression techniqueChoosing a compression technique

In the pop-up box below, choose the compressed file’s format. There are three file formats available:.zip,.tar.xz, and.7z. All of these file types are compression formats.

Select the.zip option since you’re creating a zip file. Then, after giving it a name (newfiles), click Generate to create the zip file.

The zip file is produced automatically in the /mydir folder, which contains the files you’re compressing.

The newfiles.zip file is being created. The newfiles.zip file is being created.

You’ll see that Linux produces a newfiles.zip file from the files you picked down below.

The zip archive was successfully produced.The zip archive was successfully produced.

Using the Unzip Package to Decompress Files

You learnt how to compress files in the previous instances. However, the contents of the zip file may need to be extracted or decompressed. How would you go about doing that?

You receive two ways for decompressing files, same to how you get two methods for compressing files: command-line and GUI. To decompress ZIP files, both techniques rely on the unzip software you previously installed.

UnUsing the Command Line to Zip Files

Let’s start unzipping files using the command-line technique, as you’ll be executing commands in a command-line environment throughout this lesson.

Launch your terminal and type the following command to unzip a ZIP file. The command below will unzip the contents of the ZIP file (newdir.zip). By default, the contents of the (newdir.zip) ZIP file are extracted to the same directory (/mydir).

If you want to extract the contents of a ZIP file into a directory other than the working directory, use the -d option followed by the destination directory, such as unzip newdir.zip -d anotherdir.

In the same directory, decompressingIn the same directory, decompressing

UnUsing the GUI to Zip Files

If unzipping files through command-line sounds like a lot of effort, consider using the GUI way to unzip files in a few clicks. You’d also get to pick where to extract the files, just as with the command-line technique.

To unzip files, open File Manager, as explained in the Using the GUI to Zip Files section. Right click the ZIP package you’d like to extract, and select Extract Here, as shown below.

When you select Extract Here, Linux extracts all of the files from the ZIP package into the current working directory.

Select the Extract to option from the context menu to extract all files to a different directory.

Choosing an extraction techniqueChoosing an extraction technique

The Zip File’s Integrity is Verified

ZIP files may become damaged in specific cases, such as when they are downloaded from the internet. Alternatively, the ZIP file might have been moved to a corrupted storage device. It’s crucial to check their integrity since you could not realize they’re tainted on the surface.

Run the following command to check the zip file’s integrity. The zip command below searches the working directory recursively (-r) for the (newfiles.zip) zip file, then validates (-T) that it is in excellent shape.

The command below just checks the integrity of the ZIP file, not the files and subdirectories therein.

You may notice an indicator that the test was successful in the image below.

The Zip File's Integrity is VerifiedThe Zip File’s Integrity is Verified

When dealing with a huge zip file with several files, extracting a ZIP file with faulty files will just waste time and cause issues. As a result, testing the compressed contents in the ZIP file before extracting them is a smart idea.

Checking compressed files is similar to validating the integrity of a ZIP file, except that this time you’re testing each file inside the ZIP file rather than just the ZIP file itself.

Now, use the unzip command to check the status of each file within the (newdir.zip) zip file (-t).

The unzip package discovered no issues, as seen in the figure below.

Files for testing are included in the zip file.Files for testing are included in the zip file.

Perhaps you’d like to merely look within the ZIP file rather than extracting the contents. The -l option of the unzip command, fortunately for you, shows the contents of a zip file without extracting it.

To see the contents of a ZIP file, use the unzip command to list (-l) the contents of the zip file (newdir.zip) without extracting it.

Take a look at the snapshot below, which shows a table listing the contents of the (newdir.zip) ZIP file.

Viewing the contents of a zip file without first decompressing itViewing the contents of a zip file without first decompressing it

Files with the Same File Extension Zipping

Perhaps you have a directory with many different files but only need to compress those that match a specific extension. To demonstrate how to script some common use cases with the zip utility, suppose you have a directory with many different files but only need to compress those that match a specific extension. To do so, you’ll need a script that will search for all files with the same extension over and over again. When they’re located, they’re added to a zip file.

Let’s have a look at what the script does and how to use it.

The script declares two variables: one for the file extension value (*.txt) and another for the target directory (targetDir) (anotherDir).

The script then uses a for loop to scan the target directory for all files with the.txt (files) extension (anotherDir). When a.txt file is detected, the zip command adds it to the (mynewfiles.zip) ZIP file using the -u option. Are you ready to automate the process of compressing files?

1. Open a text editor and save the script with the.sh extension (for example, script.sh), which is the extension for shell programming language files.

targetDir=anotherDir # Declare variable to hold the target directory for file in $files; do files=*.txt # Declare variable to store the.txt extension # Adds found.txt files to mynewfiles.zip file zip -u $targetDir/mynewfiles $file #

2. Modify the script’s permissions to ensure that you can execute it. To do so, use the chmod command to change (+x) the execute permission of the script (script.sh).

3. Now enter the command below to run the script. As long as the script (script.sh) is located in the current working directory, the command below will execute it.

There is a warning popup that says /mynewfiles.zip does not exist, as you can see. But don’t worry, the script will produce the zip file for you.

Using a script to automatically zip filesUsing a script to automatically zip files

Conclusion

You’ve learnt how to handle zip files (compress and decompress) and a practical example of how to automate the zipping process throughout this lesson. You are now a “zip master,” as promised!

When it comes to zipping, would you prefer to use manual or automatic methods?

The “linux zip directory recursive” is a command line tool that allows users to manage zip files in Linux. The tool can be used recursively or not.

Related Tags

  • how to zip folder in linux
  • linux zip folder and subfolders
  • unzip linux
  • linux unzip windows zip file
  • install zip linux

Table of Content