Shell – How to delete all files in a folder that haven’t been accessed in a certain amount of time

crondatefilesshell-script

I'd like to run a nightly cron job that deletes all the files in a folder that haven't been accessed in a week or more. What is the most efficient way to do this in bash?

Best Answer

You want the find tool.

find folder -depth -type f -atime +7 -delete

(This will delete all files (only regular ones, no pipes, special devices, directories, symbolic links) in the given folder and all subdirectories (recursively) where the last access time is longer than 7 days ago.)

Related Question