Industry guru Dave Taylor offers free tech support on a wide variety of technical and business topics, including HTML, online advertising, Cascading Style Sheets, Web design, management, Unix, Linux, search engine optimization, online dating, Mac OS X, shell script programming and Microsoft Windows.

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.



Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Stumble Upon    

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments

-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.

Posted by: Brian Excarnate at February 21, 2007 10:11 PM


I have a lot to say, but ...
Starbucks coffee cup I have a lot to say, and questions of my own for that matter, but most of all I'd like to say thank you for all your efforts on this Web site by buying you a chai!

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









Remember personal info?


Please note that I will never send you any unsolicited commercial email. Ever.

While I'm at it, 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.









Uniblue: Free Virus Scan

Search
Find just the answers you seek from among our 1700+ free tech support articles by using our Lijit search engine.


Member of the B5Media Network

Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Join the List!
Join my author info mailing list, where you'll learn about my upcoming books, speaking gigs, and more!


Book Links
© 2002 - 2009 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.

[whiteboard marker tray]