Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 6 down vote accepted

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.

share|improve this answer
Thank you very much!! – Nicholas Finch Jul 5 '13 at 6:12
@NicholasFinch if this works, don't forget to accept the answer, and perhaps vote it up if you feel it warrants it. – EightBitTony Jul 5 '13 at 6:27
I do not have enough of a reputation to vote it up - would otherwise do so gladly :) – Nicholas Finch Jul 5 '13 at 8:34
Also - please excuse my obvious ignorance on this matter - what do I press to 'accept' the answer? – Nicholas Finch Jul 5 '13 at 8:39
@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. – Dennis Williamson Jul 5 '13 at 11:52

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.