我想将包含字符串'12345'的所有文件压缩到文件mylog.zip。 使用grep -l我可以找到该文件并使用此命令,但它不压缩文件。bash - 如何查找和压缩包含字符串的文件?
grep -l 12345 * | zip mylog.zip;
我试着命令
grep -l 12345 *
它找到的文件。问题是如何将此传递给zip。
我想将包含字符串'12345'的所有文件压缩到文件mylog.zip。 使用grep -l我可以找到该文件并使用此命令,但它不压缩文件。bash - 如何查找和压缩包含字符串的文件?
grep -l 12345 * | zip mylog.zip;
我试着命令
grep -l 12345 *
它找到的文件。问题是如何将此传递给zip。
find -name "*12345*" -type f -print | zip name.zip [email protected]
OR
find -name "*12345*" -type f -exec zip name.zip {} +
嗨,你能解释一下 - @在第一个命令中是什么? – suhao399
@ hellio6'man zip':_- @文件列表。如果指定了一个文件列表, - @ [未在MacOS],拉链需要从标准输入,而不是从line_ –
另一个使用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
你甚至可以用'grep的-q' –
@AkshayHegde感谢命令输入文件的列表。 '-l'是另一个实验的剩余物。 –
grep的-r -l 12345 ./ | zip files.zip - @ – gzh