× NETGEAR will be terminating ReadyCLOUD service by July 1st, 2023. For more details click here.
Orbi WiFi 7 RBE973
Reply

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

GJSchaller
Guide

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!

Model: RN21400|ReadyNAS 214 Series 4- Bay (Diskless)
Message 1 of 6

Accepted Solutions
bengsig
Aspirant

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

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

 

View solution in original post

Model: EDA500|ReadyNAS Expansion Chassis 5-Bay
Message 4 of 6

All Replies
bengsig
Aspirant

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

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.

Message 2 of 6
GJSchaller
Guide

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

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?

Message 3 of 6
bengsig
Aspirant

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

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

 

Model: EDA500|ReadyNAS Expansion Chassis 5-Bay
Message 4 of 6
GJSchaller
Guide

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

It's been a while since I've done UNIX scripting - thank you!

Message 5 of 6
GJSchaller
Guide

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

The missing #!/bin/sh was the issue.

As a note for anyone else, remember that different text editors end lines different ways - make sure you use UNIX line endings!

Message 6 of 6
Top Contributors
Discussion stats
  • 5 replies
  • 1762 views
  • 0 kudos
  • 2 in conversation
Announcements