简体   繁体   English

使用空白shell脚本压缩多个文件

[英]zip multiple files with whitespace shell script

I need to zip multiple files into one zip file. 我需要将多个文件压缩成一个zip文件。 I need to preserve the directory structure of files in the zip file as well. 我还需要保留zip文件中文件的目录结构。 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. 我将文本文件中的文件名读入变量并将其传递给zip命令。 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 1) myfile.txt包含由换行符分隔的文件名

zip out.zip -@ < myfile.txt

2) myfile.txt contains filenames delimited by whitespace (no whitespace allowed in filenames!) 2) myfile.txt包含由空格分隔的文件名(文件名中不允许有空格!)

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

From man zip : 来自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. 如果文件列表指定为-@ [Not on MacOS],则zip 从标准输入而不是命令行 获取 输入文件 列表
For example, zip -@ foo will store the files listed one per line on stdin in foo.zip 例如, zip -@ foo将在foo.zip stdin上存储每行一个列出的文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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