简体   繁体   English

在目录中的文件中找到字符串,然后将其复制

[英]find string inside files in a dir then copy them

i am running centos and have found similar questions, however no one needs to do exactly what i do. 我正在运行centos,并且发现了类似的问题,但是没有人需要完全按照我的意思做。

I want to find a string inside any file reclusivly inside of: 我想在其中的任何文件中找到一个字符串:

/home/username/mail/.person1\\@someemaildir_com/ / home /用户名/邮件/.person1\\@someemaildir_com/

as it finds them or after it finds them, COPY any file that it found to another folder here: 找到它们或找到它们之后,将找到的任何文件复制到此处的另一个文件夹中:

/home/username/mail/.person2\\@someemaildir_com/.Mail\\ Folder/ / home /用户名/mail/.person2 \\ @ someemaildir_com / .Mail \\文件夹/

Here is what i tried without it working: 这是我在没有工作的情况下尝试的方法:

grep -lir 'stringtofind' /home/username/mail/.person1\@someemaildir_com/* | xargs cp -t /home/username/mail/.person2\@someemaildir_com/.Mail\ Folder/

I'd propose following (assuming your grep "stringtofind" does what it should do): 我建议采取以下措施(假设您的grep "stringtofind"做了应做的事情):

grep -lir "stringtofind" /home/username/mail/.person1\@someemaildir_com/* | while read f; do
    cp "$f" /home/username/mail/.person2\@someemaildir_com/.Mail\ Folder/
done

" around $f may be important because of blanks in file- or folder-names “ $ f左右很重要,因为文件名或文件夹名中的空格

Ok for anyone else looking for this answer. 确定的任何其他人寻找此答案。 i switched tactics and got the below working very well. 我改变了策略,并取得了很好的下面。

find /home/username/mail/.person1\@someemaildir_com/  -name '*' -exec grep -l "string to find" {} \; -exec cp {} /home/username/mail/.person2\@someemaildir_com/.Mail\ Folder/ \;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM