Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying files containing specific text in its content from a folder to other

Tags:

unix

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.

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.

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.

like image 250
user1588737 Avatar asked Nov 06 '25 22:11

user1588737


1 Answers

You can use

cp `grep -l 'FOO' dirA/*` dirB/

grep -l will only output the name of the files matching the pattern.

like image 115
tomahh Avatar answered Nov 08 '25 13:11

tomahh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!