Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zip command not working

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.

like image 261
Aparna Savant Avatar asked Dec 11 '13 21:12

Aparna Savant


People also ask

How do I zip a command line?

If you open a terminal console in the parent directory, or used the cd command to navigate there from the command line, you should then be able to run the command on the folder. The syntax is ' zip -r <zip file name> <directory name> '. The '-r' option tells zip to include files/folders in sub-directories.

How does zip command work?

The zip program puts one or more compressed files into a single zip archive, along with information about the files (name, path, date, time of last modification, protection, and check information to verify file integrity). An entire directory structure can be packed into a zip archive with a single command.

What is the zip command?

The zip command is a command-line tool in Linux that allows us to create an archive of files and directories. Besides that, it also provides a multitude of functionalities for manipulating an archive.

How do I run a zip file in Git bash?

By default, it is installed under the directory /c/Program Files/7-Zip in Windows 10 as my case. Run git Bash under Administrator privilege and navigate to the directory /c/Program Files/Git/mingw64/bin , you can run the command ln -s "/c/Program Files/7-Zip/7z.exe" 7z.exe.


1 Answers

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

like image 102
janos Avatar answered Oct 18 '22 13:10

janos