2017-07-27 67 views
-1

我想將包含字符串'12345'的所有文件壓縮到文件mylog.zip。 使用grep -l我可以找到該文件並使用此命令,但它不壓縮文件。bash - 如何查找和壓縮包含字符串的文件?

grep -l 12345 * | zip mylog.zip; 

我試着命令

grep -l 12345 * 

它找到的文件。問題是如何將此傳遞給zip。

+0

grep的-r -l 12345 ./ | zip files.zip - @ – gzh

回答

1
find -name "*12345*" -type f -print | zip name.zip [email protected] 

OR

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

嗨,你能解釋一下 - @在第一個命令中是什麼? – suhao399

+1

@ hellio6'man zip':_- @文件列表。如果指定了一個文件列表, - @ [未在MacOS],拉鍊需要從標準輸入,而不是從line_ –

1

另一個使用bash for迴路。如果需要,在變量周圍添加適當的引號。在這個例子中,我在一堆文件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%) 

編寫的Open:

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

你甚至可以用'grep的-q' –

+0

@AkshayHegde感謝命令輸入文件的列表。 '-l'是另一個實驗的剩餘物。 –

相關問題