Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to do a search using grep inside all the files of a directory for the pattern '@mail.ru>' (to clean up a mess from a spammer). Using Mac OS X's command line, which should be similar to most linux installs, how do I write the command to find all with that pattern, and move it to another directory called 'suspects'? Thanks!

Neil

share|improve this question

Create the suspects directoy. Then try a command like:

mv $(grep -l '@mail.ru>' *) suspects 

Try the grep command on its own to see what it will find. -l causes grep to list the matching file names. This will only work if there are no spaces in the file names, which should be the case for mail files. Otherwise you should look at using find or changing IFS to exclude spaces while you run the command.

share|improve this answer
    
Useful -- thanks! In the end, I went with this: grep -l -r -i -z '@mail.ru[<>]' 2a | xargs -I{} mv {} 2a-ru where [<>} gave me a choice of < or > at the end of mail.ru (part of identifying the bad mail). And 2a was the folder to be searched, and 2a-ru was for all the mail found to match the pattern. – Neil Ticktin Mar 22 '12 at 6:41
    
For safety, it would be good to add -n/--no-clobber or -i/--interactive to mv. For example, when adding an -r to grep, or using */* instead of *, it could go very wrong... – Volker Siegel Aug 4 '14 at 22:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .