How the mv Command works on Linux
📣 Sponsor
The mv
command, short for move, lets us move files and directories between two locations on Linux and other Unix like systems (i.e. MacOS).
The syntax for mv
can be found below, where [OPTIONS]
are optional settings you can use, SOURCE
is the the single or multiple files/directories you want to move, and LOCATION
is the location the file or directory should be moved to:
mv [OPTIONS] SOURCE LOCATION
How to move files on from Terminal in Linux or MacOS
At it's most basic, you can move a file using mv
by writing something like the following:
mv my-file.txt ./test
The above moves a file called my-file.txt
to a sub directory called test
. If you wish to rename the file when you move it, specify the name in the command. For example, the below will move a file called my-file.txt
to a folder called test
, and rename it to new-file.txt
:
mv my-file.txt ./test/new-file.txt
You can move directories with the same syntax.
Renaming files with mv
Since the mv
command moves a file without copying it, you can rename a file with it. For example, the following command will move a file called my-file.txt
to the same directory, and call it my-new-file.txt
, ultimately renaming it:
mv my-file.txt my-new-file.txt
Moving multiple files from Terminal on Linux and MacOS
If you should need to move multiple files or directories, simply separate them with a space. The final folder will be the location all files/directories are moved to. For example, the below will move all files and directories listed into the test
folder:
mv my-file.txt my-new-file-1.txt my-new-file.txt directory1 ./test
You can also use pattern matching. The command below moves all files starting with "my-file-" and ending with ".txt" to the test
directory:
mv my-file-*.txt ./test
Prompting Overwriting Files with mv
If a file with the same name already exists, by default mv
will overwrite it if it is writable. If you want a prompt before you overwrite the file, you can use the -i
option:
mv -i my-file.txt ./test
Forcing File Overwrites with mv
If a file is only readable, you will be prompted by default as to whether you want to overwrite it or not. If you want to force overwrite it without a prompt, use -f
:
mv -f my-file.txt ./test
Preventing File Overwrites with mv
If instead you want to prevent all file overwrites, use -n
:
mv -n my-file.txt ./test
Backing up files with mv
If you're on Linux, you can also use -b
to backup a file which is overwritten when using mv
. This will make a copy of the file about to be overwritten, with a tilde (~) at the end. This is not available on MacOS.
mv -b my-file.txt ./test
Messages when using mv
If you want to display messages regardless of what happens (often referred to as verbose), then you can use the -v
command:
mv -v my-file.txt ./test
# my-file.txt -> ./test/my-file.txt
More Tips and Tricks for Linux
- Delete .DS_Store recursively from all directories and sub-directories
- How the cd command works in Linux
- How to Rename Files in Linux and MacOS Terminal
- How the cat Command works on Linux
- How the mv Command works on Linux
- How to Kill a Process Running on a Port
- How to count all files in a directory in Linux
- How to make a Symbolic Link on Linux
- How the mkdir command works on Linux
- How the cp Command works on Linux