1

i had to do a script job in bash to delete all directory in a path which are older than a specified date except some of them. i know the name of the directories which shouldn't be deleted...

Can you help me???

i'm sorry but i'm a beginner...

lot of thanks!

mikjcl
  • 11
  • 1
  • 2

3 Answers3

3

This must be my find day.

find /yourpath -mmin +60 -type d -not \( -name dirname1 -o -name dirname2 \) -print0 | xargs -0 rm -r

will find and delete all directories older than 60 minutes (adapt accordingly) which are not named dirname1 or dirname2. You can extend this list with additional -o name dirname parts. Also, I would strongly recommend to replace the rm -r part with echo for testing.

Sven
  • 98,649
  • 14
  • 180
  • 226
2

try tmpwatch with the exclude options -x and -X

man tmpwatch for more info.

I've never used it myself with the excludes so i cant provide examples, but im pretty sure that'll work.

Sirex
  • 5,499
  • 2
  • 33
  • 54
1

For GNU find:

find . ! -newermt 2010/10/01 -type d -regextype posix-egrep ! -regex '^.*/(foo|bar|baz)/?.*' -exec echo rm -rf {} +

This will find and delete directories older than the given date that do not match the regular expression. Change "foo", etc., to match your directory names. Separate each name using |. Remove the echo when you're finished testing. The modification time is checked, change to -newerat to check the access time instead.

Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151