Linux zip folder with subfolders

How to zip files and folders in Linux

Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2023 with this popular free course.

To zip a file in Linux or macOS, we can use the zip command.

Syntax for the zip command

zip [options] zipfile_name filename_separated_by_space 

If the zip command is not present in your system, install it with:

Suppose we are in the folder project and the following is the structure of the project folder.

project │ README.md │ file1.txt │ │ └───folder_1 │ │ file011.txt │ │ file012.txt │ │ │ └───subfolder_1 │ │ file1_1_1.txt │ │ file1_1_2.txt │ │ . │ └───folder_2 │ file2_1.txt │ file2_2.txt 

Zipping files and folders

To zip files, we need the zipped filename and the files to be included in the zip file.

The zip command prints the names of the files added to the zip file and the compression method the default compression method is deflated . Now, a new zip file will be created.

adding: README.md (stored 77%) 

To hide the above message, we can use the -q option.

The above code will not print the files added or the compression method in the terminal.

To zip all the files in a folder, we can use * instead of specifying all the filenames.

The above command will zip all the files and folders (files inside the folder are not included) in the current directory.

To zip a folder, we can add the following folder name.

Читайте также:  Kali linux bruteforce http

The above code will only zip the files in the folder_1. The files in the subfolders of folder_1 will not be compressed.

To add all files in the folder and subfolder, we can use the -r command, which will compress all the files in the folder.

zip -r folders.zip folder_1 

The above code will zip all the files recursively in folder_1. The folder structure is maintained in the zip file.

We can also combine multiple folders and files by recursively zipping files.

zip -r multiplefolder.zip folder_1 folder_2 README.md 

Updating or adding a file to the zip file

Let’s add file1.txt and update README.md in text.zip.

zip -u text.zip file1.txt README.md 

The zip file will only be updated if the existing file is modified or if the file is not present in the zip.

Deleting a file from a compressed file

To delete a file from the compressed file, we can use -d .

zip -d multiplefolder.zip README.md 

Removing files after zipping

We can use the -m command to delete the original files after the zip file is created.

The above command will delete README.md.

Creating a password-protected zip file

We can use -e to create a password-protected zip file.

The above command will ask for a password.

Enter password: Verify password: 

Changing compression levels

The compression level defines how optimally the files can be compressed. The compression levels range from 0-9 but, by default, the compression level is 6. To change the compression level, we can use -compression_number .

The above code will zip files with the optimal compression. If we give -9, the CPU takes more time to create the zip, but the zip file size will be less. If we specify -0, the zip files are created without compression.

Источник

Zip a Folder in Ubuntu Command Line

Zip folder in Ubuntu

When you try to zip a folder in Ubuntu, you’ll notice that it creates an empty zip file.

[email protected]:~# zip toto.zip toto adding: toto/ (stored 0%)

The reason here is that by default, zip expects a file, not a folder. And like many other Linux commands, you’ll have to use the recursive option to deal with the directories.

Читайте также:  Semaphores and mutex in linux

To zip a folder, use it like this:

zip -r output.zip input_folder

Let’s see it in a bit more detail and with proper examples.

Zip a directory

Many Linux commands like rm, cp, scp etc use the recursive option -r while dealing with the folders. The zip command is no different.

As you have already seen above, the way to create a zip archive from a folder is to use it in the following fashion:

zip -r output.zip input_folder

Let me show it with an example. I try to zip a folder named toto in the usual (but incorrect) way:

[email protected]:~# zip toto.zip toto adding: toto/ (stored 0%) [email protected]:~#

And as you can see, the zipped folder doesn’t store the contents of the folder.

[email protected]:~# ls -lh toto.zip -rw-r--r-- 1 root root 160 Oct 27 05:12 toto.zip [email protected]:~#

Now, I use the same command but with the recursive option -r :

[email protected]:~# zip -r toto.zip toto updating: toto/ (stored 0%) adding: toto/ads.txt (deflated 80%) adding: toto/gnome-console-voiceover (deflated 57%) adding: toto/members.2022-05-29.csv (deflated 59%) adding: toto/cronjob-cheatsheet.png (deflated 8%) adding: toto/routes.yaml (deflated 27%) adding: toto/bash.pdf (deflated 22%) adding: toto/apt-get.pdf (deflated 8%) adding: toto/.member.csv (deflated 59%)

You can clearly see that the files are being added to the zipped folder now.

Zip folder in Ubuntu command line

Zip several folders and files in one zip file

You are not limited to zipping a single folder. You can zip multiple folders into one.

In fact, you can combine folders and files.

zip -r output.zip file1 folder1 file2 file3 folder2
[email protected]:~# zip -r single.zip auth.log toto logs alternatives.log adding: auth.log (deflated 89%) adding: toto/ (stored 0%) adding: toto/ads.txt (deflated 80%) adding: toto/gnome-console-voiceover (deflated 57%) adding: toto/members.2022-05-29.csv (deflated 59%) adding: toto/cronjob-cheatsheet.png (deflated 8%) adding: toto/routes.yaml (deflated 27%) adding: toto/bash.pdf (deflated 22%) adding: toto/apt-get.pdf (deflated 8%) adding: toto/.member.csv (deflated 59%) adding: logs/ (stored 0%) adding: logs/toto/ (stored 0%) adding: logs/toto/ads.txt (deflated 80%) adding: logs/toto/gnome-console-voiceover (deflated 57%) adding: logs/toto/members.2022-05-29.csv (deflated 59%) adding: logs/toto/cronjob-cheatsheet.png (deflated 8%) adding: logs/toto/routes.yaml (deflated 27%) adding: logs/toto/bash.pdf (deflated 22%) adding: logs/toto/apt-get.pdf (deflated 8%) adding: logs/toto/.member.csv (deflated 59%) adding: logs/alternatives.log (deflated 39%) adding: logs/auth.log (deflated 89%) adding: logs/fontconfig.log (deflated 86%) adding: alternatives.log (deflated 39%) 

Zip folder but exclude some files and subdirectories

You may not need all the files of a folder while creating the zip archive file. The good news is that you can exclude files and sub-directories while zipping them.

Читайте также:  Изменить название папки линукс

You can provide the files and subdirectories to exclude with option -x .

zip -r output.zip my_file my_folder -x my_folder/my_sub_folder

Do note that the option -x comes after the output and input files.

In the example below, you can see the file bash.pdf and subdirectory new is not

[email protected]:~# zip -r output.zip auth.log toto -x toto/new toto/bash.pdf adding: auth.log (deflated 89%) adding: toto/ (stored 0%) adding: toto/ads.txt (deflated 80%) adding: toto/gnome-console-voiceover (deflated 57%) adding: toto/members.2022-05-29.csv (deflated 59%) adding: toto/new/ (stored 0%) adding: toto/cronjob-cheatsheet.png (deflated 8%) adding: toto/routes.yaml (deflated 27%) adding: toto/.member.csv (deflated 59%) 

As you can see, it still adds the sub-directory but not its contents. If you don’t want the subdirectory at all, use it like this:

zip -r output.zip my_file my_folder -x "my_folder/my_sub_folder/*"

You can see that subdirectory new is not included at all this time.

[email protected]:~# zip -r simple.zip auth.log toto -x "toto/new/*" toto/bash.pdf adding: auth.log (deflated 89%) adding: toto/ (stored 0%) adding: toto/ads.txt (deflated 80%) adding: toto/gnome-console-voiceover (deflated 57%) adding: toto/members.2022-05-29.csv (deflated 59%) adding: toto/cronjob-cheatsheet.png (deflated 8%) adding: toto/routes.yaml (deflated 27%) adding: toto/.member.csv (deflated 59%) 

Conclusion

Well, there is a lot more to the zip command in Linux. The focus here was on zipping a folder in Ubuntu and this article covers it well.

Let me know if you still have some doubts.

Источник

Оцените статью
Adblock
detector