Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I tried running

locate *.orig | xargs rm

but it said No such file or directory

I've seen ways to do it with find but locate returns the full path to the object so it should be possible

share|improve this question

5 Answers 5

up vote 7 down vote accepted

If filenames contain spaces you should use

locate -0 $something | xargs -0 rm

or

locate $something | while read f; do rm "$f"; done

Also, you should protect *.orig with quotes, to avoid the shell expansion, and pass it to locate untouched.

share|improve this answer
    
What do you mean by "shell expansion"? –  soldier.moth Oct 22 '10 at 20:58
    
+1 for your second example. I always use | while read since my home directory is full of files with spaces. –  sirlancelot Oct 22 '10 at 21:35
    
@Soldier.moth: if in the current folder there are files corresponding to the pattern *.orig, the shell will expand the pattern to, say, file1.orig file2.orig ..., so that locate will not see the exact string *.orig as it should. –  enzotib Oct 23 '10 at 5:39

It's xargs not xarg

share|improve this answer
    
That's what I meant was typing question from memory thank you though. –  soldier.moth Oct 22 '10 at 19:35
    
Oh, ok. It looked like the error you'd get putting an incorrect command after | –  maco Oct 22 '10 at 19:45

The command locate *.orig | xargs rm does work actually but what was happening was that locate was finding *.orig files in the trash can and rm spits out the error No such file or directory when trying to delete files in the trash can.

share|improve this answer
    
You should add information as a "comment" to the original answer, or you can edit the original answer. This is not an answer to your own question. –  enzotib Oct 23 '10 at 5:57
    
It is an answer to my question the reason I was getting the error was because locate was finding *.orig files in the trash can and rm couldn't delete them. I accepted your answer and voted up both other answers because they were well written and might help someone who comes along later. –  soldier.moth Oct 23 '10 at 6:25

locate doesn't do the globbing, but the shell does. The shell expands *.orig to what it finds in the current directory which matches *.orig.

Just use

locate .orig

and if that gets you what you need

locate .orig | xargs rm

or, as enzotib mentioned

locate -0 .orig | xargs -0 rm

if you have Whitespace in the filenames.

share|improve this answer

A trick : Save all paths in tmp file . then , Loop on it:

#!/bin/bash
locate .orig /tmp/tmp.txt
while read line
do
    pth=$line
    rm "$pth" 
done < /tmp/tmp.txt

rm -rf /tmp/tmp.txt 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.