A script to find dir, delete files in, and then del dir?

Hello!!

I have directories from 2008, with files in them. I want to create a script that will find the directoried from 2008 (example directory:
drwxr-xr-x 2 isplan users 1024 Nov 21 2008 FILES_112108), delete the files within those directories and then delete the directories themsleves. I know the long way of doing this: cd into directory, enter rm *, then cd ../;rmdir directory.

Is ir possible to create a script instead of doing this manually?

would the code be:

find FILES_*08
read X
cd $X
rm *
cd ../rmdir $X

Thanks in advance!
Ben

Hint: rm has the useful flag '-r' which stands for recursive, meaning it will remove the directory and all contents. So it's pretty much just one command:

find . -type d -name 'FILES_*08' -exec rm -r {} \;

When I used the rm -r command I got alot of errors.

Thanks Ill try that!

Ben

---------- Post updated at 04:52 AM ---------- Previous update was at 04:44 AM ----------

THANKS SO MUCH IT WORKED!!

A couple of question if you dont mind:

find . -type d -name 'FILES_*08' -exec rm -r {} \;

what does the {} and \; do?

Ben

1 Like

The curly brackets and the escaped semicolon are part of the -exec action of find. The brackets are a placeholder, and tell find where to put the names of the matches, and the escaped semicolon tells it where the options to -exec end.