I want a way to run daily cron job on aserver that will delete all files only (not folders) which are older than X days from today's date, in the public_ftp
folder and all its sub folders to a specified max depth level?
1 Answer
The following script will do the trick:
#!/bin/sh
find /path/to/folder/toclean -mtime +5 -type f -maxdepth 3 -exec rm -f {} \;
This assumes that the directory you wish to clean up is /path/to/folder/toclean, just replace it with your name. The command find -mtime +5 -type f finds all the files (-type f) which were last modified more than (+) 5 days ago, while -maxdepth 3 limits the depth of directory to search. The exec performs the actual removal.