How to create a zip file and ignore directory structure in Linux?


In order to be able to zip file and ignore directory structure that gets created with it, we first must understand what the zip command on Linux actually means and how we can make use of it.

The zip command that Linux provides us with allows us to specify a compression level with a number prefixed with dash and that number is usually in between 0 and 9. In simple terms, it is used to reduce the size of files with the help of compression and sometimes also used as a file package facility.

The compression of files actually involves one or more compressed filed and it compresses them into a single zip archive, along with some information about those files.

Syntax

zip [OPTIONS] zipfile files

Example 1

Consider a case where you want to convert a single file into a zip file, then the command shown below will allow you to do just the same.

Command

zip file.zip sample.txt

Output

-rw-r--r-- 1 immukul staff 661 Jul 14 09:00 file.zip

Example 2

You can also unzip the zip file to get the files back with the help of the command shown below −

Command

unzip file.zip

Output

-rw-r--r-- 1 immukul staff 661 Jul 14 09:02 sample.txt

In order to make sure that you don’t get a directory structure mimicking the files, you can make use of the command shown below

Command

zip -j zipContent d1/file.txt d2/2.txt d2/3.txt

In the above command, we can notice the -j option which stands for “junk the path”, that will help you in ignoring the directory structure.

Output

-rw-r--r-- 1 immukul staff 2201512 Jul 14 09:19 zipContent.zip

Updated on: 29-Jul-2021

801 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements