How to Move Files and Directories in Linux with the mv Command

Moving files and directories is one of the most basic tasks you often need to perform on a Linux system.

In this tutorial, we will explain how to use the mv command to move files and directories.

How to Use the mv Command

The mv command (short from move) is used to rename and move and files and directories from one location to another. The syntax for the mv command is as follows:

mv [OPTIONS] source destination

The source can be one or more files or directories and destination can be a single file or directory.

  • If you specify multiple files or directories as a source, the destination must be a directory. In this case, the source files are moved to the target directory.
  • If you specify a single file as source, and the destination target is an existing directory then the file is moved to the specified directory.
  • If you specify a single file as source, and a single file as destination target then you’re renaming the file.
  • If the source is a directory and destination doesn’t, exist source will be renamed to destination, otherwise it be moved inside the destination directory.

To move a file or directory you need to have write permissions on both source and destination. Otherwise, you will receive a permission denied error.

For example, to move the file file1 from the current working directory to the /tmp directory you would run:

mv file1 /tmp

To rename a file specify the destination file name:

mv file1 file2

The command for moving directories is the same as when moving files. In the following example if the dir2 directory exists the command will move dir1inside dir2. If dir2 doesn’t exist, dir1 will be renamed to dir2:

mv dir1 dir2

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 file1 and file2 to the dir1 directory you would type:

mv file1 file2 dir1

The mv command also allows you to use pattern matching. For example, to copy all pdf files from the current directory to the ~/Documents directory you would use:

mv *.pdf ~/Documents

mv Command Options

The mv command accepts options that affect default command behavior. In some Linux distributions mv may be an alias to the mv command with a custom set of options. For example in CentOS mv is an alias to mv -i. You can find whether mv is an alias using the type command:

type mv

If mv is alias the output will look something like this:

mv is aliased to `mv -i'

If you specify conflicting options, the option specified last takes precendence.

Prompt before overwriting

By default if the destination file exists it will be overwritten. To prompt for confirmation use the -i option:

mv -i file1 /tmp
mv: overwrite '/tmp/file1'?

To overwrite the file type the character y or Y.

Force overwriting

If you try to overwrite a read only file the mv command will prompt you whether you want to overwrite the file:

mv -i file1 /tmp
mv: replace '/tmp/file1', overriding mode 0400 (r--------)? 

To avoid being prompted use the -f options:

mv -f file1 /tmp

This option is especially useful when you need to overwrite multiple reed only file.

Do not overwrite existing files

The -n option tells mv never to overwrite any existing file:

mv -f file1 /tmp

If a file1 exists the command above will do nothing, otherwise it will copy the file to the /tmp directory.

Backing up files

If the destination file exists you can create a backup of it using the -b option:

mv -b file1 /tmp

The backup file will have the same name as the original file with a tilde (~) appended to it.

Use the ls command to verify that the backup was created:

ls /tmp/file1*
/tmp/file1  /tmp/file1~

Verbose output

Another option that can be useful is -v. When using this option the command will print the name of each moved file:

mv -i file1 /tmp
renamed 'file1' -> '/tmp/file1'

Conclusion

By now you should have a good understanding of how to use the mv command to move files and directories. New Linux users who are intimidated by the command line can use the GUI file manager to move their files.

For more information about the mv command check the man page.

If you have any question or feedback, feel free to leave a comment.