As another answer points out, you can attempt to find all directories with the name test
and delete them
find -name "test" -type d -delete
I ran into some issues with cross compatibility on Mac, so I used this equivalent command:
find -path "*/test" -type d -delete
- -path: looks for a pattern in the fully qualified filename.
In either case however, if any of the directories named test
have files in them, find
will complain that the Directory is not empty
, and will fail to remove the directory.
If you intended to delete all of the files including the test
directory, then we can use the same trick to delete all of the files inside directories named test
first.
find -path "*/test/*" -delete
The pattern: "*/test/*"
, will ensure we only delete files inside a directory named /test/
. Once the directories are empty, we can go ahead and delete the directories using the first command:
find -path "*/test" -type d -delete
Example:
$ tree
.
├── mytest
│ └── test
│ └── blah.txt
├── test
│ ├── bar.jpg
│ └── dir
│ └── bar.bak
└── testdir
└── baz.c
5 directories, 4 files
$ find -path "*/test" -type d -delete
$ tree
.
├── mytest
│ └── test
├── test
└── testdir
└── baz.c
4 directories, 1 file
$ find -name "test" -type d -delete
$ tree
.
├── mytest
└── testdir
└── baz.c
2 directories, 1 file