Industry guru Dave Taylor offers tech support on technical and business topics, including iPhone, iPod, Microsoft Windows, Sony PSP, cellphones, online advertising, CSS, Web design, business, Unix, Linux, SEO, Mac OS X, and shell script programming.     


Fancy Mac OS X command line tricks

A reader writes:
"I figured that this would be right up your alley. If I want to move any document that contains "agreen" to another folder, what commands would I use on the command line?"

Dave's Answer:
Hmmm.... let's see...
find . -print | xargs grep -il "agreen"
would produce a list of all files from the current point in the file system "down" (e.g., within this subdirectory), so wrap that in a for loop and you have something like:
target="~/newdir"

for name in $(find . -print | xargs grep -il "agreen")
do
    echo moving file $name to $target
    mv $name $target
done

You'll want to make sure that 'newdir' exists first, or the 'mv' will successively step on and remove all of your files. Maybe a safer snippet would be:
target="~/newdir"

if [ ! -d $target ] ; then
    echo "Failed: target directory $target not found." 2>1
    exit 1
fi

for name in $(find . -print | xargs grep -il "agreen")
do
    echo moving file $name to $target
    mv $name $target
done
There! That should do the trick.


Related Mac OS X Help articles:
✔   Audacity can't find LAME library, I can't save Mp3?
Hey Dave. I read your article Audacity can't save mp3 audio files and am still puzzled because I downloaded the LAME Mp3 converter...
✔   How to remove Dashboard as a "space" in Mac OS X Spaces?
I'm a big fan of the Spaces utility in Mac OS X that lets me have multiple virtual screens [see Set Up Mac...
✔   Best place to buy a cheap MacBook laptop?
Hi Dave. I am looking for two gently used MacBook laptops for my teen daughters. Personal computers would greatly facilitate their studies as...
✔   File too big error copying to USB flash drive on my Mac?
I'm baffled. I have a 16GB Kingston USB flash drive that I use on my Mac system and I'm trying to copy a...
✔   Stealth image capture photo from webcam on my Mac?
Someone sneaks into my cubicle while I'm at lunch and takes candy out of my desk. Petty, but stupid too. I want to...

Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
    Enter your name: and your email addr:  








Reader Comments To Date: 1

Brian Excarnate said, on February 21, 2007 10:11 PM:

-print????

Why? It's pointless. Banish it from your mind.

How about this? It works fine for reasonable values of x:

for x in `find . -exec grep -l agreen {} \;`
> do
> mv -i $x /tmp/agreen.files/
> done

Commentary:
I leave off the -i option to grep as that wasn't asked for (I'm such a pedant!).

I add a -i option to mv because if there are 5 different files found that all have the same name, you may not want to end up with just the last one.

Notice my use of a trailing / on the directory name. If the directory doesn't exist, you just get error(s). Example:
# mv .CFUserTextEncoding nonexistantdirectory/
mv: rename .CFUserTextEncoding to nonexistantdirectory/: No such file or directory

To explain the form I am using, putting commands in back ticks makes the shell evaluate that command first and put its output in the command's place. This also means it finds all the filenames THEN acts on them, preventing the situation where the file is found again after being moved.

Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you, Dave, for all your helpful information by buying you a cup of coffee!

I do have a comment, now that you mention it!











I will never send you any unsolicited email. Ever.






Check This Out Too...

 
Look for Answers
Need Help? Ask Dave Taylor!


Follow Me on Pinterest

Find Me on Google+
ADT on G+
© 2002 - 2013 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site. Further, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site. My lawyer says "Thanks".
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.