2

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?

1 Answer 1

5

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.

5
  • I'm not sure if OP wants also uppercase letters in the name (by the example given).
    – vonbrand
    Jan 26, 2013 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, 2013 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 Jan 26, 2013 at 6:11
  • 1
    -iname is not POSIX. -delete is not POSIX but is not exclusive to GNU find. Jan 26, 2013 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. Jan 26, 2013 at 8:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .