I'm working on a bash script in an AWS EC2 instance (RHEL derived) which needs to do the following:

  1. Search for all directories named "_combined" in ${PROJECT_DIR}
  2. Delete all regular files in all those directories - but not the directory itself

What's the best approach to do this?

  • @don_crissti - good point. I meant regular files, will amend question. – BaronGrivet Mar 14 '17 at 21:15

So, something like

find "$PROJECT_DIR" -path "*/_combined/*" -type f

And if that seems right:

find "$PROJECT_DIR" -path "*/_combined/*" -type f -delete

Of course that will hit all regular files in the whole tree, not just immediate contents of _combined.

| improve this answer | |

This will do what you've described. When you are sure it works, replace the -print clause with -delete:

find "$PROJECT_DIR" -type d -name '_combined' -execdir find '_combined' -maxdepth 1 -type f -print \;

What it does is to search for all directories named _combined underneath $PROJECT_DIR, and in each one it runs the second find snippet which will remove all non-directories in the found directory.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.