mv

file managementLinux/Unix
The mv command is one of the most frequently used commands in Linux/Unix-like operating systems. mv The mv command is used to move or rename files and directories in Linux and Unix-like operating systems. It can move files between directories and rename them in a single operation while preserving file attributes.

Quick Reference

Command Name:

mv

Category:

file management

Platform:

Linux/Unix

Basic Usage:

mv old_name.txt new_name.txt

Common Use Cases

  • 1

    File renaming

    Change filenames quickly while preserving all file attributes

  • 2

    File organization

    Relocate files to more appropriate directories

  • 3

    Batch processing

    Move processed files to archive or completed directories

  • 4

    Directory restructuring

    Reorganize directory hierarchies without copying data

Syntax

mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE...

Options

Option Description
--backup[=CONTROL] Make a backup of each existing destination file
-b Like --backup but does not accept an argument
-f, --force Do not prompt before overwriting
-i, --interactive Prompt before overwrite
-n, --no-clobber Do not overwrite an existing file
--strip-trailing-slashes Remove trailing slashes from each SOURCE argument
-S, --suffix=SUFFIX Override the usual backup suffix
-t, --target-directory=DIRECTORY Move all SOURCE arguments into DIRECTORY
-T, --no-target-directory Treat DEST as a normal file
-u, --update Move only when the SOURCE file is newer than the destination file or when the destination file is missing
-v, --verbose Explain what is being done
-Z, --context Set SELinux security context of destination file to default type
--help Display help information and exit
--version Output version information and exit

Examples

How to Use These Examples

The examples below show common ways to use the mv command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

#

Basic Examples:

mv file.txt newname.txt

Rename file.txt to newname.txt in the current directory.

mv file.txt /path/to/directory/

Move file.txt to another directory.

mv file1.txt file2.txt destination/

Move multiple files to a directory.

Advanced Examples:

mv -i important.txt /backup/

Interactive mode: prompt before overwriting an existing file.

mv -n file.txt /data/

No-clobber: do not overwrite an existing file.

mv -v *.txt /archive/

Verbose mode: explain what is being done.

mv --backup=numbered file.txt /shared/file.txt

Create numbered backups of existing destination files.

mv -f old.txt /data/new.txt

Force: overwrite destination file without prompting.

mv -u source.txt /data/

Update: move only when the source file is newer than the destination.

mv dir1/ dir2/

Rename/move a directory (if dir2 doesn't exist, dir1 is renamed to dir2).

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

Key Points:

  • The mv command moves files and directories from one location to another
  • It can also be used to rename files and directories in the same location
  • Unlike cp, mv doesn't create a copy—it relocates the original file
  • By default, mv overwrites destination files without warning
  • To safely move files, use the -i option for interactive prompts
  • When moving directories, no special options are required (unlike cp which needs -r)
  • Moving files within the same filesystem is very fast as only the directory entries change, not the actual data

Common Use Cases:

  • Renaming files and directories
  • Organizing files into appropriate directories
  • Moving processed files to archive locations
  • Moving configuration files to their proper locations
  • Relocating user data during system migrations
  • Reorganizing directory structures

Differences Between Moving and Copying:

  • Moving preserves the original inode when staying on the same filesystem
  • Moving is typically faster than copying as it doesn't duplicate data
  • Moving removes the file from its original location
  • When moving across filesystems, mv actually copies the file and then deletes the original
  • Moving a file maintains the same file permissions, ownership, and timestamp

Safety Considerations:

  • Use -i (interactive) mode when working with important files to prevent accidental overwrites
  • Use -n (no-clobber) to ensure existing files aren't overwritten
  • The --backup options can create backups of files that would be overwritten
  • Be cautious when using wildcards with mv in scripts
  • Moving directories containing special files like sockets or named pipes may have unintended consequences
  • Remember that moving deletes the source file, unlike cp which preserves it

Related Commands:

  • cp - Copy files and directories
  • rm - Remove files and directories
  • rsync - Remote file synchronization with more features
  • rename - Rename files based on pattern matching
  • mmv - Move and rename files using patterns

Tips & Tricks

1

Use mv -i for interactive mode to prevent accidental overwrites: mv -i important.txt /backup/

2

Move multiple files with wildcards: mv *.txt /archive/text/

3

Rename files with pattern substitution: for f in *.JPG; do mv "$f" "${f%.JPG}.jpg"; done

4

Create a backup of files before moving: mv --backup=numbered data.txt /shared/

5

Use -n to avoid overwriting existing files: mv -n logs/*.log /backups/

6

Add verbose output for clarity: mv -v processed_file.txt /archive/

7

Move only newer files: mv -u *.log /logs/archive/

8

Use -f to force moves without prompting: mv -f temp/* /tmp/backup/ (use with caution!)

Common Use Cases

File renaming

Change filenames quickly while preserving all file attributes

File organization

Relocate files to more appropriate directories

Batch processing

Move processed files to archive or completed directories

Directory restructuring

Reorganize directory hierarchies without copying data

System configuration

Place configuration files in their proper locations

Related Commands

cp

cp

View command

rm

rm

View command

rename

rename

View command

rsync

rsync

View command

mmv

mmv

View command

install

install

View command

ln

ln

View command

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the mv command works in different scenarios.

$ mv
View All Commands