Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
add comment

5 Answers 5

up vote 12 down vote accepted

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.

share|improve this answer
1  
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 '13 at 19:02
add comment
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).

share|improve this answer
    
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 '13 at 18:18
1  
using -type f will only delete regular files, so symbolic links for example would not be deleted. –  Huygens Mar 15 '13 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. –  Hauke Laging Mar 15 '13 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. –  Stéphane Chazelas Mar 15 '13 at 20:30
add comment

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

$  rm /tmp/our_cache/* 2>/dev/null
share|improve this answer
    
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 '13 at 16:54
    
Even if you try to delete some file which some process has opened, you wont get an error. –  pradeepchhetri Mar 15 '13 at 17:03
add comment

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.

share|improve this answer
add comment

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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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