9

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.

6 Answers 6

11

Delete the contents of the folder instead.

rm -r MYFOLDER/*
4
  • 1
    Note that this doesn't hidden files/directories within MYFOLDER
    – tylerl
    Oct 24, 2011 at 14:33
  • 1
    @tylerl: It does if you set shopt -s dotglob first. Oct 25, 2011 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, 2015 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, 2015 at 17:39
5

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
2
  • 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, 2015 at 20:29
  • 1
    Make sure you are using bash shell. The shell must support "Brace Expansion" and "Regular Expressions" for file patterns. Jun 16, 2015 at 20:37
3

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

Or

rm -rf foldername; mkdir foldername
2
  • 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. Oct 28, 2011 at 22:10
  • This should be the accepted answer. +1
    – CodeMed
    Oct 13, 2015 at 21:36
0

How about

cd MYFOLDER
find . | rm -rf

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

1
0

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

0

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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .