Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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 want to move a file from one folder to another folder.

When the file's format is with space in between (file _name.dat) and when I am using the command mv file _name.dat /tmp then the file is not moving.

But when am using mv "file _name.dat" /tmp then the file is moving.

Help me in writing a script which helps to change the file _name.dat to "file _name.dat" when it is moving automatically.

share|improve this question
It's not clear to me what to you want to do. Do you want to type mv file _name.dat /tmp and have it execute mv "file _name.dat" /tmp instead? – lk- Aug 16 '12 at 17:09
Recommended reading: $VAR vs ${VAR} and to quote or not to quote – Gilles Aug 16 '12 at 17:43

Your question is very hard to understand. In particular, you need to copy-paste your code and copy-paste error messages.

That being said, there's a good chance that you made a classic mistake…

When a shell script contains a variable substitution (something like $file or $1), the result of the expansion is treated as a whitespace-separated sequence of wildcard patterns. The variable is not treated as a single word, but as a list of patterns. So if you write

mv $1 /tmp      # <<<< BAD

and you run myscript "file _name.dat", then the mv command receives three arguments: file, _name.dat and /tmp. If you run myscript "*.dat", then all the .dat files in the current directory are moved.

To avoid this, put the variable substitution between double quotes: mv "$1" /tmp. If you do this, mv always receives exactly two arguments: the first argument of the script, and /tmp. It is extremely rare to need to treat variables' values as lists of wildcard patterns, so remember this simple rule:

Always put double quotes around variable substitutions: "$foo", "$1", etc. This also goes for command substitutions: "$(mycommand)"

Furthermore, in case the file name begins with a dash (-), mv would treat it as an option. To avoid this, pass -- to mark the end of the options: anything after -- is a non-option argument (a file or directory name, for mv). Most commands recognize -- to mark the end of options.

mv -- "$1" /tmp

If you wanted to invoke your script by writing myscript file _name.dat /tmp in a shell, that is not possible. The script receives the arguments separately, it doesn't know how many spaces you happened to type on the command line. (This is different from Windows, where a program parses its own arguments. On unix, programs receive a list of arguments.) If you call your script from another shell script, use proper quoting in the calling script as well.

share|improve this answer

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.