Zip multiple files with whitespace shell script

I need to zip multiple files into one ZIP file. I need to keep the directory structure of the files in a zip file as well. The files to be zipped will be in a separate text file with full path. I read the filenames from a text file into a variable and passed it to the zip command. Here is the command I'm using.

zip -r $EXP_DIR/Export_files.zip $BASE_DIR -i "$file_name"

      

It works great for one file. But when I add multiple files it fails with below error.

zip error: Nothing to do! (zip file name)

      

I don't understand what is missing here. Any suggestion would be greatly appreciated.

Thanks, Guna

+3


source to share


1 answer


1) myfile.txt

contains filenames separated by newline character

zip out.zip -@ < myfile.txt

      

2) myfile.txt

contains filenames separated by spaces (no spaces allowed in filenames!)



zip out.zip -@ < <(tr ' ' '\n' < myfile.txt)

      

From man zip

:

If the file list is specified as -@

[Not on MacOS], zip displays a list of input files from standard input rather than from the command line.
For example zip -@ foo

will store files listed one per line per stdin infoo.zip

+2


source







All Articles