When moving files withmv
, suffix your destination folder name with/
to avoid accidentally overwriting files.
The problem
Despite its name, the little command-line tool mv
can do many things. Not only does it move files around the filesystem, it can also rename them (which counts as "moving" since its just a special case of editing the file path).
And the command line is full of DWIM magic, so sometimes you might end up renaming a file when you meant to move it, or vice versa. One example of this is moving something to what you mistook for a folder and was actually another file :
mv server.log tmp
If tmp doesn’t exist, or if it is a file, this command renames server.log to tmp, overwriting anything that was there.
Trailing Slash to the rescue
One way to avoid this is to use commands that mv can’t interpret as file renames. To achieve this, suffix your folder names with a slash:
mv server.log tmp/
If tmp is not a folder, you will get an error message and server.log will not be moved.
That’s all!