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.


Delete the contents of the folder instead.

rm -r MYFOLDER/*
  • 1
    Note that this doesn't hidden files/directories within MYFOLDER – tylerl Oct 24 '11 at 14:33
  • 1
    @tylerl: It does if you set shopt -s dotglob first. – jgoldschrafe Oct 25 '11 at 3:33
  • This requires you to manually approve y/n descending into each subdirectory, which is a complete waste of time. The correct command would handle all that automatically so you don't get stuck with 50 prompts. – CodeMed Oct 13 '15 at 21:22
  • rm -R /path/to/myfolder/* is the correct syntax to avoid excessive requests to approve deletion of every file and subfolder. Note the capital R. – CodeMed Oct 16 '15 at 17:39

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
  • 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 – Andres SK Jun 16 '15 at 20:29
  • 1
    Make sure you are using bash shell. The shell must support "Brace Expansion" and "Regular Expressions" for file patterns. – Mircea Vutcovici Jun 16 '15 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
  • 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
  • This should be the accepted answer. +1 – CodeMed Oct 13 '15 at 21:36

How about

find . | rm -rf

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


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


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

find MYFOLDER/ -type f -print0 | xargs -0 rm -f

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.