mv Command in Linux: Move Files and Directories

By 

Updated on

11 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

Everything inside the directory moves with it. There is no recursive option to add here, unlike the cp command , which needs -R before it will touch a directory tree.

To move a directory somewhere else while keeping its name, give the parent directory as the destination:

Terminal
mv project /opt

This creates /opt/project. A trailing slash on the destination makes no difference, so mv project /opt/ does the same thing.

The switch between moving and renaming is worth planning for in scripts, because the same command does two different things depending on what already exists. If /opt/project is already there, mv project /opt/project nests the source and you end up with /opt/project/project. The -T (no target directory) option removes the guesswork by telling mv to treat the last argument as the final path rather than a container:

Terminal
mv -T project /opt/project

Now mv will not nest. If the destination directory exists and still has files in it, the command stops with an error instead:

output
mv: cannot move 'project' to '/opt/project': Directory not empty

To rename a directory in place, the plain two-argument form is enough. For the other tools that handle bulk directory renames, see How to Rename Directories in Linux .

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.

Moving Directory Contents

Emptying a directory into another one, without moving the directory itself, is a common task, and the obvious command has a trap:

Terminal
mv olddir/* newdir/

The visible files land in newdir, but * never matches names that start with a dot, so anything like .bashrc or .config is quietly left behind. Turn on the dotglob shell option first so patterns include hidden names:

Terminal
shopt -s dotglob
mv olddir/* newdir/
shopt -u dotglob

The last line switches the option back off, so later commands in the same shell session behave the way you expect. If you skip that step and forget the setting is on, an unrelated rm * later in the session will reach further than you intended.

To move everything except one file, turn on extglob and negate the name you want to keep:

Terminal
shopt -s extglob
mv !(keep.txt) /backup/

The !(pattern) form matches every name that does not match the pattern. Separate several names with | to exclude more than one, as in !(keep.txt|notes.md). It follows the same rule as * and skips hidden names, so leave dotglob on when the directory holds dotfiles. Point the destination somewhere outside the current directory, as shown above. If the destination sits inside the directory you are emptying, the pattern matches the destination too and mv refuses with “cannot move … to a subdirectory of itself”.

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 file is left where it is. Otherwise, it moves the file.

Skipping is treated as a failure rather than a quiet no-op. Since coreutils 9.2, mv -n exits with a nonzero status when the destination exists, and since 9.3 it also prints a diagnostic, so a script running under set -e stops there. When an existing destination is an acceptable outcome, use --update=none instead.

Move Only Newer Files (-u)

The -u (update) option moves a file only when the source is newer than the destination, or when the destination does not exist yet:

Terminal
mv -u file1 /tmp

When /tmp/file1 carries the same or a newer modification time, mv skips the file silently and still exits successfully. That makes -u a good fit for repeating a bulk move after adding or editing a few files, since the untouched ones are left alone.

GNU coreutils 9.3 and later also accept the longer --update=UPDATE form for finer control:

  • older - Skip when the destination has the same or a newer timestamp. This is the default and what plain -u does.
  • all - Move every file, which is the normal behavior without -u.
  • none - Skip every existing destination without reporting an error.
  • none-fail - Skip every existing destination and exit with a failure status.

The none value is handy in scripts where an existing file is an acceptable outcome rather than a problem:

Terminal
mv --update=none file1 /tmp
Info
The --update=UPDATE form needs GNU coreutils 9.3 or later, and none-fail needs 9.5 or later. On older systems such as Ubuntu 22.04 (coreutils 8.32) or Debian 12 (9.1), the option is rejected as unrecognized, and Ubuntu 24.04 (9.4) accepts every value except none-fail. Check your version with mv --version.

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.

The --backup=CONTROL form decides how those backups are named. GNU mv accepts these values:

  • none or off - Do not make backups at all.
  • numbered or t - Make numbered backups such as file1.~1~ and file1.~2~.
  • existing or nil - Use numbered backups when numbered ones are already present, and a simple backup otherwise.
  • simple or never - Always make a simple backup with the suffix appended.

Numbered backups are the safer option when the same file is moved into place over and over, because each move keeps the previous version instead of overwriting a single file1~:

Terminal
mv --backup=numbered file1 /tmp

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), the rename cannot work, so mv falls back on copying the data as if by cp -a and then removes the original once the copy succeeds. 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 -u file1 /tmpMove only when the source is newer
mv -b file1 /tmpCreate backup of destination before overwriting
mv --backup=numbered file1 /tmpKeep numbered backups of the destination
mv -v file1 /tmpPrint each file as it is moved
mv -t dir/ file1 file2Specify target directory first
mv -T dir1 /opt/dir1Treat the destination as a path, not a container

FAQ

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

Does mv preserve file permissions and timestamps?
Yes. When moving within the same filesystem, mv only rewrites the directory entry, so nothing about the file changes. When moving across filesystems, the copy is made as if by cp -a, which carries over permissions, timestamps, symbolic links, and ownership wherever the destination allows it.

How do I move hidden files (dotfiles)?
The glob * does not match hidden files by default. Run shopt -s dotglob in Bash before the move so patterns include them, then shopt -u dotglob afterwards. See Moving Directory Contents for the full pattern.

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 add -T when a script must not nest a directory inside an existing one. When the original needs to stay where it is, see How to Copy Files and Directories in Linux .

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