0

I have a directory full of several other directories with the same structure, so it looks like this

top/A/Output/platform/..
top/B/Output/platform/..
top/C/Output/platform/..

and so on. I want to, starting from the top level, delete all the directories downwards of the platform directories, such that I end up with

top/A/Output
top/B/Output
top/C/Output

Can anyone recommend me a command for this? I am on rhel7 with bash 4.1.2

EDIT:

I have a lot of '2nd level' directories whose names are more complex than single letters, sorry should have noted that more explicitly

2
  • Something like rm -rf top/[ABC]/Output/platform maybe? Commented Feb 6, 2017 at 15:16
  • If any of the existing answers solved your problem, please consider accepting it with the checkmark; thank you! Commented Apr 18, 2017 at 10:53

3 Answers 3

3
rm -rf top/?/Output/platform

or, if the 2nd-level directory may be more than one character (A, B, or C in your example):

rm -rf top/*/Output/platform
2
  • This works because Bash, like all shells, interprets ? as “any single character in a filename” and * as “any sequence of characters in a filename” (note that this is “filename”, not “path”; they don’t match /). Commented Feb 6, 2017 at 15:31
  • Note that the star (*) won't match directories and files which start with a dot. Commented Feb 6, 2017 at 16:04
0

See man find for more options like regex patterns:

find top -type d -name platform -print0 | xargs -0 rm -rf
2
  • Will unfortunately also delete any other platform directory elsewhere, not just under top/*/Output. Commented Feb 6, 2017 at 15:43
  • @Kusalananda That's right, I have forgot the working directory. Thank you for your note. Commented Feb 6, 2017 at 15:47
0

You can use find this way :

find top -path 'top/*/Output/platform' -delete

Note that this will delete file and directories which start with a dot.

The following command won't delete files and directories starting with a dot, because the shell will skip them when it replaces the stars :

rm top/*/Output/platform

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.