mv Command in Linux: Move Files and Directories

By 

Updated on

7 min read

Move Files and Directories in Linux - mv Command

When you need to reorganize directories, rename files in place, or move data between locations, the mv command is one of the fastest tools to use.

To move a file in Linux, use mv SOURCE DESTINATION. For example, mv file.txt /tmp/ moves file.txt into the /tmp directory.

The mv command (short for move) is used to move and rename files and directories from one location to another. This guide explains how to use the mv command with practical examples covering common options.

mv Command Syntax

The general syntax for the mv command is:

txt
mv [OPTIONS] SOURCE DESTINATION

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

How mv behaves depends on the source and destination:

  • If SOURCE is a file and DESTINATION is a directory, the file is moved into that directory.
  • If SOURCE is a file and DESTINATION is a file, the source is renamed to the destination.
  • If multiple files are given as SOURCE, the DESTINATION must be a directory.
  • If SOURCE is a directory and DESTINATION does not exist, SOURCE is renamed to DESTINATION. If DESTINATION exists and is a directory, SOURCE is moved inside it.

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

Moving Files

To move a file from the current working directory to another directory:

Terminal
mv file1 /tmp

To move a file and give it a new name at the destination:

Terminal
mv file1 /tmp/newname.txt

If the file name starts with a dash (-), add -- before the source name so mv does not treat it as an option:

Terminal
mv -- -file.txt /tmp/

Renaming Files

To rename a file within the same directory, specify the new name as the destination:

Terminal
mv file1 file2

This renames file1 to file2 in the current directory.

Moving Directories

The syntax for moving directories is the same as for files. If dir2 exists, the command moves dir1 inside dir2. If dir2 does not exist, dir1 is renamed to dir2:

Terminal
mv dir1 dir2

Moving Multiple Files

To move multiple files, list them before the destination directory:

Terminal
mv file1 file2 file3 dir1

You can also use pattern matching. For example, to move all .pdf files to the ~/Documents directory:

Terminal
mv *.pdf ~/Documents

The -t (target directory) option is useful when combining mv with other commands like find :

Terminal
find /tmp -name '*.log' -exec mv -t /var/log/archive {} +

This moves all .log files found in /tmp to /var/log/archive.

Info
The -t option is supported by GNU mv (common on Linux). It is not available on some BSD or macOS systems.

mv Command Options

The mv command accepts several options that affect its default behavior.

On some Linux distributions, mv is aliased with default options. For example, some systems alias mv to mv -i. You can check whether mv is an alias using the type command:

Terminal
type mv

If mv is aliased, the output will look like this:

output
mv is aliased to `mv -i'

If conflicting options are given, the last one takes precedence.

Prompt Before Overwriting (-i)

By default, if the destination file exists, it is overwritten without warning. To prompt for confirmation, use the -i (interactive) option:

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

Type y to overwrite or n to skip.

Force Overwriting (-f)

When you try to overwrite a read-only file, mv prompts for confirmation by default:

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

To skip the prompt and force the overwrite, use the -f option:

Terminal
mv -f file1 /tmp

This option is useful when you need to overwrite multiple read-only files without being prompted for each one.

Do Not Overwrite (-n)

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

Terminal
mv -n file1 /tmp

If file1 already exists in /tmp, the command does nothing. Otherwise, it moves the file.

Create Backups (-b)

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

Terminal
mv -b file1 /tmp

The backup file has the same name as the original with a tilde (~) appended. Use the ls command to verify:

Terminal
ls /tmp/file1*
output
/tmp/file1  /tmp/file1~

To use a custom suffix instead of ~, use --suffix:

Terminal
mv -b --suffix=.bak file1 /tmp

This creates a backup named file1.bak.

Verbose Output (-v)

The -v option prints the name of each file as it is moved:

Terminal
mv -v file1 file2 /tmp
output
renamed 'file1' -> '/tmp/file1'
renamed 'file2' -> '/tmp/file2'

Moving Files Across Filesystems

When you move a file within the same filesystem, mv simply updates the directory entry without copying any data. This is nearly instant regardless of file size.

When you move a file to a different filesystem (for example, from your hard drive to a USB drive), mv copies the file to the destination and then deletes the original. This takes longer for large files and means the move is not atomic. If the process is interrupted, the file may exist in both locations or be incomplete at the destination.

Troubleshooting

mv: cannot stat 'file': No such file or directory
The source path is incorrect or the file has already been moved. Verify the path with ls and quote paths that contain spaces.

mv: cannot move ...: Permission denied
You do not have write permissions on the source or destination directory. Check permissions with ls -ld and use sudo only when required.

Move across filesystems was interrupted
When moving across filesystems, mv copies data and then removes the source. If interrupted, verify both source and destination paths before deleting anything manually.

mv: target 'dir' is not a directory
This happens when you pass multiple source files and the destination does not exist as a directory. Create the destination directory first or pass a single source file.

Quick Reference

For a printable quick reference, see the mv cheatsheet .

CommandDescription
mv file1 /tmpMove a file to a directory
mv file1 file2Rename a file
mv dir1 dir2Move or rename a directory
mv file1 file2 dir/Move multiple files to a directory
mv *.txt dir/Move files matching a pattern
mv -i file1 /tmpPrompt before overwriting
mv -f file1 /tmpForce overwrite without prompting
mv -n file1 /tmpDo not overwrite existing files
mv -b file1 /tmpCreate backup of destination before overwriting
mv -v file1 /tmpPrint each file as it is moved
mv -t dir/ file1 file2Specify target directory first

FAQ

What is the difference between mv and cp?
mv moves or renames a file, and the original is removed from its source location. cp creates a copy, leaving the original in place.

Does mv preserve file permissions and timestamps?
Yes. When moving within the same filesystem, mv preserves all metadata. When moving across filesystems, mv copies the file and attempts to preserve permissions, ownership, and timestamps.

How do I move hidden files (dotfiles)?
The glob * does not match hidden files by default. Use mv .[!.]* ..?* /destination/ to move dotfiles safely, or use shopt -s dotglob in Bash to include dotfiles in glob patterns.

How do I move a file without overwriting the destination?
Use the -n option: mv -n file1 /tmp. If the destination file already exists, mv does nothing.

Can I undo a mv command?
No. The mv command does not have an undo feature. If you used -b, you can restore the backup file manually. Otherwise, you must move the file back to its original location yourself.

Conclusion

Use mv for moves and renames, add -i or -n when overwrites are possible, and use cp when you need to keep the original file.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page