Sorry, I keep forgetting which email address I signed up with... I would add quotes and use while, just in case the directory names have spaces and such. "for" splits on any whitespace, which could be quite troublesome. grep -RFl "moe" somedirectory/ | while read file; do echo mv "`dirname "$file"`" "destination_path"; done Randall Mason randall@mason.ch On Thu, Oct 29, 2009 at 12:47 PM, Randall Mason <randall@mason.ch> wrote:
I would add quotes and use while, just in case the directory names have spaces and such. "for" splits on any whitespace, which could be quite troublesome.
grep -RFl "moe" somedirectory/ | while read file; do echo mv "`dirname "$file"`" "destination_path"; done
Randall Mason randall@mason.ch
On Thu, Oct 29, 2009 at 12:25 PM, Jamie Guinan <guinan@bluebutton.com>wrote:
Adding to that:
for FILE in $( grep -lRF moe somedirectory/* ) ; \ do echo mv -v $(dirname "$FILE") destination_path ; done
Remove the "echo" and repeat if it looks Ok. Uses dirname to move the containing directory.
-Jamie
On Thu, 29 Oct 2009, James Gray wrote:
On Thu, Oct 29, 2009 at 11:41 AM, Maurice <mauricep@cds-cumberland.org> wrote:
Looking for some guidance;
I have several files within several folders (5 files per folder, and thousands of folders) that I need to search a text file within each folder for a word match (like three_little_pigs.txt, and I need to find "moe", if he's listed) and then when a match is found I need to move (not copy) that entire folder (and it's 3~5 files contained within) to another location...
I'm thinking grep, but don't know the correct syntax to make all this happen. I can easily find all the folders (1949 of them) and the word match 3923 times within the text file(s)...
I think this should do the trick. I haven't tested it cause I don't want to have *my* files moved around.
for FILE in $( grep -lRF moe somedirectory/* ) ; do mv "$FILE" destination_path ; done
~James _______________________________________________ Wlug mailing list Wlug@mail.wlug.org http://mail.wlug.org/mailman/listinfo/wlug
_______________________________________________ Wlug mailing list Wlug@mail.wlug.org http://mail.wlug.org/mailman/listinfo/wlug
There's also a useful tool in ``moreutils'' called vidir, which lets you filter out directory names in a pipeline and view them in an editor/pager
Sorry, I keep forgetting which email address I signed up with...
I would add quotes and use while, just in case the directory names have spaces and such. "for" splits on any whitespace, which could be quite troublesome.
grep -RFl "moe" somedirectory/ | while read file; do echo mv "`dirname "$file"`" "destination_path"; done
Randall Mason randall@mason.ch
On Thu, Oct 29, 2009 at 12:47 PM, Randall Mason <randall@mason.ch> wrote:
I would add quotes and use while, just in case the directory names have spaces and such. "for" splits on any whitespace, which could be quite troublesome.
grep -RFl "moe" somedirectory/ | while read file; do echo mv "`dirname "$file"`" "destination_path"; done
Randall Mason randall@mason.ch
On Thu, Oct 29, 2009 at 12:25 PM, Jamie Guinan <guinan@bluebutton.com>wrote:
Adding to that:
for FILE in $( grep -lRF moe somedirectory/* ) ; \ do echo mv -v $(dirname "$FILE") destination_path ; done
Remove the "echo" and repeat if it looks Ok. Uses dirname to move the containing directory.
-Jamie
On Thu, 29 Oct 2009, James Gray wrote:
On Thu, Oct 29, 2009 at 11:41 AM, Maurice <mauricep@cds-cumberland.org> wrote:
Looking for some guidance;
I have several files within several folders (5 files per folder, and thousands of folders) that I need to search a text file within each folder for a word match (like three_little_pigs.txt, and I need to find "moe", if he's listed) and then when a match is found I need to move (not copy) that entire folder (and it's 3~5 files contained within) to another location...
I'm thinking grep, but don't know the correct syntax to make all this happen. I can easily find all the folders (1949 of them) and the word match 3923 times within the text file(s)...
I think this should do the trick. I haven't tested it cause I don't want to have *my* files moved around.
for FILE in $( grep -lRF moe somedirectory/* ) ; do mv "$FILE" destination_path ; done
~James _______________________________________________ Wlug mailing list Wlug@mail.wlug.org http://mail.wlug.org/mailman/listinfo/wlug
_______________________________________________ Wlug mailing list Wlug@mail.wlug.org http://mail.wlug.org/mailman/listinfo/wlug
On Thu, Oct 29, 2009 at 12:48 PM, Randall Mason <clashthebunny@gmail.com> wrote:
Sorry, I keep forgetting which email address I signed up with...
I would add quotes and use while, just in case the directory names have spaces and such. "for" splits on any whitespace, which could be quite troublesome.
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. 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 -BR
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
participants (3)
-
Brett Russ
-
jrm8005@gmail.com
-
Randall Mason