In UNIX-based operating systems like Linux and macOS, `mv` stands for “move”. But in this article, we will be talking about the “mv command in Linux”. As its name suggests this command is used to rename file directories and move files from one location to another within a file system.
Two Distinct Functions of `mv` Command
- 1) Renaming a file or directory.
- 2) Moving a file or directory to another location
Syntax for the `mv` command in Linux
Syntax: mv [options(s)] [source_file_name(s)] [Destination_file_name]
Here,
- source_file_name(s) = The name of the files that we want to rename or move.
- Destination_file_name = The name of the new location or the name of the file.
Examples of `mv` Command
1) Renaming a file in Linux
Syntax:
mv [source_file_name(s)] [Destination_file_name]
Enter your source file name in place of [source_file_name(s)] and your destination file name in place of [Destination_file_name].
For Example:
If we have a file “name = jayesh_gfg” and want to rename it to “name = geeksforgeeks”.
mv jayesh_gfg geeksforgeeks

mv jayesh_gfg geeksforgeeks
Here we used the `ls` command to see the files and directories in the following location.
This command is renamed `jayesh_gfg` to `geeksforgeeks`. If `geeksforgeeks` already exists, in that case, it will be overwritten without prompting for confirmation.
2) Moving a file in Linux
Syntax:
mv [source_file_name(s)] [Destination_path]
Enter your source file name in place of [source_file_name(s)] and your destination path in place of [Destination_path].
For Example:
If we have a file “name = geeksforgeeks” and want to move it to location “name = /home/jayeshkumar/jkj”.
mv geeksforgeeks /home/jayeshkumar/jkj/

mv geeksforgeeks /home/jayeshkumar/jkj/
Here we used the `ls` command to see the files and directories in the following location.
This command moved file “name = `geeksforgeeks`” to the destination “name = “/home.jayeshkumar/jkj/”.
3) Moving Multiple files in Linux
Syntax:
mv [source_file_name_1] [source_file_name_2] [source_file_name_ .....] [Destination_path]
Enter your source file names in place of [source_file_name_1…..] and your destination path in place of [Destination_path].
For Example:
If we have a file “name = gfg_1 , gfg_2” and want to move it to location “name = /home/jayeshkumar/jkj”.
mv gfg_1 gfg_2 /home/jayeshkumar/jkj/

mv gfg_1 gfg_2 /home/jayeshkumar/jkj/
Here we used the `ls` command to see the files and directories in the following location.
This command moved file “name = `gfg_1 and gfg_2`” to the destination “name = “/home.jayeshkumar/jkj/”.
4) Renaming a directory in Linux
Syntax:
mv [source_directory_name(s)] [Destination_directory_name]
Enter your source directory name in place of [source_directory_name(s)] and your destination directory name in place of [Destination_directory_name].
For Example:
If we have a directory “name = jkj” and want to rename it to “name = new_gfg”.
mv jkj new_gfg

mv jkj new_gfg
Here we used `ls` command to see the files and directories in the following location.
This command renames `jkj` to `new_gfg`. If `new_gfg` already exists, in that case it will be overwritten without prompting for confirmation.
Options available in `mv` Command
1) `-i` (interactive)
The “-i” option makes the “mv” command ask for confirmation before overwriting an existing file. If the file doesn’t exist, it will simply rename or move it without prompting.
Syntax:
mv -i [source_file/directory_name(s)] [Destination_file/directory_name/path]
Enter your source file/directory name in place of [source_file/directory_name(s)] and your destination file/directory name/path in place of [Destination_file/directory_name/path].
For Example:
If we have a file “name = jayesh_gfg” and want to rename it to existing file “name = geeksforgeeks”.
mv -i jayesh_gfg geeksforgeeks

mv -i jayesh_gfg geeksforgeeks
Here we used `ls` command to see the files and directories in the following location.
Here we have to give permission by typing yes = `y` and no =`n`.
2) -f (Force)
mv prompts for confirmation overwriting the destination file if a file is write-protected. The -f option overrides this minor protection and overwrites the destination file forcefully and deletes the source file.
Syntax:
mv -f [source_file/directory_name(s)] [Destination_file/directory_name/path]
Enter your source file/directory name in place of [source_file/directory_name(s)] and your destination file/directory name/path in place of [Destination_file/directory_name/path].
For Example:
If we have a file “name = gfg” and want to rename it to existing file “name = geeksforgeeks”.
mv -f gfg geeksforgeeks

mv -f gfg geeksforgeeks
Here we used `ls` command to see the files and directories in the following location.
3) -n (no-clobber)
With -n option, mv prevents an existing file from being overwritten.
Syntax:
mv -n [source_file/directory_name(s)] [Destination_file/directory_name/path]
Enter your source file/directory name in place of [source_file/directory_name(s)] and your destination file/directory name/path in place of [Destination_file/directory_name/path].
For Example:
If we have a file “name = oldfile” and want to rename it to existing file “name = newfile”.
mv -n oldfile newfile

mv -n oldfile newfile
Here we used `ls` command to see the files and directories in the following location.
4. -b(backup)
With this option, it is easier to take a backup of an existing file that will be overwritten as a result of the mv command. This will create a backup file with the tilde character (~) appended to it.
Syntax:
mv -b [source_file/directory_name(s)] [Destination_file/directory_name/path]
Enter your source file/directory name in place of [source_file/directory_name(s)] and your destination file/directory name/path in place of [Destination_file/directory_name/path].
For Example:
If we have a file “name = first_file” and want to rename it to existing file “name = second_file”.
mv -b first_file second_file

mv -b first_file second_file
Here we used `ls` command to see the files and directories in the following location.
5. –version
This option is used to display the version of mv which is currently running on your system.
Syntax:
mv --version

mv –version
Conclusion
The `mv` command is a useful tool for managing directories and files in Linux-based Operating Systems. We have discussed two distinct functions of `mv` command they are: renaming or moving files or directories from one location to another. Overall, we can say that `mv` command is an essential tool for managing files and directories efficiently in Linux.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
02 May, 2023
Like Article
Save Article