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.
You can use
cp `grep -l 'FOO' dirA/*` dirB/
grep -l will only output the name of the files matching the pattern.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With