Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This must be a very simple issue to solve, but I am stuck (not a UNIX pro). I need to move a gz to a target directory, but I get a No such file or directory.

NOW=$(date +"%Y-%m-%d-%T")

DEST="~/scripts/backup"
SQL_DUMP="db_dump_$NOW.sql.gz"
echo "$SQL_DUMP"

drush sql-dump | gzip -9 > $SQL_DUMP
mv -t $DEST $SQL_DUMP

The sql.gz is created properly, only the last command does not work. How should I use it? Thanks.

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

The problem is not with your mv command.

Tilde expansion is the name for what the shell does when it replaces the ~ character with the user's home directory, e.g. /home/username. It is possible to tell the shell to not do this by quoting the tilde character. This is exactly what you have done in

DEST="~/scripts/backup"

You have assigned to DEST the string ~/scripts/backup. This directory does not actually exist. Two solutions:

  • Don't quote the ~ character:

    DEST=~/scripts/backup
    
  • Use $HOME instead:

    DEST="$HOME/scripts/backup"
    

A final debugging tip

Put set -x at the top of your script to turn on the xtrace option. If you had done that you would have seen that the mv command was using the wrong directory, ~/scripts/backup instead of /home/yourname/scripts/backup.

share|improve this answer
add comment

Read mv manual

SYNOPSIS
    mv [SOURCE] [DEST]

And you may want to check if $DEST already exist:

if [ -d ${DEST} ]; then
    do something
fi

If not, use mkdir with -p as parameter to make parent directories.

...
else
    mkdir -p ${DEST}
fi
share|improve this answer
    
I did try those mv source target too, but same issue. I think it has to do with '~'. The directory exists, this is not the issue. –  JVerstry Feb 17 '12 at 21:40
    
Then use absolute path: /home/user/scripts/backup, or use $HOME –  ramonovski Feb 17 '12 at 21:42
add comment

Your Answer

 
discard

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.