简体   繁体   中英

zip multiple files with whitespace shell script

I need to zip multiple files into one zip file. I need to preserve the directory structure of files in the zip file as well. The files to be zipped will be in a separate text file with full path. I read the file names from the text file into a variable and pass it to the zip command. Here is the command i use.

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

It works fine for single file. But when i add multiple files it fails with the below error.

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

I do not understand what i am missing here. Any suggestion would be greatly appreciated.

Thanks, Guna

1) myfile.txt contains filenames delimited by newline

zip out.zip -@ < myfile.txt

2) myfile.txt contains filenames delimited by whitespace (no whitespace allowed in filenames!)

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

From man zip :

If a file list is specified as -@ [Not on MacOS], zip takes the list of input files from standard input instead of from the command line.
For example, zip -@ foo will store the files listed one per line on stdin in foo.zip

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM