|
|
Fancy Mac OS X command line tricksA 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"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"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!
Categorized:
Mac OS X Help
(Article 3654,
Written by Dave Taylor)
Tagged: Previous: How do you edit your existing Web pages on a remote server? Next: Changing Link Colors on an HTML page Reader Comments To Date: 1
I do have a comment, now that you mention it!Check This Out Too... |
Recent Entries
Look for Answers
Recommended
All Our Categories
Apple iPad Help
Articles and Reviews Auctions and Online Shopping Blogs and Blogging Building Web Site Traffic Business and Management Computer and Internet Basics d) None of the Above Facebook Help Google Gmail Help Google Plus Help HTML, JavaScript and Web Site Programming Industry News and Trade Shows iPhone and Cell Phone Help iPod, Sony PSP and MP3 Player Help Kindle Fire Help Mac OS X Help Pay Per Click (PPC) Advertising Pinterest Help Search Engine Optimization (SEO) Shell Script Programming Tech Support Video Help The Writing Business Twitter, LinkedIn and Social Network Help Unix and Linux Help Video Game Tips and Help Windows PC Help Find Me on Google+ ADT on G+ |
-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.