zip command not working

52.4k views Asked by At

I am trying to zip a file using shell script command. I am using following command:

  zip ./test/step1.zip $FILES

where $FILES contain all the input files. But I am getting a warning as follows

    zip warning: name not matched: myfile.dat

and one more thing I observed that the file which is at last in the list of files in a folder has the above warning and that file is not getting zipped.

Can anyone explain me why this is happening? I am new to shell script world.

8

There are 8 answers

2
Aparna Savant On

I converted my script for unix environment using dos2unix command and executed my script as ./myscript.sh instead bash myscript.sh.

5
janos On

zip warning: name not matched: myfile.dat

This means the file myfile.dat does not exist.

You will get the same error if the file is a symlink pointing to a non-existent file.

As you say, whatever is the last file at the of $FILES, it will not be added to the zip along with the warning. So I think something's wrong with the way you create $FILES. Chances are there is a newline, carriage return, space, tab, or other invisible character at the end of the last filename, resulting in something that doesn't exist. Try this for example:

for f in $FILES; do echo :$f:; done

I bet the last line will be incorrect, for example:

:myfile.dat :

...or something like that instead of :myfile.dat: with no characters before the last :

UPDATE

If you say the script started working after running dos2unix on it, that confirms what everybody suspected already, that somehow there was a carriage-return at the end of your $FILES list.

od -c shows the \r carriage-return. Try echo $FILES | od -c

0
Oussama Boumaad On

Although the Title is vast but I came into similar Warning issue when trying to zip a project created by docker-compose.yml where the data of mysql is saved as folder inside my project folder.

When I tried to zip, it gives same error, but zip could create an archive that contains all the files except mysql.sock. although the file exist but zip couldn't compress it. it's because the file is being used by mysql and constantly being red/written into and also it's protected.

so whenever you're trying to compress and such files you'll get that warning. So try to find such files and exclude them if you don't need them.

In my case I did zip -rq .... -x '**/mysql.sock' and it worked.

enter image description here

0
Sean On

I also encountered this issue. In my case, the line separate is CRLF in my zip shell script which causes the problem. Using LF fixed it.

0
chukko On

eebbesen hit the nail in his comment for my case (but i cannot vote for comment). Another possible reason missed in the other comments is file exceeding the file size limit (4GB).

0
David L On

I just discovered another potential cause for this. If the permissions of the directory/subdirectory don't allow the zip to find the file, it will report this error. Actually, if you run a chmod -R 444 on the directory, and then try to zip it, you will reproduce this error, and also have a "stored 0%" report, like this:

zip warning: name not matched: borrar/enviar adding: borrar/ (stored 0%)

Hence, try changing the permissions of the file. If you are trying to send them through email, and those email filters (like Gmail's) invent silly filters of not sending executables, don't forget that making permissions very strict when making zip compression can be the cause of the error you are reporting, of "name not matched".

0
Mr.T On

spaces are not allowed:

it would fail if there are more than one files(s) in $FILES unless you put them in loop

0
Dave Wood On

Another possible cause that can generate a zip warning: name not matched: error is having any of zip's environment variables set incorrectly.

From the man page:

ENVIRONMENT
    The following environment variables are read and used by zip as described.

ZIPOPT
    contains default options that will be used when running zip.  The contents of this environment variable will get added to the command line just after the zip command.

ZIP
    [Not on RISC OS and VMS] see ZIPOPT

Zip$Options
    [RISC OS] see ZIPOPT

Zip$Exts
    [RISC OS] contains extensions separated by a : that will cause native filenames with one of the specified extensions to be added to the zip file with basename and extension swapped.

ZIP_OPTS
    [VMS] see ZIPOPT

In my case, I was using zip in a script and had the binary location in an environment variable ZIP so that we could change to a different zip binary easily without making tonnes of changes in the script.

Example:

ZIP=/usr/bin/zip
...
${ZIP} -r folder.zip folder

This is then processed as:

/usr/bin/zip /usr/bin/zip -r folder.zip folder

And generates the errors:

zip warning: name not matched: folder.zip
zip I/O error: Operation not permitted
zip error: Could not create output file (/usr/bin/zip.zip)

The first because it's now trying to add folder.zip to the archive instead of using it as the archive. The second and third because it's trying to use the file /usr/bin/zip.zip as the archive which is (fortunately) not writable by a normal user.

Note: This is a really old question, but I didn't find this answer anywhere, so I'm posting it to help future searchers (my future self included).