Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I know a command to do the same with files, but what about folders (and all the files in it of course)?

share|improve this question
up vote 2 down vote accepted

The command you refer is find I suppose.

You should use the command -exec rm -r {} \; and add the -depth option.

The -r option to rm remove directories with all the content.
The -depth option tell find to elaborate content of folders before the folder itself.

share|improve this answer
That's almost perfect, thanks. Is there a way so it doesn't delete the actual folder I'm searching in? So only folders within the folder get deleted? I'm currently doing "find "/home/user/folder/" -mtime +7 -exec rm -r {} \;", but it deleted the folder called "folder" as well if the time matches. I only want subfolders deleted older than 7 days. – nLinked Dec 10 '11 at 19:24
Solved using: rm -rf "/target/directory with spaces/"* – nLinked Dec 10 '11 at 20:12
find ./dirc/* -mtime +x -type f -delete
  • ./dirc/* : is your directory (Path)
  • -mtime +x : older than x days
  • -type f : only files
  • -delete : no surprise. Remove it to test before like rm

Auto delete files older than 7 days

share|improve this answer

Your Answer


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.