Linux 使用 grep 在文件中查找内容并在匹配时移动它们
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/91899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use grep to find content in files and move them if they match
提问by user17582
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 命令并将文件移动到其他地方?
采纳答案by daveb
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) 的输出(因此空格不用作分隔符)。
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 filenames DIR
.
回答by Confusion
You can pass the result to the next command by using grep ... | xargs mv {} destination
您可以使用 grep ... 将结果传递给下一个命令。xargs mv {} 目的地
Check man xargs for more info.
检查 man xargs 以获取更多信息。
回答by e-satis
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
回答by Tobias Kunze
This alternative works where xargs is not availabe:
此替代方法适用于 xargs 不可用的情况:
grep -L -r 'Subject: \[SPAM\]' . | while read f; do mv "$f" out; done
回答by Brad Vokey
This is what I use in Fedora Core 12:
这是我在 Fedora Core 12 中使用的:
grep -l 'Subject: \[SPAM\]' | xargs -I '{}' mv '{}' DIR
回答by Ritz
Maybe this will work:
也许这会奏效:
mv $(grep -l 'Subject: \[SPAM\]' | awk -F ':' '{print }') your_file
回答by Mike Castro Demaria
Work perfect fo me :
对我来说很完美:
move files who contain the text withe the word MYSTRINGTOSEARCH to directory MYDIR.
find . -type f -exec grep -il 'MYSTRINGTOSEARCH' {} \; -exec mv {} MYDIR/ \;
将包含带有单词 MYSTRINGTOSEARCH 的文本的文件移动到目录 MYDIR。
找 。-type f -exec grep -il 'MYSTRINGTOSEARCH' {} \; -exec mv {} MYDIR/ \;
I hope this helps
我希望这有帮助
回答by vladkras
This is what helped me:
这对我有帮助:
grep -lir 'spam' ./ | xargs mv -t ../spam
grep -lir 'spam' ./ | xargs mv -t ../spam
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.
当然,我已经在所需的文件夹中(这就是为什么./
)并将它们移动到相邻的文件夹中。但是您可以将它们更改为任何路径。
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
回答by Loukan ElKadi
mv `grep -L -r 'Subject: \[SPAM\]' .` <directory_path>
Assuming that the grep you wrote returns the files paths you're expecting.
假设您编写的 grep 返回您期望的文件路径。