mv Command in Linux with Examples


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

mv – Moves files.


The mv command moves files and directories from one directory to another or renames a file or directory. If you move a file or directory to a new directory, it retains the base file name. When you move a file, all links to other files remain intact, except when you move it to a different file system. When you move a directory into an existing directory, the directory and its contents are added under the existing directory.

When you use the mv command to rename a file or directory, the TargetDirectory parameter can specify either a new file name or a new directory path name.

You can use the mv command to move files within the same file system or between file systems. Whether you are working in one file system or across file systems, the mv command copies the file to the target and deletes the original file. The mv command preserves in the new file the time of the most recent data modification, the time of the most recent access, the user ID, the group ID, the file mode, the extended attributes, and ACLs of the original file. For symbolic links, the mv command preserves only the owner and group of the link itself.

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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!


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

mv [ -i | -f ] SourceFile/SourceDirectory TargetDirectory


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.
Prompts before overwriting another file.



1. To rename a file, enter:

$ 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.

2. To move a directory, enter:

$ mv book manual

This command moves all files and directories under book to the directory named manual, if manual exists. Otherwise, the directory book is renamed manual.

3. To move a file to another directory and give it a new name, enter:

$ mv intro manual/chap1

This command moves intro to manual/chap1. The name intro is removed from the current directory, and the same file appears as chap1 in the directory manual.

4. To move a file to another directory, keeping the same name, enter:

$ mv chap3 manual

This command moves chap3 to manual/chap3

5. To move several files into another directory, enter:

$ mv chap4 abc/chap5 /home/manual

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

6. 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 . (period), retaining the names they had in manual. This move also empties manual. You must type a space between the asterisk and the period.

7. Overwrite existing file

$ mv -f file /dest


$ mv -i /etc/passwd /backup

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 & technical discussions at Telegram SanfoundryClasses.