Move Files and Folders on Linux

mv Command

The mv command is one of the basic Linux commands for copying files and directories from one location to another.

mv [options] source destination

+------------------------------------------------------------------+
| Option | Description |
+------------------------------------------------------------------+
| mv -f | force mv by overwriting destination file without prompt |
| mv -i | interactive prompt before overwrite |
| mv -u | update - move when source is newer than destination |
| mv -v | verbose - print source and destination files |
| man mv| help manual |
+------------------------------------------------------------------+

Move a File from One Location to Another in Linux Using the mv Command

mv /home/source/demo.txt /home/destination 

So we have moved the source file “/home/source/demo.txt” to the target “/home/destination” directory.

Moving Multiple Files and Directories

To move multiple files and directories, specify the files you want to move as the source. For example, to move the files source1 and source2 to the destination the directory by following command

mv source1 source2 destination

The following example is the same as above but uses pattern matching to move all files with a .txt extension.

mv *.txt folder

Options:

  1. -i (Interactive): -i option makes the command ask the user for confirmation before moving a file that would overwrite an existing file, you have to press y for confirm moving, any other key leaves the file as it is. This option doesn’t work if the file doesn’t exist, it simply rename it or move it to new location.
If file2 exists and is a file, it will be overwritten.
mv -i file file2

2. -f (Force): mv prompts for confirmation overwriting the destination file if a file is write protected. The -f option overrides this minor protection and overwrite the destination file forcefully and delete the source file.

mv -f  home/source/demo.txt home/destination

3. -n (no-clobber): With -n option, mv prevent an existing file from being overwritten. In the following example the effect is for nothing to happen as a file would be overwritten.

mv -n file file2

4. -b(backup): With this option it is easier to take a backup of an existing file that will be overwritten as a result of the mv command. This will create a backup file with the tilde character(~) appended to it

For example:

5. –version: This option is used to display the version of mv which is currently running on your system.

mv --version

Thanks for Reading!!!!!!!!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store