28

For this type of dir structure :

/config/filegroups/filegroupA/files/fileA1.txt
/config/filegroups/filegroupA/files/fileA2.txt
/config/filegroups/filegroupB/files/fileB1.txt
/config/filegroups/filegroupB/files/fileB2.txt
...

I know that I can use rm -rf /config/filesgroups to delete parent folder and all sub-folders ...

but I want to delete only /filegroupA , /filegroupB , etc. , and not delete /config/filegroups

4
  • 13
    Is there any problem to use rm -rf /config/filesgroups/* ?
    – dsmsk80
    Commented Aug 15, 2013 at 20:35
  • 3
    Or maybe even rm -rf /config/filegroups/filegroups{A,B} ?
    – Drav Sloan
    Commented Aug 15, 2013 at 20:40
  • @dsumsky By default bash doesn't include '.' folders in pathname expansion. Which may or may not be desired. Myself I do shopt -s dotglob in .bashrc to fix that. Just a side note that somebody might find useful ;). Commented Aug 15, 2013 at 20:58
  • Did any o fthis work for you? If so, would you mind accepting an answer, in order to help others (likw me) who want to do the same thing?
    – Mawg
    Commented Oct 24, 2016 at 9:41

5 Answers 5

41
rm -rf /config/filegroups/*

If you want to delete only directories (and symlinks to directories), leaving any files in /config/filegroups untouched, you can use a trailing slash:

rm -rf /config/filegroups/*/

If you want to delete directories with names beginning with a . as well, assuming you have a fairly recent bash, you should use the dotglob shell option:

shopt -s dotglob
rm -rf /config/filegroups/*/
shopt -u dotglob
1
  • 1
    and please not that it will not work for rm -rf "/config/filegroups/*" Commented Nov 23, 2017 at 12:10
8

I prefer using find with -exec, that would make your call something like this:

find /config/filegroups/ -maxdepth 1 -mindepth 1 -type d -exec rm -rf {} \;
5

This will delete all files and directories under /config/filegroups including "hidden" files and directories (names starting with .).

find /config/filegroups -mindepth 1 -maxdepth 1 | xargs rm -rf

If the file or directory names contain spaces you have to do it like this:

find /config/filegroups -mindepth 1 -maxdepth 1 -print0 | xargs -0 rm -rf

Bonus: you can first check what is going to be deleted like this:

find /config/filegroups -mindepth 1 -maxdepth 1

If you want to keep certain files or directories you can do it like this:

find /config/filegroups -mindepth 1 -maxdepth 1 -not -name "keep"
2
  • Is there any reason not to use find's -delete option?
    – evilsoup
    Commented Aug 16, 2013 at 8:13
  • 4
    -delete refuses to delete non empty directories. -maxdepth overrides -depth, which is needed by -delete to delete non empty directories. Without -maxdepth you cannot easily see which directories are going to be deleted because it also lists the files inside the directories. Not using -maxdepth also means you cannot easily use filters like -name. Furthermore -delete deletes the directories by deleting all the objects inside first, which can take a long time if it is a big and deep tree.
    – Lesmana
    Commented Aug 16, 2013 at 10:46
1

Try this:

find /config/filegroups/* -type d -name filegroupA -prune -exec rm -rf {} \;
-2

If you are already in the folder you can just type rm -rf ./**

So:

cd /config/filesgroups
rm -rf ./**

This is a glob pattern to delete all the subfolders from the local path..

./ referring to the local folder... and ** for all the folders beneath..

2
  • 1
    Which folder, and why `**``
    – RalfFriedl
    Commented May 28, 2019 at 18:35
  • 1
    This would be exactly the same as rm -rf ./*, except that you are likely to get diagnostic messages about "No such file or directory" if you're not using -f in your command (since the top-most directory would be recursively deleted before its contents is processed by rm).
    – Kusalananda
    Commented May 28, 2019 at 20:13

You must log in to answer this question.

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