Zip and Unzip commands in Linux/Unix

Filed Under: UNIX/Linux

In this guide, we will focus on zip and unzip commands in Linux. zip command is a utility commonly used to compress files, while unzip command is used for uncompressing or unzipping files. Let’s have a look at each of these commands in detail.

Compressing files using the zip command

To create a compressed file with a .zip file extension, use the zip syntax as shown

zip {options} zipfile file_name

How to compress files with zip command

To compress a single file to a zip file without any arguments use the syntax below

$ zip zipfile file_name

Let’s create a file file1.doc using touch command

$ touch file1.doc

Next, we are going to zip or compress it to files.zip

$ zip files.zip file1.doc

Output

Zip A Single File

To verify the creation of the zipped file use the ls command as shown

ls -l

Output

Display Zipped File

Similarly, you can zip multiple files into one zipped file. Suppose we have 4 files as shown


file1.doc 
file2.doc
file3.doc
file4.doc

To zip the files

$ zip files.zip file1.doc file2.doc file3.doc file4.doc

Output

Zip Multiple Files

Once again, if you wish to verify the results, use the ls command as shown below

Output

Display Zipped Files

Adding a new file to a pre-existing compressed file

Let’s assume you have created 2 more files; file5.doc and file6.doc and you want to include it to the zipped file.

Use the -u argument as shown below

$ zip files.zip file5.doc file6.doc

Output

Add Files To A Zipped File

Deleting a file from a compressed file

To delete a file from an archive or a compressed file, use the -d flag as shown

$ zip -u compressed_file file_name

Suppose you want to delete file2.doc and file3.doc from the compressed file. The command will be

$ zip -d files.zip file2.doc file3.doc

Output

Deleting Files From A Compressed File

Deleting original files after arching or compressing

To delete files after compressing , use the -m option as shown

$ zip -m archive file_name

In our example where we have the following files,


file1.doc 
file2.doc
file3.doc
file4.doc

The command shall be

$ zip -m files.zip *.doc

OR

$ zip -m files.zip file1.doc file2.doc file3.doc file4.doc

Output

Delete Files After Zipping

Zipping a directory recursively

If you wish to zip a directory recursively, use the -r option. This command will zip all the files in the directory and save on space.

The syntax for this will be

$ zip –r filename.zip directory_name

suppose we have a directory called data the following files


file1.doc
file2.doc
file3.doc
file4.doc
file5.doc

To zip it into a zip file called data_files.zip the command will be

$ zip -r data_files.zip data

Output

Zip Directories Recursively

Exclude a file from being zipped or compressed

To exclude a file from being compressed use the -x option as shown

$zip –x filename.zip file_to_be_excluded

In the previous example, to exclude file2.doc from getting compressed, run

$ zip -x data_files.zip file1.doc

Getting help with zip commands

If you are stuck and want to know more about the usage of zip command run

$ zip --help

Output

Zip Help

Additionally, you can visit the command’s man pages as shown

$ man zip

Output

Man Zip

Unzip command

Unzip command is the converse of zip command. It is used for decompressing or unzipping compressed files and comes with several options. The syntax for unzipping files is

$ unzip {option} file.zip

Checking the contents of a zipped directory before unzipping

If you wish to display the contents of an archive before unzipping, use the -l option as shown

$ unzip -l  file.zip

For instance

$ unzip -l data_files.zip

Output

List Files Before Unzipping

Unzipping files with verbose output

To unzip a zipped file/directory with verbose output, use the -v option as shown

$ unzip -v data_files.zip

Output

Unzip display verbose output

Unzipping files to a specific directory

If you want to unzip an archive to a specified directory , use the -d option as shown

$ unzip -d data_files.zip directory_name

To unzip the zip files into a directory called james, execute

$ unzip -d data_files.zip james

Output

Unzip To A Different Directory

Checking for errors in an archive

If you wish to check for any errors in the archive before uncompressing, use the -t flag as shown

$ unzip -t data_files.zip 

Output

Testing For Errors In Archive

Getting help with unzip commands

If you are stuck and want to know more about the usage of unzip command run

$ unzip --help

Output

Unzip Help

Additionally, you can visit the command’s man pages as shown

$ man unzip

Output

Man Unzip

That’s all we had for today. Your feedback is highly welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages