Linux and Unix mv command tutorial with examples
Tutorial on using mv, a UNIX and Linux command to move or rename files. Examples of moving a file, moving multiple files, moving a directory, prompting before overwriting and taking a backup before moving.
What is the mv command in UNIX? ¶
The mv
command is a command line utility that moves files or directories from
one place to another . It supports moving single files, multiple files and
directories. It can prompt before overwriting and has an option to only move
files that are new than the destination.
How to move a file ¶
To move a file using the mv
command pass the name of the file and then the new
name for the file. In the following example the file foo.txt
is renamed to
bar.txt
.
ls
foo.txt
mv foo.txt bar.txt
ls
bar.txt
How to move a file into a directory ¶
To move a file into a directory using the mv
command pass the name of the file
and then the directory. In the following example the file foo.txt
is moved
into the directory bar
.
tree -F .
.
├── bar/
└── foo.txt
mv foo.txt bar
tree -F
.
└── bar/
└── foo.txt
How to move multiple files into a directory ¶
To move multiple files using the mv
command pass the names of the files or a
pattern followed by the destination.
mv file1.txt file.2.txt file3.txt folder
The following example is the same as above but uses pattern matching to move all
files with a .txt
extension.
mv *.txt folder
How to move a directory ¶
To move a directory using the mv
command pass the name of the directory to
move followed by the destination.
ls -F
foo/
mv foo bar
ls -F
bar/
How to prompt before overwriting a file ¶
By default the mv
command will overwrite an existing file. Consider the
following example.
ls
foo.txt bar.txt
mv foo.txt bar.txt
ls
bar.txt
In this example bar.txt
is overwritten by the file file.txt
. At some point
when running mv
a file that was not meant to be overwritten probably will be.
To prompt before overwriting a file the -i
option can be used.
ls
foo.txt bar.txt
mv -i foo.txt bar.txt
mv: overwrite 'bar.txt'? n
How to not overwrite an existing file ¶
To prevent an existing file from being overwritten pass the -n
option. This
causes the mv
command to ignore anything that would overwrite an existing
file. In the following example the effect is for nothing to happen as a file
would be overwritten.
ls
foo.txt bar.txt
mv -n foo.txt bar.txt
ls
foo.txt bar.txt
How to only move files newer than the destination ¶
To only move files that are newer than the destination pass the -u
option. If
a file is not newer than a file with the same name in the destination it will
not be moved.
tree -F .
.
├── bar/
│ ├── bar.txt
│ └── foo.txt
├── bar.txt
└── foo.txt
Looking at the timestamps of these files we can see that the file foo.txt
in
the current directory is older than the file foo.txt
in the directory bar
.
ls -l
total 4
drwxr-xr-x 2 george users 4096 Oct 11 20:18 bar
-rw-r--r-- 1 george users 0 Oct 11 20:18 bar.txt
-rw-r--r-- 1 george users 0 Oct 11 20:20 foo.txt
ls -l bar
total 0
-rw-r--r-- 1 george users 0 Oct 11 20:18 bar.txt
-rw-r--r-- 1 george users 0 Oct 11 20:23 foo.txt
Using the -u
option means that only newer files will be moved.
mv -u foo.txt bar.txt bar
ls
bar foo.txt
The file foo.txt
is not moved as it is older than the file in the destination
folder.
How to take a backup of an existing file ¶
To take a backup of an existing file that will be overwritten as a result of the
mv
command pass the -b
option. This will create a backup file with the tilde
character appended to it.
ls
foo.txt bar.txt
mv -b foo.txt bar.txt
ls
bar.txt bar.txt~
To change the backup suffix the -S
option may be used.
ls
foo.txt bar.txt
mv -S .bak -b foo.txt bar.txt
ls
bar.txt bar.txt.bak
Further reading ¶
Tags
Can you help make this article better? You can edit it here and send me a pull request.
See Also
-
Linux and Unix ln command tutorial with examples
Tutorial on using ln, a UNIX and Linux command to make links between files. Examples of creating a hard link, creating a symbolic link, and a broken symbolic link. -
Linux and Unix rm command tutorial with examples
Tutorial on using rm, a UNIX and Linux command for removing files or directories. Examples of removing a file, removing multiple files, prompting for confirmation, removing recursively and forcing removal. -
Linux and Unix ls command tutorial with examples
Tutorial on using ls, a UNIX and Linux command for listing directory contents. Examples of listing a directory, showing hidden files, showing long listings, sorting on various items and showing recursive listings.