使用 grep 在文件中查找内容并在匹配时移动它们 [英] Use grep to find content in files and move them if they match

查看:18
本文介绍了使用 grep 在文件中查找内容并在匹配时移动它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 grep 生成我需要移动的文件列表:

I'm using grep to generate a list of files I need to move:

grep -L -r 'Subject: [SPAM]' .

如何将此列表传递给 mv 命令并将文件移动到其他位置?

How can I pass this list to the mv command and move the files somewhere else?

推荐答案

如果您想查找和移动与您的模式不匹配的文件(移动不包含 'Subject [SPAM]' 的文件) 在这个例子中)使用:

If you want to find and move files that do not match your pattern (move files that don't contain 'Subject [SPAM]' in this example) use:

grep -L -Z -r 'Subject: [SPAM]' . | xargs -0 -I{} mv {} DIR

-Z 表示在文件名后带有零 () 的输出(因此不使用空格作为分隔符).

The -Z means output with zeros () after the filenames (so spaces are not used as delimeters).

xargs -0

表示将 解释为分隔符.

means interpret to be delimiters.

-L 表示查找与模式不匹配的文件.如果您想移动与您的模式匹配的文件,请将 -L 替换为 -l.

The -L means find files that do not match the pattern. Replace -L with -l if you want to move files that match your pattern.

然后

-I{} mv {} DIR

意味着用文件名替换{},这样你就得到mv filenames DIR.

means replace {} with the filenames, so you get mv filenames DIR.

这篇关于使用 grep 在文件中查找内容并在匹配时移动它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆