Take the 2-minute tour ×
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

5 Answers 5

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

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

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.