How to clean old directories programmatically in unix / shell?

Does anyone have a good shell for this?

I want to check the age in the directory. If I created several directories weekly, and I want to clear or delete them, for example, after 7 days.

How should I do it?

+3
source share
1 answer

This will allow you to perform a dry run, delete echoif you like the exit

find /path/to/toplevel -type d -mtime +7 -exec echo rm -rf {} +

Update

If you have an older version findthat does not comply with POSIX 2004, use instead:

find /path/to/toplevel -type d -mtime +7 -exec echo rm -rf {} \;

or

find /path/to/toplevel -type d -mtime +7 -print0 | xargs -0 echo rm -rf {}

\; rm , xargs rm , rm , , . , +

+3

Source: https://habr.com/ru/post/1796413/


All Articles