zip warning - name not matched
Asked Answered
B

2

7

I am getting the following error while using zip in my bash script

zip warning: name not matched: test.png test2.png

#!/bin/bash
files_to_zip="test.png test2.png"
zipfile_name=result$(date "+%Y.%m.%d-%H.%M.%S").zip
zip "$zipfile_name"  "$files_to_zip"

Note: The images are in the same directory as the script and when i execute zip test.zip test.png test2.png , the zip get created just fine.

Brittani answered 2/9, 2017 at 15:13 Comment(9)
If you substitute your vars into command you'll get zip "result-2017-09-02.zip" "test.png test2.png" this is different from what you want to achieveCaterpillar
The name of the zip is an example , i use test.zip for simplicityBrittani
I mean your last parameter goes into same "" so is treated as one file name which will not be foundCaterpillar
@Artemy Vysotsky but $files_to_zip contain list of files as a string separated by spaces so zip should know they are different filenamesBrittani
Why you have not tried zip test.zip "test.png test2.png"? If you sure zip has to understand itCaterpillar
Ahh you are right....zip is treating it as a single filename.What would be the most elegant solution in this situationBrittani
Well use zip "$zipfile_name" $files_to_zip and if you have special chars or spaces inside the names - add quotes inside the files_to_zip variableCaterpillar
How could i missed that...word splitting is exactly what was needed here.#Lackofsleep .Post it as an answerBrittani
Note that this also shows if you try to zip symlinks that point to non-existing files. In this case add --symlinksScag
C
8

When names are combined inside same quotes whole string is treated as file name. Use

zip "$zipfile_name" $files_to_zip

instead. And if your png names have special characters like spaces - add quotes or escape this characters inside the $files_to_zip variable

Caterpillar answered 2/9, 2017 at 16:5 Comment(0)
L
0

add this line before sorting the files in directory

IFS=$'\n' 
files=($(ls | sort))

This worked for me and handled for numbers, dashes, special characters in the input files that are being converting to the zip file

Linkoski answered 21/11, 2023 at 12:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.