mv — Moving Files and Directories
Asha's garden photos keep landing in her home directory, and last month's notes are sitting in the wrong folder. On the command line there is no dragging with a mouse. One command moves things, and the same command renames them.
The rule
mv takes the thing first, then where it goes.
The shape is always:
mv SOURCE DESTINATIONWhat happens depends on the destination:
- If the destination is an existing directory, the source moves into it and keeps its name.
- If the destination does not exist, the source is renamed to that name.
- If the destination is an existing file, it is replaced, silently, with no warning.
$ cd ~/garden
$ mv ../tomato.jpg plots/ # moves into plots, still called tomato.jpg
$ mv notes.txt june-notes.txt # renames in place
$ mv plots beds # renames a whole directoryMoving a directory needs no special option. mv doesn't copy anything; it just changes where the name points, so the whole folder goes along, whatever it contains.
You can move several things at once. When you give more than one source, the last argument must be a directory:
$ mv tomato.jpg basil.jpg chilli.jpg plots/Two safety options are worth learning now:
mv -i(interactive) asks before it overwrites a file that already exists.mv -n(no-clobber) never overwrites. It just skips that file.