stell1315
stell1315

Reputation: 153

Linux MV command failing from Script - using variables

I'm trying to move files from within a bash script,

I have the source and destination saved to variables:

source='/Users/usr/Downloads/Drop/test\ movie.m4v'
dest='/Users/usr/Downloads/Movies/test\ movie.m4v'

And I'm running:

mv $source $dest

I get the output:

usage: mv [-f | -i | -n] [-v] source target
   mv [-f | -i | -n] [-v] source ... directory

What am I doing wrong!?

The files I'm moving may or may not contain spaces in their paths. I'm using sed to add the escape char before the space.

Thanks in advance :)

Upvotes: 2

Views: 2105

Answers (2)

stell1315
stell1315

Reputation: 153

Rather than using escape chars I'm just wrapping my variables in quotes, so:

source='/Users/usr/Downloads/Drop/test movie.m4v'
dest='/Users/usr/Downloads/Movies/test movie.m4v'

mv "$source" "$dest"

Upvotes: 0

l'L'l
l'L'l

Reputation: 47169

There are a bunch of ways to deal with filenames that contain spaces, one quick solution might be to wrap the variables in the mv command in quotes:

#!/bin/bash

source=/path/to/file\ with\ space
dest=/path/to/file\ with\ space2

mv "$source" "$dest"

Upvotes: 1

Related Questions