NETGEAR is aware of a growing number of phone and online scams. To learn how to stay safe click here.
Forum Discussion
GJSchaller
Jun 15, 2018Guide
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 t...
- 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
GJSchaller
Jun 16, 2018Guide
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
Jun 17, 2018Aspirant
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
- GJSchallerJun 17, 2018Guide
It's been a while since I've done UNIX scripting - thank you!
- GJSchallerJun 18, 2018Guide
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!
Related Content
NETGEAR Academy

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