NETGEAR is aware of a growing number of phone and online scams. To learn how to stay safe click here.

Forum Discussion

GJSchaller's avatar
Jun 15, 2018
Solved

Need help crafting a script to delete files older than 60 days

I have a ReadyNAS that acts as a backup FTP site for some of my websites - every night / week, the websites back themselves up and download via FTP to the ReadyNAS.  So far, so good.

 

I'd like to make a script that deletes files that are older than 60 days, and run it on a weekly basis on my ReadyNAS, to clean up old backups. The script contains the following commands:

 

find /data/Backups/camp-sacajawea.com/* -mtime +60 -exec rm {} \;
find /data/Backups/geoffanddiane.com/* -mtime +60 -exec rm {} \;
find /data/Backups/gjschaller.com/* -mtime +60 -exec rm {} \;

 

The commands work fine when I run them from a SSH session / command line, but when I attempt to execute the .sh script, I get an error:

 

find: missing argument to `-exec'

 

Any idea how I can modify my script to pass the arguments I want, and have it delete the files older than 60 days?

 

Thank you!

  • bengsig's avatar
    bengsig
    Jun 17, 2018

    Hmm, I don't understand the \ in your script execution.  The script file (which does not need to have a .sh suffix) should have a first line containinig:

    #!/bin/sh

    and is should have the execute bit set e.g.

    chmod 755 yourscript.sh

    If your script is in any directory in PATH, you can then just call it

    yourscript.sh

    otherwise you need

    ./yourscript.sh

    with no spaces assuming the script is in your current directory.  The following works for me and it will happily do an ls of all files older than 600 days.  I don't want to remove them ;-)

     

     

    bjorn@nas:~$ cat oldfiles  
    #!/bin/sh  
    find . -mtime +600 -exec ls -ld {} \; 
    bjorn@nas:~$ ls -l oldfiles
    -rwxr-xr-x 1 bjorn engsig 49 Jun 17 13:54 oldfiles
    bjorn@nas:~$ ./oldfiles

     

5 Replies

Replies have been turned off for this discussion
  • It's probably the backslash in front of your semicolon that gets removed by the shell, although it depends on how your script is exactly written and called.

    • GJSchaller's avatar
      GJSchaller
      Guide

      The script is written as shown above - each line is its own line in the .sh file.

       

      I've tested it by running .\scriptname.sh from the command line - that is what generates the error.

       

      Is there an alternative format for the command to use in the script, or a different way to call it?

      • bengsig's avatar
        bengsig
        Aspirant

        Hmm, I don't understand the \ in your script execution.  The script file (which does not need to have a .sh suffix) should have a first line containinig:

        #!/bin/sh

        and is should have the execute bit set e.g.

        chmod 755 yourscript.sh

        If your script is in any directory in PATH, you can then just call it

        yourscript.sh

        otherwise you need

        ./yourscript.sh

        with no spaces assuming the script is in your current directory.  The following works for me and it will happily do an ls of all files older than 600 days.  I don't want to remove them ;-)

         

         

        bjorn@nas:~$ cat oldfiles  
        #!/bin/sh  
        find . -mtime +600 -exec ls -ld {} \; 
        bjorn@nas:~$ ls -l oldfiles
        -rwxr-xr-x 1 bjorn engsig 49 Jun 17 13:54 oldfiles
        bjorn@nas:~$ ./oldfiles

         

NETGEAR Academy

Boost your skills with the Netgear Academy - Get trained, certified and stay ahead with the latest Netgear technology! 

Join Us!

ProSupport for Business

Comprehensive support plans for maximum network uptime and business peace of mind.

 

Learn More