Is there command line tool that removes everything that whereis returns (even if they are directories) but works even with partial names (e. g. if I give the string gmai it will also delete files that are named gmail).

So recursively look through all directories and delete the contents of those that contain a string in their name.

e. g. If the directories are mail/gmail/sentmail delete everything below gmail.

  • 5
    What whereis returns is very specific, and normally you wouldn't want to delete the files it lists. It's certainly possible to delete files based on all or part of their names, but I suggest you tell us exactly what you want to do rather than hint at it – Zanna Feb 20 '18 at 17:15
  • @DavidFoerster I wanted what the answer is doing, except if I am doing this to delete, it might be faster not to look further inside directories that will be deleted – Nesa Feb 21 '18 at 21:53
  • 1
    Unix file systems semantics don't allow the deletion of non-empty directories. Any tool that wants do successfully delete a directory needs to delete all of its children first. – David Foerster Feb 21 '18 at 21:59

If you really want to do this, you can use this command, but you should test before without the switch -delete to test if it's what's you expect :

find ./mail -depth -path '*gmai*' -delete

It's recursive in all sub-dirs

With the help of comment from @David Foerster and my original command


whereis(1) doesn't sound suitable to your purpose since it's no general purpose path name search tool.

Without a search index

As Gilles noted in his answer you can use find(1) for this job but I want to make a small yet significant improvement1:

find [PATH...] -depth -path '*gmai*' -print # -delete


  • find [PATH...] goes through all files and subdirectories below each PATH or the current working directories, if no path was given, and prints all matches (by default).

  • -path PATTERN matches full paths against PATTERN using globbing if any, so *gmai* matches all path names with an infix gmai.

  • -delete deletes all matched paths (and overrides the default action to print them).

  • -depth instructs find to match children before parents, i. e. directory entries before the parent directories themselves. It's necessary to delete children before their parents because in Unix' file system semantics only empty directories can be deleted.

  • -print prints all matched paths. Use this to check the result before the actual deletion. Comment in the -delete command (by removing the # in front of it) to actually delete them.

With a search index

If all the locations that you intend to delete appear in the mlocate.db(5) search index you can use it for faster searches:

locate '/your/parent/path/*gmai*'

searches all (indexed) paths that start with /your/parent/path/ and have an infix gmai in their remainder. This command only lists search results.

To delete the results use:

locate -0 '/your/parent/path/*gmai*' | xargs -r -0 -- rm -rf --


  • | redirects the output of the left-side command to the input of the right-side command.

  • xargs collects "items" from its input, appends them to a given command and runs that command.

  • -r makes xargs not run the command if no input item occurs.

  • rm -rf removes files and directories recursively

  • -0 tells locate to delimit matches by null-bytes and xargs to accept null-separated input items. This avoids issues with path names that contain white-space and, more specifically, line break characters.

1 that he incorporated in his answer before the question was reopened and allowed me to answer.

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.