4.2.13. mv
— move a file or directory#
With the mv
command, you can move a file or directory
from one location (source) to another (destination).
You can choose to keep the existing file or directory
name or rename them.
The basic format of the command is:
mv source destination
A safer approach is to add the -iv
options to the
command:
mv -iv source destination
With -i
(interactive), mv
will require your
confirmation before overwriting a file or directory,
if it exists already in destination.
-v
(verbose) will print the command’s actions on
the screen.
For convenience, you can add an alias.
Sample files#
To follow the examples below, you will need to copy the following files into your home directory using the cp command:
cp -v /usr/share/dict/words ~
cp -rv /usr/share/doc/bash ~
~
is a shortcut for home directory.
Moving a file or directory#
The simplest use case is to move a file or directory from one location (directory) to another.
Moving a File#
For example, to move the words
file copied
above into your Documents
directory, use:
mv -iv words Documents/
Output:
renamed 'words' -> 'Documents/words'
Moving a directory#
Similarly, to move the bash
directory copied
above into your Documents
directory, use:
mv -iv bash Documents/
Output:
renamed 'bash' -> 'Documents/bash'
In both cases, the file or directory name will not be changed.
Renaming a file or directory#
In this case, you would like to rename a file or directory.
Renaming a file#
To rename the words
file copied
above to dictionary.txt
, use:
mv -iv words dictionary.txt
Alternatively, to move it into your Documents
directory and rename it at the same time, use:
mv -iv words Documents/dictionary.txt
Renaming a directory#
To rename the bash
directory copied
above into bash-commands
, use:
mv -iv bash bash-commands
Notes#
What happens if a file exists?#
You will notice a prompt requesting you for confirmation to overwrite the file.
Type y
and press the ENTER key to proceed:
mv: overwrite 'Documents/words'? y
renamed 'words' -> 'Documents/words'
To cancel, simply press ENTER key at the prompt.
What happens if a directory exists?#
mv
will overwrite a directory only if it is empty.
You can either:
copy files into destination directory or
rename the destination directory
Adding an alias for mv
#
Rather than typing mv -iv
, every time you need to use
the command, you can add an alias
for the command in your ~/.bash_aliases
file.
For example:
alias mv='mv -iv'
Now, when you type mv
, you will actually be running
mv -iv
.