How to Move Files and Directories on Linux (mv Command)

Introduction

One of the most basic operations you'll need to conduct on a Linux system is moving files and folders.

In this tutorial, we'll show you how to use the mv command to relocate files and directories.  We will also address a few FAQs on how to move files and directories on Linux (mv Command).

How to Use the mv Command

To rename and move files and directories from one location to another, use the mv command (short for move). The mv command has the following syntax:

mv [OPTIONS] SOURCE DESTINATION

DESTINATION can be a single file or directory, and SOURCE can be one or more files or folders.

  • When a SOURCE contains numerous files or folders, the DESTINATION must be a directory. The SOURCE files are relocated to the target directory in this situation.
  • When you indicate a single file as SOURCE and an existing directory as the DESTINATION target, the file is relocated to that directory.
  • You're renaming a file if you give a single file as SOURCE and a single file as DESTINATION target.
  • SOURCE will be renamed to DESTINATION if SOURCE is a directory and DESTINATION does not exist. If DESTINATION already exists, it will be transferred to the DESTINATION directory.

You must have write rights on both SOURCE and DESTINATION in order to move a file or directory. If you don't, you'll get a permission refused error.

To move the file file1 from the current working directory to the /tmp directory, for example, use the command:

mv file1 /tmp

You must give the destination file name when renaming a file:

mv file1 file2

For relocating directories, the syntax is the same as when moving files. If the dir2 directory exists, the command in the following example will transfer dir1 within dir2. Dir1 will be renamed to dir2 if dir2 does not exist:

mv dir1 dir2

Moving Multiple Files and Directories

Specify the files you want to move as the source to move multiple files and folders. To copy the files file1 and file2 to the dir1 directory, for example, type:

mv file1 file2 dir1

You can also use pattern matching with the mv command. To relocate all pdf files from the current directory to the ~/Documents directory, for example, run the command:

mv *.pdf ~/Documents

mv Command Options

The mv command accepts a number of arguments that change the default behavior of the command.

mv may be an alias for the mv command with a custom set of options in some Linux distributions. In CentOS, for example, mv is an alias for mv -i. The type command can be used to see if mv is an alias:

type mv 

If mv is set to alias, the output will look like this:

Output

mv is aliased to `mv -i'

If there are multiple possibilities, the latest one gets precedence.

Prompt before overwriting

If the target file already exists, it will be rewritten by default. Use the -i option to ask for confirmation:

mv -i file1 /tmp
Output

mv: overwrite '/tmp/file1'?

Type y or Y to overwrite the file.

Force overwriting

When you try to overwrite a read-only file, the mv command will ask if you want to overwrite it:

mv -i file1 /tmp
Output

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

Use the -f options to avoid being prompted:

mv -f file1 /tmp

If you don't want to be prompted, use the -f options:

Do not overwrite existing files

The -n option tells mv to never overwrite a file that already exists:

mv -n file1 /tmp

If a file1 already exists, the command above will have no effect. If not, the file will be moved to the /tmp directory.

Backing up files

If the destination file already exists, the -b option can be used to create a backup:

mv -b file1 /tmp

The backup file will be named the same as the original file, but with a tilde (~).

Verify that the backup was generated with the ls command:

ls /tmp/file1*
Output

/tmp/file1  /tmp/file1~

Verbose output

The -v option is another handy option. The command prints the name of each transferred file when this option is selected:

mv -v file1 /tmp
Output

renamed 'file1' -> '/tmp/file1'

FAQs to Move Files and Directories on Linux (mv Command)

What happens if I move a file to a directory that already contains a file with the same name?

If a file with the same name already exists in the destination directory, it will be overwritten by the one you are moving. Exercise caution while using this command.

Is it possible to move multiple files at once with the mv command? 

Yes, you can move multiple files by specifying all their names followed by the destination directory: mv <file1> <file2> ... <destination_directory>.

Can I move a directory and all its contents to a different location? 

Absolutely! To move a directory along with its contents, simply use the command: mv <directory> <destination_directory>.

Is it necessary to provide the full path when moving files or directories using the mv command? 

No, it is not necessary. If you are in the same directory as the file or directory you want to move, you can simply provide its name without the full path.

How can I rename a file using the mv command? 

To rename a file, you can move it to the same directory with a different name, like this: mv <current_name> <new_name>.

What permissions are required to move a file or directory using mv? 

You need write permissions on the file or directory you are moving, as well as write permissions on the destination directory.

Can I use a wildcard (*) with the mv command to move multiple files or directories simultaneously? 

Yes, you can use a wildcard to match multiple files or directories. For example, mv *.txt <destination_directory> will move all files ending with ".txt" to the desired location.

Conclusion

Move and rename files and directories with the mv command.

Check the man page or type man mv in your terminal for further information on the mv command.

To move files, new Linux users who are frightened by the command line might utilize the GUI file manager.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.