On Thu, Oct 29, 2009 at 1:06 PM, Brett Russ <icycle@gmail.com> wrote:
On Thu, Oct 29, 2009 at 12:48 PM, Randall Mason <clashthebunny@gmail.com> wrote:
grep -RFl "moe" somedirectory/ | while read file; do echo mv "`dirname "$file"`" "destination_path"; done
Good suggestion. For this to work, I think you need to add the '-l' (lowercase L) option to grep such that it only prints *names* of files with matching lines instead of every matching line in the file.
Brett brings up a good point here. I think all of the above have had lowercase L's in them, but it looks like an uppercase i. Copy and paste solves this problem, but you could then run into the issue of all on one line problems. I'm sorry I didn't specify that. All of my above command should be run on one line and the RFl is AR-EF-el. And,
to avoid seeing lots of errors with moving directories with multiple matching files, add a test to the dirname to see that it exists before trying to move it.
Something like this:
grep -l -RFI "moe" <search-space> | while read file;do dd=`dirname $file`; [ -d $dd ] && mv $dd <destination>;done
I was trying to figure out an elegant way of doing this with sort and uniq, but decided that it wasn't that big of a deal, but this is great. I'll have to remember this. I was only coming up with things that involved using xargs and dirname, but I never tried anything because I've run into issues with xargs not working by element and only working with entire lists.
-BR
Randall Mason