Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I need to delete all the content of a folder in CentOS but not the folder itself.

For example: I have a folder named "MYFOLDER" which contains subfolders FOLD_1, FOLD_2, FOLD_3, FOLD_4... etc , and some files. I need to delete all these folders and files but not the container folder MYFOLDER. I need this directory to direct the results of a previous process.

I tried with the rm -rf command, but this delete the container folder too.

share|improve this question

7 Answers 7

up vote 4 down vote accepted

Delete the contents of the folder instead.

rm -r MYFOLDER/*
share|improve this answer
Note that this doesn't hidden files/directories within MYFOLDER –  tylerl Oct 24 '11 at 14:33
thanks so much..!! ;) –  verofairy Oct 24 '11 at 15:31
@tylerl: It does if you set shopt -s dotglob first. –  jgoldschrafe Oct 25 '11 at 3:33

You can use:

rm -r MYFOLDER/{.[^.],.??*}

This deletes also the hidden files and folders. If you have too many files in the MYFOLDER directory, then you should run instead:

ls MYFOLDER/{.[^.],.??*}|xargs rm -r
share|improve this answer
I have over 500k files inside my meta/ directory, so I wanted to try the xargs alternative, however i get these error messages: ls: cannot access meta/.[^.]: No such file or directory ls: cannot access meta/.??*: No such file or directory rm: missing operand –  andufo Jun 16 at 20:29
Make sure you are using bash shell. The shell must support "Brace Expansion" and "Regular Expressions" for file patterns. –  Mircea Vutcovici Jun 16 at 20:37

The easiest way, in my opinion, is to delete the entire directory, including itself, and then recreate the folder. There are situations when this is not a good solution (such a unattended scripts or more complex pipelines), but you didn't specify why you didn't want to delete the folder.

rm -rf foldername
mkdir foldername

Oops. You did specify (i reread your post). Well,still, deleting and recreating can work. Especially if you chain commands together, like

rm -rf foldername && mkdir foldername


rm -rf foldername; mkdir foldername
share|improve this answer
Sometimes you do not have permission in the parent folder or you want to preserve certain attributes you are unaware of. E.g. you can not delete your home folder as non root user, but you can remove anything under it. –  Mircea Vutcovici Oct 28 '11 at 22:10

How about

find . | rm -rf

This deletes hidden files (the ones starting with a dot), too.

share|improve this answer
This will delete MYFOLDER too. –  Mircea Vutcovici Oct 28 '11 at 22:12

Try rm -r -f filename its working with CentOS 6.x

share|improve this answer

I strongly recommend this alternative (it also removes files with spaces in names):

find MYFOLDER/ -type f -print0 | xargs -0 rm -f
share|improve this answer
find FOLDER/ -type f -exec rm -f {} \; | rm -r FOLDER/* 

should do the trick

share|improve this answer
This looks like you copied the accepted answer, made it three times as complicated, and added a few bugs in the process. I don't see any advantage of this approach compared to the accepted answer. –  kasperd Jun 16 at 21:24

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.