Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Simple UNIX scripting question?

Simple UNIX scripting question?
Thread Tools
Laurence
Mac Enthusiast
Join Date: Mar 1999
Location: Portland, Oregon, United States
Status: Offline
Reply With Quote
Jan 24, 2008, 12:36 AM
 
I think this is probably very easy, but since I couldn't find a solution I thought I'd ask here...

I know how to use the locate command to search the locate database for files and I know how to delete files using rm, but I can't find an easy way to link the two together. I've read a few tutorials on how to write basic sh scripts, but for some reason this seems harder than I would assume it would be. I just want a script where I can type something like the following...

sudo locate_delete foo

and it will delete any file that has the word foo anywhere in its path, anywhere on the hard drive. I know that this sounds like a very dangerous thing to do, but its being used to possibly solve an issue where the current solution is to just format the hard drive and re-image. Once a proof-of-concept is working I can probably get someone to take a closer look at the problem and hopefully find a more elegant solution.

BTW, I know I could use the find command mixed with sed to do something along these lines, but the final script is going to be looking for literally hundreds, perhaps thousands, of things to delete and thus would be way to slow actually searching for each file on each iteration. Pulling things out of the locate database would be faster by a large factor.
--Laurence
     
smitty825
Forum Regular
Join Date: Sep 2003
Location: San Diego
Status: Offline
Reply With Quote
Jan 24, 2008, 12:52 AM
 
Well, you said you didn't want find, but here is how you could use it:

% find . -name "*<string>*" -exec rm {}\;

-------------

That being said, I bet you could do something similar with locate:

% locate string | xargs rm
     
Laurence  (op)
Mac Enthusiast
Join Date: Mar 1999
Location: Portland, Oregon, United States
Status: Offline
Reply With Quote
Jan 24, 2008, 01:08 AM
 
I kept trying to figure out a way to use the | or > but didn't know about xargs... I'm looking at the man page now and that's exactly what I was looking for!

Thanks!
--Laurence
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 09:04 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,