zip command not working

Aparna Savant picture Aparna Savant · Dec 11, 2013 · Viewed 46.2k times · Source

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.

Answer

janos picture janos · Dec 11, 2013

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