使用grep在文件中查找内容,如果匹配就移动它们

时间:2022-06-02 00:27:14

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

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

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

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

如何将列表传递给mv命令并将文件移到其他地方?

9 个解决方案

#1


64  

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

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

z表示在文件名之后输出0(\0)(因此空格不用作递归表)。

xargs -0

means interpret \0 to be delimiters.

表示将\0解释为分隔符。

Then

然后

-I{} mv {} DIR

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

意思是用文件名替换{},因此您将获得mv文件名DIR。

#2


16  

This alternative works where xargs is not availabe:

这种替代方法在xargs无效的情况下有效:

grep -L -r 'Subject: \[SPAM\]' . | while read f; do mv "$f" out; done

#3


4  

This is what I use in Fedora Core 12:

这是我在Fedora Core 12中使用的:

grep -l 'Subject: \[SPAM\]' | xargs -I '{}' mv '{}' DIR

#4


2  

This is what helped me:

这帮助了我:

grep -lir 'spam' ./ | xargs mv -t ../spam

垃圾邮件。/ | xargs mv -t ../垃圾邮件

Of course, I was already in required folder (that's why ./) and moved them to neighboring folder. But you can change them to any paths.

当然,我已经在required文件夹中(这就是为什么。/),并将它们移动到相邻的文件夹。但是你可以把它们变换成任何路径。

I don't know why accepted answer didn't work. Also I didn't have spaces and special characters in filenames - maybe this will not work.

我不知道为什么大家接受的答案不管用。另外,我在文件名中没有空格和特殊字符——这可能行不通。

Stolen here: Grep command to find files containing text string and move them

此处窃取:Grep命令查找包含文本字符串的文件并移动它们

#5


1  

There are several ways but here is a slow but failsafe one :

有几种方法,但这里有一个缓慢但不会失败的方法:

IFS=$'\n'; # set the field separator to line break
for $mail in $(grep -L -r 'Subject: \[SPAM\]' .); do mv "$mail" your_dir; done;
IFS=' '; # restore FS

#6


1  

Maybe this will work:

也许这将工作:

mv $(grep -l 'Subject: \[SPAM\]' | awk -F ':' '{print $1}') your_file

#7


1  

mv `grep -L -r 'Subject: \[SPAM\]' .` <directory_path>

Assuming that the grep you wrote returns the files paths you're expecting.

假设您编写的grep返回您期望的文件路径。

#8


0  

Work perfect fo me :

完美的工作:

  • move files who contain the text withe the word MYSTRINGTOSEARCH to directory MYDIR.

    移动文件,其中包含文字MYSTRINGTOSEARCH到目录MYDIR。

    find . -type f -exec grep -il 'MYSTRINGTOSEARCH' {} \; -exec mv {} MYDIR/ \;

    找到。-type f -exec grep -il 'MYSTRINGTOSEARCH' {};-执行mv {} MYDIR/ \;

I hope this helps

我希望这有助于

#9


-1  

You can pass the result to the next command by using grep ... | xargs mv {} destination

您可以使用grep将结果传递给下一个命令。目标

Check man xargs for more info.

查看man xargs以获取更多信息。

#1


64  

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

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

z表示在文件名之后输出0(\0)(因此空格不用作递归表)。

xargs -0

means interpret \0 to be delimiters.

表示将\0解释为分隔符。

Then

然后

-I{} mv {} DIR

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

意思是用文件名替换{},因此您将获得mv文件名DIR。

#2


16  

This alternative works where xargs is not availabe:

这种替代方法在xargs无效的情况下有效:

grep -L -r 'Subject: \[SPAM\]' . | while read f; do mv "$f" out; done

#3


4  

This is what I use in Fedora Core 12:

这是我在Fedora Core 12中使用的:

grep -l 'Subject: \[SPAM\]' | xargs -I '{}' mv '{}' DIR

#4


2  

This is what helped me:

这帮助了我:

grep -lir 'spam' ./ | xargs mv -t ../spam

垃圾邮件。/ | xargs mv -t ../垃圾邮件

Of course, I was already in required folder (that's why ./) and moved them to neighboring folder. But you can change them to any paths.

当然,我已经在required文件夹中(这就是为什么。/),并将它们移动到相邻的文件夹。但是你可以把它们变换成任何路径。

I don't know why accepted answer didn't work. Also I didn't have spaces and special characters in filenames - maybe this will not work.

我不知道为什么大家接受的答案不管用。另外,我在文件名中没有空格和特殊字符——这可能行不通。

Stolen here: Grep command to find files containing text string and move them

此处窃取:Grep命令查找包含文本字符串的文件并移动它们

#5


1  

There are several ways but here is a slow but failsafe one :

有几种方法,但这里有一个缓慢但不会失败的方法:

IFS=$'\n'; # set the field separator to line break
for $mail in $(grep -L -r 'Subject: \[SPAM\]' .); do mv "$mail" your_dir; done;
IFS=' '; # restore FS

#6


1  

Maybe this will work:

也许这将工作:

mv $(grep -l 'Subject: \[SPAM\]' | awk -F ':' '{print $1}') your_file

#7


1  

mv `grep -L -r 'Subject: \[SPAM\]' .` <directory_path>

Assuming that the grep you wrote returns the files paths you're expecting.

假设您编写的grep返回您期望的文件路径。

#8


0  

Work perfect fo me :

完美的工作:

  • move files who contain the text withe the word MYSTRINGTOSEARCH to directory MYDIR.

    移动文件,其中包含文字MYSTRINGTOSEARCH到目录MYDIR。

    find . -type f -exec grep -il 'MYSTRINGTOSEARCH' {} \; -exec mv {} MYDIR/ \;

    找到。-type f -exec grep -il 'MYSTRINGTOSEARCH' {};-执行mv {} MYDIR/ \;

I hope this helps

我希望这有助于

#9


-1  

You can pass the result to the next command by using grep ... | xargs mv {} destination

您可以使用grep将结果传递给下一个命令。目标

Check man xargs for more info.

查看man xargs以获取更多信息。