The Linux Move Command: Explained & Made Easy!

The Linux move command, or short 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 prompt 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.

Table of Contents

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/Code language: Bash (bash)

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

mv file1.doc /root/docsCode language: Bash (bash)

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/Code language: Bash (bash)
 move file from one directory to another

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/Code language: Bash (bash)

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/Code language: Bash (bash)
Linux Move command - move several files

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/docsCode language: Bash (bash)

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_directoryCode language: Bash (bash)

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

mv docs /reportsCode language: Bash (bash)
Move a directory to another - Linux move command

To confirm, run

ls /reportsCode language: Bash (bash)
 ls directory to confirm directory is moved

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/docsCode language: Bash (bash)

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

Prompt to overwrite a file

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/Code language: Bash (bash)

View Verbose output when moving files

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

mv -v file_name(s)  /directory/pathCode language: Bash (bash)

For example,

mv  -v file5.txt file6.txt /reports/docs/Code language: Bash (bash)
mv verbose

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.docCode language: Bash (bash)
mv-rename-a-file

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 mvCode language: Bash (bash)
man mv command

Conclusion

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.

Check out other tutorials over in the Linux category! If you want an easy Linux distro to get your feet wet, I always recommend Ubuntu.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap