Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
March 25, 2022 01:37 pm GMT

How the mv Command works on Linux

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

Original Link: https://dev.to/smpnjn/how-the-mv-command-works-on-linux-pm6

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To