As part of a deployment script, I want to dump some cached stuff from my temp directory. I use a command like:

rm /tmp/our_cache/*

However, if /tmp/our_cache is empty (fairly common when pushing many changes in quick succession to our testing server), this prints the following error message:

rm: cannot remove `/tmp/our_cache/*': No such file or directory

It's not a big deal, but it's a little ugly and I want to cut down the noise-to-signal ratio in the output from this script.

What's a concise way in unix to delete the contents of a directory without getting messages complaining that the directory is already empty?

5 Answers 5


Since you presumably want to remove all files without prompting, why not just use the -f switch to rm to ignore nonexistent files?

rm -f /tmp/our_cache/*

From man page:

-f, --force
          ignore nonexistent files, never prompt

Also, if there may be any subdirectories in /tmp/our_cache/ and you want those and their contents deleted as well, don't forget the -r switch.

  • 4
    most obvious solution and I did not think about it! I feel like the first guy on this comic xkcd.com/399 ;-)
    – Huygens
    Mar 15, 2013 at 19:02
find /tmp/our_cache/ -mindepth 1 -delete

Edit 1

removed "-type f

Edit 2

Added non-standard option -mindepth 1 in order to prevent the search root directory to be removed (after taking the -type f limitation away).

  • Just a note, I'd leave out -type f since there may be temporary directories as well (although the example in the question doesn't account for them, so they're probably not there...)
    – Izkata
    Mar 15, 2013 at 18:18
  • 1
    using -type f will only delete regular files, so symbolic links for example would not be deleted.
    – Huygens
    Mar 15, 2013 at 18:58
  • @rush Sorry, I didn't check that but just had a look at the man pages which says "Delete files", not mentioning directories anyhow. I delete my comment. Mar 15, 2013 at 18:58
  • 1
    That removes /tmp/our_cache so you might as well do rm -r /tmp/our_cache. While you're at using non-standard options, you could use -mindepth 1. Note that contrary to rm /tmp/our_cache/*, it also removes subdirectories and their content and hidden files. Mar 15, 2013 at 20:30

You can redirect the standard error to /dev/null so that it wont print this

$  rm /tmp/our_cache/* 2>/dev/null
  • 2
    Wouldn't this also suppress errors shown if, say, a file can't be deleted because it is in use? The only error I want to suppress is the one shown if the target directory is empty; this seems like it would have unwanted side effects.
    – Mark Amery
    Mar 15, 2013 at 16:54
  • Even if you try to delete some file which some process has opened, you wont get an error. Mar 15, 2013 at 17:03

Another way (though slightly overkill...):

[ ! -z "$(ls -A /tmp/our_cache/)" ] && rm -rf /tmp/our_cache/*

this uses test or [ to test if the string returned from running an ls on your directory is not empty. Then if, and only if, that string is NOT empty does it remove everything under your directory.

NOTE: leaving the glob out of the ls will suppress the error of no files.


If your script is usin BASH you can try:

if test "$(ls /tmp/our_cache/)" !=  ""; then rm /tmp/our_cache/*; fi

Doing so will still produce an error if there is a "real" problem or if a file is protected (you would then need the -f modifier of rm to delete this type of files) but will avoid the error when no files is there.

If you use another shell than BASH for your scripting, such as zsh, ksh, you can try this other syntax which might be more portable:

if [[ "$(ls /tmp/our_cache/)" !=  "" ]]; then rm /tmp/our_cache/*; fi

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.