How to move Files or Folders with mv command in Ubuntu with examples

This brief tutorial shows students and new users how to use the mv command in Ubuntu to move or rename files and directories.

On Linux systems, including Ubuntu the mv command can be used to move or rename files and directory. It is also commonly used when creating a backup of existing files and folders.

If you’re a student or new user looking for a Linux system to start learning on, the easiest place to start is Ubuntu Linux OS. It’s a great Linux operating system for beginners and folks looking for easier Linux distribution to use.

Ubuntu is an open-source Linux operating systems that runs on desktops, laptops, server and other devices.

Both Ubuntu and Windows systems allow you to be productive, easy to use, reliable and enable you to install and run thousands of programs from gaming to productivity suite software for individuals and businesses.

About mv command:

The mv command moves or renames files and folders on Linux systems, including Ubuntu. If you use the -b or –backup options, the mv will rename the destination file if it exists, appending a suffix to its file name. this prevents overwriting existing files.

Syntax:

Syntax is the rule and format of how the mv command can be used. These syntax options can be reordered, but a straight format must be followed.,.

Below is an example syntax of how to use the mv command.

mv [OPTION].  [-T]  SOURCE     DESTINATION
mv [OPTION].   SOURCE.     DIRECTORY

Options:

The command line options are switches or flags that determined how the commands are executed or controlled. they modify the behavior of the commands. they are separated by spaces and followed after the commands.

Below are some options of the mv command:

  SOURCE  Replace SOURCE with the source of the file or folder
 DESTINATION  Replace DESTINATION with the destination of the file or folder
  –backup[=CONTROL]    If you use the -backup option, mv make a backup of each existing destination file
     -b Use the -b mv behaves like –backup but does not accept an argument
    -f, –force Use the -f or –force, mv does not prompt before overwriting existing content
    -i, –interactive The -i or interactive option, mv prompts before overwriting exiting content
     –help Display a help message and exit.

If you use the -b or –backup options, mv will rename the destination file if it exists, appending a suffix to its file name. This saves a copy of the original file instead of overwriting it.

For example, if you want to rename a file called myfile.txt to myprivate.txt, simply run the commands below:

Example:

mv myfile.txt myprivate.txt

The command above moves or deletes myfile.txt and create a new file with content called myprivate.txt

If you wish to move myfile.txt into a different directory in your home folder called private, simply run the commands below:

Example:

mv myfile.txt /home/private

The command above moves myfile.txt into the specified directory called private inside your home folder.

Use the -f option, forces the mv command to overwrite existing file with same name in the destination without prompted.

mv -f myfile.txt /home/private

When you use the -i option, the mv command prompts before overwriting exiting files with same name.

mv -i myfile.txt /home/private/

If there’s already a file called myfile.txt inside the /home/private directory, a prompt is given:

Output:
mv: overwrite '/home/private/myfile.txt'?

Entering "y", "yes", "Yes", or "Y" will result in the file being overwritten

When you use the -b or –backup option, the mv command makes a copy of existing file.

Example:

mv -b myfile.txt /home/private

If myfile.txt exists in the /home/private, the exiting copy will be renamed to

/home/private/myfile.txt~

For moving directories, simply provide the source directory and the destination:

Example:

mv secrets /home/secrets

There are many more complex options you can use with the mv commands, but the few above will get you started with how to use the mv command on Ubuntu.

Conclusion:

This post shows you how to use the mv command on Ubuntu to move or renew files and directories. If you find any error above, please comment below to alert us.