0

I used this script:

#!/bin/sh

for file in "$( find /var/backup/web2 -type f -mtime +1 -maxdepth 1 -exec rm -f {} \; )"
do
  rm -f $file
done

exit 0

to delete files older than 24h from fixed directory. The script stopped working, giving the error:

find: warning: you have specified the -maxdepth option after a non-option argument -type, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it). Please specify options before other arguments.

After I fixed the command according to given specifications, the script no longer produces any errors but neither does it work (older files still exist). Any ideas what caused a working script to fail and how to fix it?

  • 1
    That's an odd script and perhaps not working as you expect. The find command will remove any matching files. It won't print anything so the loop won't run. Try just running the find command on its own without the exec and change it until it shows the list of files you want to remove. – Paul Haldane Jun 28 '15 at 13:11
  • It was working perfectly for montchs, don't know why suddenly stopped. I tested it on a workstation, and then put on server. On my workstation it still do the job, not on server thou. – wjk Jun 28 '15 at 13:21
3

Not sure of the purpose of for loop in your script which is not needed and does not serve any purpose. The following will work as you expected.

find /var/backup/web2 -maxdepth 1 -type f -mtime +1 -delete
  • Thanx. It's working like a charm. I really don't remember why there was a loop in the script. – wjk Jun 28 '15 at 13:46

Your Answer

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