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

BaltoStar
  • 2,371

5 Answers5

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
evilsoup
  • 6,807
  • 3
  • 34
  • 40
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 {} \;
Panthro
  • 201
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"
Lesmana
  • 27,439
  • Is there any reason not to use find's -delete option? – evilsoup Aug 16 '13 at 08: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 Aug 16 '13 at 10:46
1

Try this:

find /config/filegroups/* -type d -name filegroupA -prune -exec rm -rf {} \;
AdminBee
  • 22,803
-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..

  • 1
    Which folder, and why `**`` – RalfFriedl May 28 '19 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 May 28 '19 at 20:13