简体   繁体   English

bash-如何查找和压缩包含字符串的文件?

[英]bash - how to find and zip files that contain a string?

I want to zip all files contain string '12345' to file mylog.zip. 我想将所有包含字符串'12345'的文件压缩为mylog.zip文件。 Using grep -li can find the file and use this command but it does not zip the files. 使用grep -li可以找到文件并使用此命令,但不会压缩文件。

grep -l 12345  *  |  zip  mylog.zip; 

I try command 我尝试命令

grep -l 12345  *

It found the files. 它找到了文件。 Issue is how to pass this to zip. 问题是如何将其传递给zip。

find -name "*12345*" -type f -print | zip name.zip -@

要么

find -name "*12345*" -type f -exec zip name.zip {} +

Another using bash for loop. 另一个使用bash for循环。 Add proper quoting around variables if needed. 如果需要,请在变量周围添加适当的引号。 In this example I grep for a in a bunch of files: 在这个例子中,我用grep了a在一堆文件:

$ for f in *.txt; do grep -q a $f; if [ $? -eq 0 ]; then zip my.zip $f ; fi ; done
  adding: test1.txt (stored 0%)
  adding: test3.txt (stored 0%)

Written open: 书面开放:

for f in *.txt
do 
    grep -l a $f
    if [ $? -eq 0 ]
    then 
        zip my.zip $f
    fi
done

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

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