find files with pattern, then copy those files to another directory
I am trying to find files that match a certain pattern and then copy just those files to another directory.
So far I have this, which find the files that match my pattern but I can't figure out how to then copy those to another directory.
find . -type f -maxdepth 1 -name "*" -exec grep -l "pattern" {} \;
I have tried adding
find . -type f -maxdepth 1 -name "*" -exec grep -l "pattern" -exec cp /directory {} \;
but that fails.
I've also tried a script which fails:
#!/bin/bash
for i in find . -type f -maxdepth 1 -name "*" -exec grep -l "pattern" {} \;
do
cp $i directory
done
but all that does is copy ALL the files into the new directory
edit:
cheap and dirty way that I figured out:
#!/bin/bash
find . -type f -maxdepth 1 -name "*" -exec grep -l "pattern" {} \; >> filename
for b in `cat filename`
do
cp $b test
done
Last edited by ncsuapex; 06-13-2008 at 03:19 PM.
Reason: .
|