zip command not working
Asked Answered
B

8

15

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.

Broadside answered 11/12, 2013 at 21:2 Comment(1)
Can you provide an ls -ltr for myfile.dat? I've heard of some issues with files that are too big and/or are symbolic links.Invaluable
M
11

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

Mauricemauricio answered 11/12, 2013 at 21:5 Comment(5)
But I am bale to see that file in the given folderBroadside
As I said whichever is the last file in list is having that error. If I try to zip type.txt file along with $FILES file then type.txt is having that warning and is not getting included in zip!Broadside
Then we need more details. What is in $FILES? Show an ls as requested. The following "works for me": $ mkdir ziptest $ cd ziptest $ touch a b c $ mkdir d $ touch d/e $ zip foo.zip a b c d/e adding: a (stored 0%) adding: b (stored 0%) adding: c (stored 0%) adding: d/e (stored 0%)Dorsman
How do you create $FILES?Mauricemauricio
I'm willing to bet there's an carriage-return on the end of your $FILES list.Pantelegraph
W
5

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).

Wier answered 3/4, 2017 at 1:11 Comment(0)
B
1

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

Broadside answered 11/12, 2013 at 22:23 Comment(2)
Both ./myscript.sh and bash myscript.sh should work. 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.Mauricemauricio
@Mauricemauricio You are right, there was a carriage return. But there is still problem if I run the script with 'bash myscript.sh'. But with ./myscript.sh my script is running perfectly fine!Broadside
W
1

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).

Waterline answered 7/4, 2015 at 14:42 Comment(0)
C
0

spaces are not allowed:

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

Calfskin answered 18/4, 2014 at 0:21 Comment(0)
C
0

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".

Calliper answered 29/9, 2014 at 14:56 Comment(0)
E
0

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.

Exegesis answered 11/4, 2020 at 5:58 Comment(0)
T
0

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

Thenna answered 1/12, 2023 at 9:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.