I have had a long standing script over 5 years preparing a daily backup of MySQL databases in a directory on the server. I want to know how to automatically delete files older than 90 days. Here is my current script:



for db in $(echo -e "show schemas like \"XXXX%\";" | mysql -u $uname -p$upass -N)
wget -q -nv -P ${dbprefix} --http-user=${uname} --http-password=${upass} --auth-no-challenge http://localhost:2082/getsqlbackup/${db}.sql.gz


find /pathtofiles/ -type f -mtime +90 -name '*.sql.gz' -execdir rm -- '{}' \;

also something like this should work too

find /pathtofiles/ -name '*.sql.gz' -mindepth 1 -mtime +90 -delete
  • Yes. But be really, really careful when using find -delete or the like. Jan 31 '18 at 19:58
  • One should test the find command with the -print command instead of -delete, then find simply shows the filenames it would delete. Jan 31 '18 at 21:23
  • OK, thanks. So I can add one of the code suggestions above and it should execute? Do I need to specify a variable anywhere else? (New to the programming scene).
    – JS7319
    Feb 1 '18 at 21:11
  • with find + delete script most important thing is to be sure that you have correct paths. In my scripts i have all variables inside scripts (this way i know where to find them )
    – Michał G
    Feb 6 '18 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.