shell script to delete directories...

*Just realized that i posted this in the wrong forum. should have been in Shell, though it is on AIX...

Hi.

I'm trying to write a script that will delete all directories found, that are not named as a "number" (year)...

here is what i mean, let's say i have within /data/exports the following directories:

/data/exports/2000
/data/exports/2001
/data/exports/2002

/data/exports/daily/2000
/data/exports/monthly/2000

/data/exports/daily/2001
/data/exports/monthly/2001

/data/exports/daily/2002
/data/exports/monthly/2002

/data/exports/blahblah/something/another/etc

and so on...

I want to write a script, that will delete all directories within /data/exports/ but not the 2000,2001 and 2002 found at that first level. The 200x found within daily and etc i want gone though.

So i thought about writing up a script that would list all directories within /data/exports/ and those that are not numbers, do a rm -R on it...but i can't seem to get it right...

any thoughts, help appreciated.

Thanks.

This is complicated, I would post this on perl scripting.

If you were on Linux, things would have been easier by using -mindepth option of GNU find. In this case we need first to sort the directories that aren't needed:

cd /data/exports/

find . -type d | awk '!/\.\/200*/' | xargs -p rm -r        

Test it first, by removing rm -r part, ( -p will prompt you ), to see if you have the all the desired directories. If you need the dirs that are named as numbers 200x, then change the find part to:

find . -type d -name "200*"

This didn't work, my apology I'm answering this...

It deleted the sub-dir with 200* which is not supposed to happened according to the instructions above.

It did work fine for me. There's not much complexity in that command, it's just a simple find, select/sort the proper directories, ( instead of awk , a simple tail -n could have done that), and finally remove them.

Maybe you didn't do a cd first, or you might need to check your awk version. Please reread carefully O/P's requirement :).

I think you don't need to consider deleting the 200* on sub-dir then it's okay.

Thanks

thanks for all the responses guys...i've tried this:

find . -type d | awk '!/\.\/200*/' | awk -F"/" '{print $2}' | more

just to see what it would return, since technically, i will do a rm -fR on the top level dir...Seems like it will do the trick.

Thanks.

The double awks are wasteful, though.

find . -type d | awk -F "/" '!/\.\/200/ { print $2 }'

Note also that 200* in a regular expression means 20 or 200 or 2000 or 20000 or ...

Moving the thread to "Shell scripting" as requested by OP.

bakunin