Linux 我如何搜索文件并将它们压缩到一个 zip 文件中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13234850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:42:07  来源:igfitidea点击:

how can i search for files and zip them in one zip file

linuxoperating-system

提问by LOGAN

I tried to search files and zip them with the following commmand

我尝试搜索文件并使用以下命令压缩它们

find . regexpression -exec zip {} \;

however it is not working. How can i do this?

但是它不起作用。我怎样才能做到这一点?

采纳答案by iabdalkader

The command you use will run zip on each file separately, try this:

您使用的命令将分别在每个文件上运行 zip,试试这个:

find . -name <name> -print | zip newZipFile.zip -@

The -@tells zip to read files from the input. From man zip(1),

-@告诉拉链从输入读取文件。从 man zip(1)

-@file lists.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 从标准输入而不是从命令行获取输入文件列表。

回答by jheddings

You can also provide the names as the result of your find command:

您还可以提供名称作为 find 命令的结果:

zip name.zip `find . -name <name> -print`

This is a feature of the shell you are using. You can search for "backticks" to determine how your shell handles this.

这是您正在使用的 shell 的一个特性。您可以搜索“反引号”来确定您的 shell 如何处理此问题。

回答by Dan Jones

Your response is close, but this might work better:

您的回答很接近,但这可能会更好:

find -regex 'regex' -exec zip filname.zip {} +

That will put all the matching files in one zip file called filename.zip. You don't have to worry about special characters in the filename (like a line break), which you would if you piped the results.

这会将所有匹配的文件放在一个名为 filename.zip 的 zip 文件中。您不必担心文件名中的特殊字符(如换行符),如果您通过管道传输结果,您会担心。