I am running a website on an Amazon ec2 instance running the default Debian linux. I set up a cronjob to copy the site files, take a database imprint, and zip them up. I want to keep the file archives in the same directory as the bash script itself, which is located somewhat deep within /var/www/[a new directory]/[a new directory]/[etc]/. I created a mysql user that can only read and dump the database, but cannot write to it.

The script is as follows




CURRDATE=$(date +"%Y-%m-%d__%T_%p")

# I first copy the files so I can stick a database imprint in the same directory
cp -r  "$SITEPATH/" "$CURRPATH/"
mv "$CURRPATH/html" "$TEMPDIR"
mysqldump -u "$MYSQLUSER" -p"$MYSQLPASS" "$MYSQLDB" > "$TEMPDIR/BackupDB_$CURRDATE.sql"

rm -rf "$TEMPDIR"

I set this with crontab, and it works perfectly, except for one problem - the archive ends up in the ~/root folder (I set up the crontab with the root user).

Is there a way to get this archive to spit out in the same directory as the script, the $CURRPATH variable?

1 Answer 1


CURRPATH is not the directory the script is in, it's the working directory that the script inherited from its parent process; for cron jobs, this is the home directory of the account it's running under (i.e. ~root). If you want the directory the script is in, use CURRPATH=$(dirname "$BASH_SOURCE") instead.

  • @NicholasFinch if this works, don't forget to accept the answer, and perhaps vote it up if you feel it warrants it. Jul 5, 2013 at 6:27
  • I do not have enough of a reputation to vote it up - would otherwise do so gladly :) Jul 5, 2013 at 8:34
  • Also - please excuse my obvious ignorance on this matter - what do I press to 'accept' the answer? Jul 5, 2013 at 8:39
  • 1
    @NicholasFinch: You should see a check mark next to the answer. Click on it to mark the answer as accepted. Also, see BashFAQ/028 regarding script locations. Jul 5, 2013 at 11:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .