LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 03-02-2011, 10:02 AM   #1
nass
Member
 
Registered: Apr 2006
Location: Athens, Greece
Distribution: slackware, debian, ubuntu
Posts: 667

Rep: Reputation: 39
using locate with rm


Hello everyone,
does anyone know how can I use locate with rm?

rm `locate "some file"`

does not work when my pathnames occasionally contain:
-spaces and other characters (like + or possibly others that need escaping)
-greek characters

how can I grab line-by-line the output from locate and wrap quotes"" around the paths??

Thank you for your help
 
Old 03-02-2011, 10:07 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
locate "some file" | sed 's/.*/"&"/' | xargs rm

Locate will also return files where "some file" is a part of the filename. Be carefull. Try it without the xargs command first.

locate usually is configured for system files, not files under /home. The find command may be better to use.
example:
find music/ -iname "some file" -print0 | xargs -0 rm

Last edited by jschiwal; 03-02-2011 at 10:08 AM.
 
1 members found this post helpful.
Old 03-02-2011, 11:07 AM   #3
nass
Member
 
Registered: Apr 2006
Location: Athens, Greece
Distribution: slackware, debian, ubuntu
Posts: 667

Original Poster
Rep: Reputation: 39
i'm on a preconfigured system running busybox .. and find is not the standard find, not all options are enabled unfortunately.
it's also pain to search through 3.6TB of data with find ...

you are correct about being extra careful when deleting with locate and find. I did check the output of locate thoroughly before piping to xargs rm..

your command works wonderfully. truth is I should have thought about it but i'm not very keen on the use of sed yet.

Thank you
 
Old 03-02-2011, 11:25 AM   #4
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
Just to note, that the locate database is usually rebuild each night by a cron-job. Hence the output you get might not reflect necessarily the actual list of files.
 
Old 03-02-2011, 11:27 AM   #5
nass
Member
 
Registered: Apr 2006
Location: Athens, Greece
Distribution: slackware, debian, ubuntu
Posts: 667

Original Poster
Rep: Reputation: 39
i'm aware..
and have altered it to update the index every few hours.

at any rate for up to date info I run 'updatedb' directly
 
Old 03-02-2011, 11:27 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,336

Rep: Reputation: 4176Reputation: 4176Reputation: 4176Reputation: 4176Reputation: 4176Reputation: 4176Reputation: 4176Reputation: 4176Reputation: 4176Reputation: 4176Reputation: 4176
check the locate manpage for passing it regex - you can be *very* specific if you wish.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting 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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using locate. stf92 Linux - General 5 06-18-2010 07:45 AM
locate mikeshn Linux - Software 4 05-14-2007 05:38 AM
cpan> locate -u Unknown shell command 'locate -u'. Type ? for help. sharad2005 Linux From Scratch 1 08-04-2006 01:40 AM
Can't locate locate hindenbergbaby Linux - Newbie 3 10-22-2003 05:13 PM
how to locate ? jamaso Linux - General 4 10-17-2002 04:45 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 08:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration