将内容中包含特定文本的文件从文件夹复制到其他文件夹 [英] Copying files containing specific text in its content from a folder to other
问题描述
有时我会遇到这样的情况,我必须将内容中包含特定模式的所有文件从一个文件夹复制到另一个文件夹.
At times I have come across a situation whereby I have to copy all the files containing a specific pattern in its content from a folder to another.
例如,DirA
包含 100 个文件,其中有 60 个文件包含模式 FOO
.我的要求是将这 60 个文件从 DirA
复制到 DirB
.
For example, DirA
contains 100 files, out of which there are 60 files which contains a pattern FOO
. My requirement is to copy these 60 files from DirA
to DirB
.
我通常会编写一个小的 shell 脚本来完成这项工作,并且它可以正常工作.但是,我试图了解是否有一种方法可以仅使用某些命令的组合,这样我就不需要编写任何 shell 脚本.
I typically write a small shell script to do this job and it works properly. However, I am trying to understand if there is a way to it only using a combination of some commands such that I need not write any shell script.
推荐答案
可以使用
cp `grep -l 'FOO' dirA/*` dirB/
grep -l
只会输出匹配模式的文件名.
grep -l
will only output the name of the files matching the pattern.
这篇关于将内容中包含特定文本的文件从文件夹复制到其他文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!