mv Command in Linux with Examples

This tutorial explains the Linux “mv” command, its options, and usage with examples.

What is mv Command?

mv stands for Move and Rename. The ‘mv’ command is used for moving files and directories to different locations or renaming them. When you move a file or directory to a new location, it keeps its original name. Links to other files stay intact when you move a file, except when it’s moved to a different file system. If you move a directory into an existing one, its contents are placed within the existing directory.

For renaming, you can specify either a new file name or a new directory path in the TargetDirectory parameter. You can use mv to move files within the same file system or between different ones. In both cases, the original file is copied to the target and then deleted. Various file attributes, including modification and access times, user ID, group ID, file mode, extended attributes, and ACLs, are preserved in the new file. For symbolic links, only the owner and group of the link itself are preserved.

Note: The mv command can overwrite many existing files unless you specify the -i flag. The -i flag prompts you to confirm before it overwrites a file. If both the -f and -i flags are specified in combination, the last flag specified takes precedence.



To Move and Rename a File

mv  [ -i | -f ]  SourceFile ... TargetFile

To Move and Rename a Directory

mv  [ -i | -f ]  SourceDirectory ... TargetDirectory

To Move Files or Directories to a Directory Maintaining Original File Names

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
mv  [ -i | -f ]  SourceFile/SourceDirectory TargetDirectory


  • -f: mv will move the file(s) without prompting even if it is writing over an existing target. Note that this is the default if the standard input is not a terminal.
  • -i: Prompts before overwriting another file.

mv Command Examples

Example 1: Rename a File

To rename a file, use the following command:

$ mv appendix apndx.a

This command renames appendix to apndx.a. If a file named apndx.a already exists, its old contents are replaced with those of appendix.
This command renames the file “appendix” to “apndx.a”. If a file named “apndx.a” already exists, its previous contents will be replaced with those of “appendix”.

Example 2: Rename a Directory with ‘mv’

Just like renaming a file, you can rename a directory using mv command as shown below. This also keeps the inode number of the directory same after renaming.

sanfoundry-> mkdir sam
sanfoundry-> ls -ldi sam
612573 drwxrwxr-x 2 himanshu himanshu 4096 Jul 16 18:59 sam
sanfoundry-> mv sam sam1 
sanfoundry-> ls -ldi sam1
612573 drwxrwxr-x 2 himanshu himanshu 4096 Jul 16 18:59 sam1

Here as we can see that the innode number, 612573 is remained the same. We here used “-d” option along with ls since we want info about the directories.


Example 3: Move a Directory

To move a directory, execute the following command:

$ mv book manual

This command relocates all files and subdirectories within “book” to a directory named “manual.” If “manual” already exists, the contents of “book” are added to it. If “manual” doesn’t exist, “book” is effectively renamed as “manual”.

Example 4: Move a File to Another Directory with a New Name

To move a file to another directory while giving it a new name, use this command:

$ mv intro manual/chap1

This action moves the file “intro” to the directory “manual/chap1”. The name “intro” is removed from the current directory, and the same file appears as “chap1” within the “manual” directory.

Example 5: Move a File to Another Directory with the Same Name

To move a file to another directory while keeping the same name, use this command:

$ mv chap3 manual

This command moves the file chap3 to manual directory while preserving its original name, so it will be located at “manual/chap3”.

Example 6: Move Several Files to Another Directory

You can move multiple files into another directory with this command:

$ mv chap4 abc/chap5 /home/manual

This command moves the “chap4” file to “/home/manual/chap4” and the “abc/chap5” file to “/home/manual/chap5”.

Example 7: Using Pattern-Matching Characters

To use the ‘mv’ command with pattern-matching characters, enter:

$ mv manual/* .

This command moves all files in the “manual” directory into the current directory (represented by “.”), retaining their original names. This action also empties the “manual” directory. Be sure to include a space between the asterisk and the period.

Example 8: Overwriting Existing Files
If you need to overwrite existing files, you can use the ‘mv’ command with the ‘-f’ flag for force, or the ‘-i’ flag for interactive confirmation.

$ mv -f file /dest


$ mv -i /etc/passwd /backup

The ‘-f’ flag allows you to forcefully move a file, while the ‘-i’ flag prompts you for confirmation before overwriting an existing file.

Example 9: Taking a Backup Before Overwriting with ‘mv’
To avoid overwriting, rename the file before moving it using ‘mv’ with the ‘–suffix=extension’ option.

$ mv --suffix=.backup sample1.txt sample2.txt

This command renames “sample1.txt” as “sample2.txt.backup” and moves it. Now you have both “sample2.txt” and “sample2.txt.backup”. By adding the “–suffix=.backup” option, you ensure that the original file is renamed and kept as a backup with the specified extension.

Example 10: Viewing Errors with ‘mv’
When using the ‘mv’ command, it may move files without prompting, even if it’s writing over an existing target. This is the default behavior when the standard input is not a terminal. Here are examples of error messages:

(i) Renaming “1.txt” to “../sam/”:

$ mv -f 1.txt ../sam/

Error: “mv: ‘1.txt’ and ‘../sam/1.txt’ are the same file”

(ii) Moving “11.txt” to “../sam/”:

$ mv -f 11.txt ../sam/

Error: “mv: cannot stat ’11.txt’: No such file or directory”

These errors provide feedback on ‘mv’ actions and potential issues.

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.