Delete older files with Unix find and xargs

find . -type f -mtime +5  -print0 | xargs -0 rm
find . -type d -depth -print0  | xargs -0 rmdir  2>/dev/null

With these two bash commands, you can recursively delete all files older than 5 days and after that all now empty directories. I use these commands to clean up temp directories.

In the first line -mtime +5 finds all files that are older than 5 days. The + sign is important. Without it, only those being exactly 5 days (5 * 86400 seconds) old would be found.

In the second line -depth makes that child directories are treated before parents. That is important here. Otherwise a directory a which contained only an empty directory b would not be deleted.

rmdir only deletes empty directories. For that you don’t see rmdir's griping about non-empty ones, the 2>/dev/null is used.