Move To Directory

 mv $file $dir
will move the a file to the named directory ... if the named directory exists. If not, it will treat the "directory" name as a file, and move or rename the file to that directory. In other words, the command:
 for i in precious*
 do
mv $i dir
 done
will save all the files in the directory "dir" if "dir" is a directory, or rename each file as "dir" (all but the last will be lost) if "dir" is not a directory.

Therefore:

make sure the destination is unambiguously a directory reference:

 for i in precious*
 do
mv $i dir/
 done
will fail, loudly and obviously, if "dir" is not already a directory. This form is preferable to using "dir/$i" as the target for several reasons:
  1. It also "does the right thing" with the command "mv precious* dir/"
  2. It works if the file to be moved is the result of, say, a backquoted computation that does not really need to be saved in a variable.
  3. It easy enough to type for interactive use.

(Don't ask me for any of my archived e-mail messages from last month. )-:

If I do this:

 $ touch foo bar
 $ mv foo bar baz
then GNU mv complains about the last name not being a directory.


CategoryUnixShellPattern


Contributors: PaulChisholm, BillTrost


EditText of this page (last edited December 10, 2004) or FindPage with title or text search