The Linux move command –mv – is a Linux/Unix command-line tool used for moving or renaming files and directories depending on the arguments used. It gives the user the ability to move single or multiple files, rename files and even prompting you before overwriting a file. In this tutorial, we will discuss the Linux Move command mv and how you can use it to move or rename your files or directories.

How to move a file from one directory to another

To move a file from one directory to another, use the syntax:

# mv filename /directory/path/

For example, to move a file file1.doc to the directory /root/docs/ execute the command:

# mv file1.doc /root/docs

The command moves the file entirely from its current directory to the directory defined as /root/docs/. You can confirm the existence of the file in its new location by running the ls command as shown:

# ls -l /root/docs/

Move multiple files from one directory to another

To move multiple files from one directory to another, use the syntax:

# mv file1 file2 . . . /directory/path/

For instance, to move 3 files, file1.txt, file2.txt, and file3.txt to /root/docs run the command

# ls file1.txt file2.txt file3.txt /root/docs/

Use pattern matching to move files

Moving multiple files one by one can be a tedious task. The Linux move command has made it a whole lot easier to move multiple files which share the same file extension.

In the previous example, if you want to move all the files which have the .txt extension, use the wild card symbol as shown:

# mv *.txt /root/docs

Move a directory to another

When moving directories, the syntax remains pretty much the same as when moving files. The syntax for moving a folder or directory to another is:

# mv folder /destination_directory

For example, to move the docs directory to /reports directory, run the command:

# mv docs /reports

To confirm, run

# ls /reports

How to prompt before overwriting a file

When you move a file to another directory with the same file name, the destination file will usually be overwritten. However, you can use the -i option with mv command to prompt you whether or not to overwrite the file. This will prevent accidental overwriting of a file.

# mv -i file1.txt /reports/docs

To go ahead with overwriting the file, type Y (for Yes) and hit ENTER.

If you wish to cancel the operation, simply Type N (For No) and hit ENTER.

Move files newer than the destination

Sometimes, you may need to update files in the destination directory. To do so, use the -u flag

# mv -u file1.txt /reports/docs/

View Verbose output when moving files

The Linux move command can allow you to have a glimpse of the on-going process using the -v option. To view the files being moved use the syntax:

# mv -v file_name(s) /directory/path

For example,

# mv -v file5.txt file6.txt /reports/docs/

How to rename files using the Linux move command

Aside from moving files, the mv command can be used to rename files. This happens only and only if the source and destination directory of the original and renamed file is the same.

For instance to rename a file CentOS_tutorials.doc to Ubuntu_tutorials.doc run the command

# mv CentOS_tutorials.doc Ubuntu_tutorials.doc

View more options about the Linux mv command

We have covered the main usages of the Linux mv command. However to quench your curiosity about more options that are used with the command, open the man pages for the command by running

# man mv

And with that, we come to the end of this tutorial. It’s our hope that you can now move and rename files on your Linux system without any difficulty.

Please Share! Email

Facebook

Reddit

Twitter

Pinterest

More

LinkedIn

