Linux zip multiple files

Linux zip files

Today we’ll be looking at how to zip up one or multiple files on Linux systems.
Zipping files can come in super handy when dealing with multiple files or backups that need to be transferred or moved. In my case, this situation occurred because I wanted to email attach multiple log files.

The Linux ZIP command

zip options zip_name files 
  • zip : The Linux command to zip things
  • options : We can pass in arguments for the end zip results
  • zip_name : The output name, without the .zip part
  • files : One or more files comma separated

To show you the most simple zip command for one file, we could do this.

This will create an archive.zip in that folder containing the test.txt file.

Installing zip on Ubuntu or Centos

To install the zip command on Ubuntu or Debian systems, you can run the following command.

For CentOS and Fedora systems, you can use Yum.

Zipping multiple files in Linux systems

To add multiple files, we can specify them with a space between like this.

zip archive test.txt test2.txt test3.txt 

However, in most cases, you want to zip a whole folder and its contents, right?

To achieve this, we can pass the -r option (Recursive).

zip -r archive my_directory 

This will take all the files inside my_directory and zip them into one archive.

As we did above, we can still add separate files in the zip by passing them along.

zip -r archive my_directory test.txt 

Linux zip options

So far, we have seen the -r option to add files in a folder recursively.

As you may know, there are multiple compressions levels for zip files. The default one is deflate . But we can also zip files using the bzip2 compression.

To use a different compression method, we can pass the -Z option.

zip -r -Z bzip2 archive my_directory 

We can also specify the level of compression we want.
This is passed with a number from 0-9.
Where 0 is no compression and 9 the highest.
The default compression is -6.

zip -r -9 archive my_directory test.txt 

Another cool option is to encrypt the zip. This means we put a password on it.

zip -r -e archive my_directory 

This will prompt you for the password twice.

# Enter password: # Verify password: 

Thank you for reading, and let’s connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Источник

How to create a zip with multiple files without sub-folders?

What I need to do: I would like to create a zip containing d0, d1 and d4 which must have the fallowing structure:

What I have already tried: I tried it with zip myfiles d0 f1/d1 f2/f3/d4 . But this keeps the original folder structure, which is not what I want.

3 Answers 3

You can use find to get the file list and execute zip -j myfiles to pack them ignoring the paths:

find . -name "d[014]" -exec zip -j myfiles <> + 

Example

$ tree . ├── d0 ├── f0 ├── f1 │ └── d1 └── f2 └── f3 ├── d2 ├── d3 └── d4 $ find . -name "d[014]" -exec zip -j myfiles <> + adding: d1 (stored 0%) adding: d4 (stored 0%) adding: d0 (stored 0%) $ unzip -l myfiles.zip Archive: myfiles.zip Length Date Time Name --------- ---------- ----- ---- 0 2017-10-09 10:47 d1 0 2017-10-09 10:48 d4 0 2017-10-09 10:47 d0 --------- ------- 0 3 files 

However this works for files only, directories will be ignored by zip -j . To get this working for directories too, say we want to pack d0 , d1 and the whole f3 directory in the above example, the find line gets a little more complicated:

$ find . \( -name "d[01]" -o -name "f3" \) -exec sh -c 'p=$(pwd); for i in $0 $@; do cd $; zip -ur "$p"/myfiles $; cd "$p"; done' <> + zip warning: /home/dessert/myfiles.zip not found or empty adding: d1 (stored 0%) adding: f3/ (stored 0%) adding: f3/d3 (stored 0%) adding: f3/d2 (stored 0%) adding: f3/d4 (stored 0%) adding: d0 (stored 0%) $ unzip -l myfiles.zip Archive: myfiles.zip Length Date Time Name --------- ---------- ----- ---- 0 2017-10-11 10:18 d1 0 2017-10-11 10:19 f3/ 0 2017-10-11 10:19 f3/d3 0 2017-10-11 10:19 f3/d2 0 2017-10-11 10:19 f3/d4 0 2017-10-11 10:17 d0 --------- ------- 0 6 files 

Источник

Читайте также:  Linux to do list software

Linux Zip Multiple Files

Linux Zip Multiple Files

The following article provides an outline for Linux Zip Multiple Files. Zip multiple files are defined as a command or utility which enables users to compress and package multiple files together. Now, compression of files can be performed in various ways. The first one is reducing the file size and then package them for later steps. The other way of compression is not through size but maybe collating different files at a different location to a single location before packaging them. In order to perform compression, there is a compression ratio that is maintained to reduce the file size but not interfere with the quality. The utilities post zipping can be either from archiving, saving space temporarily for unused files or directories.

Web development, programming languages, Software testing & others

Syntax of Linux Zip Multiple Files

In Linux, there are a lot of utilities of zip available for users, and later in the article, take a dig at the working of zip multiple files command in Linux. The parallel and analogous command to zip is the tar command.

2. Removal of a file from zip.

3. Updating or including a file in a zip.

4. Removal of original files after zip.

5. Recursive zip of folders.

6. Exclusion of a file while zipping.

7. Verbose mode during zip.

How to Zip Multiple Files in Linux?

In recent times, there are a lot of frequently occurring situations that the files downloaded from the internet are too many in numbers, or data is widely and more importantly present in huge amount. And while this happens, sharing files amongst each other possess a challenge. This calls for an immediate answer to solve all these problems, and that is where zipping multiple files is so important in today’s world. Zipping of files has been a practice since a while ago, and the quest for using it even more is getting its share of interest.

Here we would look at the process of zipping multiple files in Linux. The process is very similar to the one we perform in windows if one is familiar with the process in windows. The only difference lies in the way we process the same. In Linux, we perform it through the command line interface and in windows, there are tools like Zip, 7Zip, etc., to perform the same utilities.

Читайте также:  Primary and extended partitions in linux

Now, let us look at different options in the command zip when it comes to the sipping of multiple files.

Given below are the options:

  • d: This option is used for the deletion of a file in the zip.
  • u: This option is used for updating or including a file in the zip.
  • m: This option is used for the removal of the original file after the zip.
  • x: This option is used for the exclusion of files in the zip.
  • r: This option is used for recursive zip of folders.
  • v: This option is used for verbose print during zip.
  • dc: This option is used for displaying running counts of entries zipped.
  • e: This option is used for encrypting a zip file.
  • sf: This option is used for extraction of contents without actual extraction of zip.
  • 7: This option is used for moderating the level of compression during zip (0 being lowest and 9 being highest).
  • y: This option is used for preserving the symbolic links during zip.

From the above options available, we see that different options take care of different utilities of zipping multiple files. While some of them are widely used, some of them are essential for the quick turnaround of things. For example, running an unzip function and then extracting the content takes more time, rather than just using the option of sf. One more important thing is that we should make sure of the compression level during the compression of large files. We can reach at the highest level of compression, i.e. 9, given the case that the user is ready to take the burnt of higher time for high compression. In case it is not that important, one can use the default, which is 6. There is a possibility of symbolic links being present in the zip, and as for preserving the same, the option y should be used. Zip of multiple files allows users to use different compression method and one of them being bzip2.

Examples of Linux Zip Multiple Files

Given below are the examples of Linux Zip Multiple Files:

Example #1

Removal of a file from zip.

zip -sf eduCBA.zip zip -d eduCBA.zip file3.txt zip -sf eduCBA.zip

Linux Zip Multiple Files 1

Example #2

Updating or including a file in a zip.

zip -sf eduCBA.zip zip -u eduCBA.zip fileInclude.txt zip -sf eduCBA.zip

Linux Zip Multiple Files 2

Example #3

Removal of original files after zip.

ls -lrt zip -m eduCBA.zip *.txt ls -lrt

Removal of original

Here we see that all the txt files are deleted after they are zipped.

Example #4

zip -r eduCBA.zip zipDemo/ ls *.zip -lrt zip -sf eduCBA.zip

Recursive

Example #5

Exclusion of a file while zipping.

zip eduCBAexclude.zip *.txt -x file3.txt zip -sf eduCBAexclude.zip

Exclusion

Here we see that all the files are included in the zip, except the file3.txt.

Example #6

zip -v eduCBAverbose.zip *.txt

Linux Zip Multiple Files 6

Conclusion

With the set of examples and explanation of the way of working for zip command, the usage and utilizes are even more clear for the readers, and now we leave it up to readers to experiment with other options which are not explained in the examples, so as not to exhaust the article.

Читайте также:  Install ubuntu and linux mint

We hope that this EDUCBA information on “Linux Zip Multiple Files” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

502+ Hours of HD Videos
54 Courses
4 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5

57+ Hours of HD Videos
15 Courses
9 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5

Источник

How do I zip up multiple files on command line?

Forgive this most basic question, but I couldn’t find a direct answer or simple example on stack exchange. Let’s say I have a folder of files that I would like to compress into one zip file that I can share with my terrible Windows friends (otherwise I’d just use tar and be done with it). It looks like this:

. ├── file1.txt ├── file2.txt ├── file3.txt ├── file.jpg └── test.jpg 

-@ file lists. If a file list is specified as -@ [Not on MacOS], zip takes the list of input files from standard input instead of from the command line. For example,

cat zip.lst file1.txt file2.txt file3.txt file.jpg test.jpg 

But it didn’t do anything except create a blank line on the console. And I can keep pushing Enter and it just keeps making more blank lines without seemingly executing the command. After some searching, I realized that I needed to terminate input by pressing Ctrl + D But now I get this error:

 zip warning: missing end signature--probably not a zip file (did you zip warning: remember to use binary mode when you transferred it?) zip warning: (if you are trying to read a damaged archive try -F) zip error: Zip file structure invalid (zip.lst) 

What!? How is my simple list not a «valid file structure»? Back to the manual for more information, and I take a closer look at:

So I try it without -@ , and that at least processes it right away without having to press Ctrl + D but I’m left with the same error. Luckily, I found a comment on an answer to a nearly unrelated question that led me to realize that I needed to explicitly name the zip file. This error’d because by default it was trying to create a zip file called zip.lst . But since that’s the name of my list, one might think that it would simply overwrite it, but no, it was in fact trying to update it. And since the list obviously isn’t a zip file, we get the invalid file structure error, and now it’s clear why it said «probably not a zip file.» So then I tried:

zip files.zip zip.lst adding: zip.lst (deflated 35%) 
. ├── file1.txt ├── file2.txt ├── file3.txt ├── file.jpg ├── files.zip ├── test.jpg └── zip.lst 
unzip -l files.zip Archive: files.zip Length Date Time Name --------- ---------- ----- ---- 48 2016-05-24 15:30 zip.lst --------- ------- 48 1 file 

No! Well, actually that makes sense from standard usage because it just zipped up the one list file I gave it. Finally I tried again with -@ but got the same result. What am I doing wrong? BTW, I know I can use the GUI and do it with my mouse, but I need to script this, and in general I’m faster on the CLI when I know what I’m doing.

Источник

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