Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove files older than 30 days without using find, using a shell script

Tags:

linux

bash

I have been looking for quite a while for an answer to this.

My hosting environment will not let me use the find command. Is there a way to search a directory for all files older than 30 days and delete/rm them? Any reading or examples would be greatly appreciated.

Thanks in advance.

like image 823
itivae Avatar asked May 02 '26 03:05

itivae


1 Answers

Try something like this:

touch -d 'now -30 days' reference_file
for f in *; do
    if [ "$f" -ot reference_file ]; then
        rm "$f"
    fi
done

Or is touch also on the blacklist?

like image 199
Thomas Erker Avatar answered May 03 '26 19:05

Thomas Erker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!