Pipe results of locate into rm

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


Solution 1:

If filenames contain spaces you should use

locate -0 $something | xargs -0 rm

From locate man page:

-0, --null Separate the entries on output using the ASCII NUL character instead of writing each entry on a separate line. This option is designed for interoperability with the --null option of GNU xargs(1).

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.

Solution 2:

It's xargs not xarg