0

In a bash script, I have:

TEMPPATH="/tmp/directory" BACKUPPATH="/backup/path" 

I'm trying to move one file using:

mv $TEMPPATH/file $BACKUPPATH/file 

I want to move the file from /tmp/directory/file to /backup/path/file, but instead it ends up in /backup/path/tmp/directory/file. I've tried adding and removing trailing slashes from the destination, and excluding the filename from the destination (just using the destination directory). I've searched and find a lot of methods for moving lots of files in a directory, but I only want to move one.

========

EDIT: here is the script section that causes the problem. It's part of a script that backs up WordPress folders and databases. Note: if you are wondering why I'm not just zipping the files directly to the backup location, it's because I'm writing to Keybase. If you haven't used Keybase, since i/o to and from it is so slow, my goal was to zip files locally then move the file after zipping is complete.

#back up the WordPress folder and move over to backup location echo Compressing site files zip -r --quiet $TEMPPATH/$DATEFORM-$SITE.wp-content.zip . echo Moving site zip file to Keybase #mv $TEMPPATH/$DATEFORM-$SITE.wp-content.zip $BACKUPPATH/$SITE/$DATEFORM-$SITE.wp-content.zip # ---------> this mv command works as expected: mv $TEMPPATH/$DATEFORM-$SITE.wp-content.zip $BACKUPPATH/$SITE #back up the WordPress database, compress, move to backup location and clean up echo Exporting database /usr/local/bin/wp db export $TEMPPATH/$DATEFORM-$SITE.sql --all-tablespaces --single-transaction --quick --lock-tables=false --allow-root --skip-themes --skip-plugins echo Compressing database zip --quiet $TEMPPATH/$DATEFORM-$SITE.sql.zip $TEMPPATH/$DATEFORM-$SITE.sql # ---------> this is the way the script was originally written -- maybe this cat method was used to prevent the same problem I am having? # cat $TEMPPATH/$DATEFORM-$SITE.sql | zip > $TEMPPATH/$DATEFORM-$SITE.sql.zip echo Moving sql zip file to Keybase # ---------> this is the mv command that results in the directory tree being copied over to the destination mv $TEMPPATH/$DATEFORM-$SITE.sql.zip $BACKUPPATH/$SITE echo Removing tmp file rm $TEMPPATH/$DATEFORM-$SITE.sql 
4
  • 4
    Welcome! What you have works for me. Are you sure that you didn't misspell on of the variables in your script? Do any of your paths contain spaces? Commented Dec 16, 2018 at 20:54
  • 1
    What's the output of type mv? Commented Dec 16, 2018 at 20:56
  • @jimmij, type mv returns mv is /bin/mv Commented Dec 17, 2018 at 5:21
  • 1. It is a good idea to double-quote the variables, for example "$DATEFORM"; 2. Furthermore, it is a good idea to use curly brackets to mark the end of a variable, when it is directly followed by some character (except whitespace) or another variable, for example `"${DATEFORM}-${SITE}.sql.zip" Commented Dec 17, 2018 at 7:23

1 Answer 1

0

I tried your example and it works as expected - see output below. What mv version do you use and on what system? And a piece of advice, to avoid any special characters (e.g. names with spaces) doing unexpected things I recommend quoting your variables and you can drop file name from the target: mv "$TEMPPATH/file" "$BACKUPPATH/".

Example from the question:

$ tree /tmp/directory /tmp/directory └── file 0 directories, 1 file $ tree /backup /backup └── path 1 directory, 0 files $ TEMPPATH="/tmp/directory" $ BACKUPPATH="/backup/path" $ mv $TEMPPATH/file $BACKUPPATH/file $ tree /tmp/directory /tmp/directory 0 directories, 0 files $ tree /backup /backup └── path └── file 1 directory, 1 file 

Still cannot reproduce:

$ tree /tmp/test /backup/ /tmp/test └── some-example.sql /backup/ └── path 1 directory, 1 file $ TEMPPATH=/tmp/test $ DATEFORM=some $ SITE=example $ BACKUPPATH=/backup/path $ zip --quiet $TEMPPATH/$DATEFORM-$SITE.sql.zip $TEMPPATH/$DATEFORM-$SITE.sql $ mv $TEMPPATH/$DATEFORM-$SITE.sql.zip $BACKUPPATH/$SITE $ tree /tmp/test /backup/ /tmp/test └── some-example.sql /backup/ └── path └── example 1 directory, 2 files 
4
  • How strange. I don't understand what I'm doing wrong. I did try dropping the filename from the target as suggested. Commented Dec 17, 2018 at 5:24
  • I added the actual script above in case that helps. Commented Dec 17, 2018 at 5:40
  • I think it isn't fault of the mv command - there must be something changing the variable used to specify the target. Maybe try printing the command (echo mv $TEMPPATH/$DATEFORM-$SITE.sql.zip $BACKUPPATH/$SITE) or related variables (or log to a file echo "$BACKUPPATH/$SITE" >> /tmp/log) every few lines. Commented Dec 17, 2018 at 14:37
  • I made some changes as recommended and now I am getting the file to move properly, though the filename when extracted is simply "-". Perhaps there is something wrong with the way the variable is stored. Thanks everyone for your input. Learned lots. Commented Dec 17, 2018 at 17:41

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.