How the MV Command Works on Linux
Let's look at how this very common command works, and how to use it to move files and directories around.
Join the DZone community and get the full member experience.Join For Free
The mv command, short for the 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 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 its 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 subdirectory 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.
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 that 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
Published at DZone with permission of Johnny Simpson, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.