Ask Dave Taylor
  • Facebook
  • Instagram
  • Linkedin
  • Pinterest
  • Twitter
  • YouTube
  • Home
  • YouTube Videos
  • Top Categories
  • Subscribe via Email
  • Ask A Question
  • Meet Dave
  • Home
  • Mac & MacOS Help
  • Fancy Mac OS X command line tricks

Fancy Mac OS X command line tricks

August 11, 2003 / Dave Taylor / Mac & MacOS Help / 1 Comment

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?”

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.

Let’s Stay In Touch!

Never miss a single article, review or tutorial here on AskDaveTaylor, sign up for my fun weekly newsletter!
Name: 
Your email address:*
Please enter all required fields
Correct invalid entries
No spam, ever. Promise. Powered by FeedBlitz
Please choose a color:
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!

One comment on “Fancy Mac OS X command line tricks”

  1. Brian Excarnate says:
    February 21, 2007 at 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.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Search

Recent Posts

  • How Can I Add My Pronouns to my Instagram Account Profile?
  • Possible to Pair Bluetooth Headphones with my Vizio TV?
  • Possible to Schedule a Windows 11 System Update & Restart?
  • How Can I Add Spacers and Divider Lines to my MacOS 12 Dock?
  • How Do I Pair Bluetooth Earbuds with my Google Chromebook?

On Our YouTube Channel

Google Pixel 6a Budget Android Smartphone -- DEMO & REVIEW

QueStyle M15 Mobile Lossless DAC / Headphone Amp -- UNBOXING & REVIEW

Categories

  • AdSense, AdWords, and PPC Help (106)
  • Amazon, eBay, and Online Shopping Help, (161)
  • Android Help (201)
  • Apple iPad Help (145)
  • Apple Watch Help (52)
  • Articles, Tutorials, and Reviews (344)
  • Auto Tech Help (11)
  • Business Advice (199)
  • Chrome OS Help (25)
  • Computer & Internet Basics (764)
  • d) None of the Above (165)
  • Facebook Help (383)
  • Google, Chrome & Gmail Help (179)
  • HTML & Web Page Design (245)
  • Instagram Help (48)
  • iPhone & iOS Help (607)
  • iPod & MP3 Player Help (173)
  • Kindle & Nook Help (93)
  • LinkedIn Help (85)
  • Linux Help (166)
  • Linux Shell Script Programming (87)
  • Mac & MacOS Help (894)
  • Most Popular (16)
  • Outlook & Office 365 Help (26)
  • PayPal Help (69)
  • Pinterest Help (53)
  • Reddit Help (18)
  • SEO & Marketing (81)
  • Spam, Scams & Security (92)
  • Trade Show News & Updates (23)
  • Twitter Help (217)
  • Video Game Tips (66)
  • Web Site Traffic Tips (62)
  • Windows PC Help (921)
  • Wordpress Help (204)
  • Writing and Publishing (72)
  • YouTube Help (46)
  • YouTube Video Reviews (159)
  • Zoom, Skype & Video Chat Help (57)

Archives

Social Connections:

Ask Dave Taylor


Follow Me on Pinterest
Follow me on Twitter
Follow me on LinkedIn
Follow me on Instagram


AskDaveTaylor on Facebook



microsoft insider mvp


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 site or on any linked site. Further, please note that by submitting a question or comment you're agreeing to our terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site. Our lawyer says "Thanks for your cooperation."
© 2022 by Dave Taylor. "Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.
Privacy Policy - Terms and Conditions - Accessibility Policy