Question

I'm currently backing up a project into a tarball. I'd only like to keep the five most recent backups. Currently, the script reads:

tar -cjf $HOME/projects/foo.$(date +%Y%m%d%H%M%S).tar.bz2 $HOME/projects/foo > /dev/null 2>&1
find $HOME/projects -maxdepth 1 -name "foo*.tar.bz2" | ghead -n -5 | xargs rm > /dev/null 2>&1
# CR and blank line

The tarballs are created, but the old ones are never removed. Odd thing is, when I copy and paste the second line into a shell, the files are deleted as expected. The script functions as expected when called manually via the command line. Is the script not reaching the second line, or are there some rules about running commands via cron I'm not aware of?

Mac OS X 10.8 with gnu-coreutils. Attempted under cron and using launch services, with the same results.

Was it helpful?

Solution

The following script now works when called from cron or an equivalent. Thanks to @ansh0l for the clue.

PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin

cd $HOME/projects

tar -cjf foo.$(date +%Y%m%d%H%M%S).tar.bz2 foo
find ./ -maxdepth 1 -name "foo*.tar.bz2" | ghead -n -5 | xargs rm
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top