- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
Need help crafting a script to delete files older than 60 days
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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!
Solved! Go to Solution.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
All Replies
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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!
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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!