在目录中的文件中查找字符串然后复制它们
find string inside files in a dir then copy them
我是 运行 centos 并且发现了类似的问题,但是没有人需要完全按照我的方式去做。
我想在以下任何文件中找到一个字符串:
/home/username/mail/.person1\@someemaildir_com/
在找到它们时或找到它们后,将找到的任何文件复制到此处的另一个文件夹:
/home/username/mail/.person2\@someemaildir_com/.Mail\ Folder/
这是我试过但没有成功的方法:
grep -lir 'stringtofind' /home/username/mail/.person1\@someemaildir_com/* | xargs cp -t /home/username/mail/.person2\@someemaildir_com/.Mail\ Folder/
我建议如下(假设您的 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
" $f 附近可能很重要,因为文件名或文件夹名中有空格
好的,其他任何正在寻找此答案的人。我改变了策略,让下面的方法运行得很好。
find /home/username/mail/.person1\@someemaildir_com/ -name '*' -exec grep -l "string to find" {} \; -exec cp {} /home/username/mail/.person2\@someemaildir_com/.Mail\ Folder/ \;
我是 运行 centos 并且发现了类似的问题,但是没有人需要完全按照我的方式去做。
我想在以下任何文件中找到一个字符串:
/home/username/mail/.person1\@someemaildir_com/
在找到它们时或找到它们后,将找到的任何文件复制到此处的另一个文件夹:
/home/username/mail/.person2\@someemaildir_com/.Mail\ Folder/
这是我试过但没有成功的方法:
grep -lir 'stringtofind' /home/username/mail/.person1\@someemaildir_com/* | xargs cp -t /home/username/mail/.person2\@someemaildir_com/.Mail\ Folder/
我建议如下(假设您的 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
" $f 附近可能很重要,因为文件名或文件夹名中有空格
好的,其他任何正在寻找此答案的人。我改变了策略,让下面的方法运行得很好。
find /home/username/mail/.person1\@someemaildir_com/ -name '*' -exec grep -l "string to find" {} \; -exec cp {} /home/username/mail/.person2\@someemaildir_com/.Mail\ Folder/ \;