I want to search files having name libre in them & then delete them. I am half done here & can't find a reliable way to delete files especially when filenames have white spaces in them.

find -iname *libre*

this will search the files, but let's say a file is 'Libre Office Word' now it has white spaces can you guide how to delete such files too?


You can use the -delete option if your version of find supports it or you can use rm in the -exec options.

find -iname '*libre*' -delete       # GNU find
find -iname '*libre*' -exec rm {} + # POSIX

Note that you should quote the pattern. This prevents the shell from expanding it prior to being passed to the find command.

|improve this answer|||||
  • I'm not sure if OP wants also uppercase letters in the name (by the example given). – vonbrand Jan 26 '13 at 5:46
  • 1
    @vonbrand my understanding is the OP wanted "Libre Office Word' to match, but was using some method of deletion (such as xargs or a for loop) that broke on files with spaces. – jordanm Jan 26 '13 at 5:49
  • @jordanm yes you identified my real position. OK so the -delete flag worked even for whitesapces. How can I also pass a file extension to this search I mean search for libre & *.desktop – nightcrawler Jan 26 '13 at 6:11
  • 1
    -iname is not POSIX. -delete is not POSIX but is not exclusive to GNU find. – Stéphane Chazelas Jan 26 '13 at 7:58
  • Also note that -delete deletes (non-empty) directories, while rm wouldn't. -delete (both with GNU and FreeBSD finds at least) implies -depth. – Stéphane Chazelas Jan 26 '13 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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