In UNIX-based operating systems like Linux and macOS, `mv` stands for "move". The mv
command is a UNIX command for renaming and moving files and directories within a filesystem. Although desktop operating systems have graphical user interfaces for file operations, using mv
the terminal usually offers a better approach. The uses of the mv command are:
The Basic Syntax for the 'mv' command in Linux is mentioned below.
mv [options(s)] [source_file_name(s)] [Destination_file_name]
Use the mv command to rename a file while keeping it in the same directory.
Syntax:
mv [Old_file_name(s)] [New_file_name]
Enter your Old file name in place of [Old_file_name(s)] and your New file name in place of [New_file_name].
For Example:
If we have a file "name = jayesh_gfg" and want to rename it to "name = geeksforgeeks".
mv jayesh_gfg geeksforgeeksmv 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. How to Move a File in Linux Using mv CommandThe 'mv'
Command in Linux is used to move files or directories from the source_file_name(s) to the specified Destination_path by modifying their filesystem location without duplicating data.
Syntax:
mv [source_file_name(s)] [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. How to Move Multiple files in Linux Using mv CommandThe mv Command
allows moving multiple files like source_file_name_1, source_file_name_2... simultaneously to a specified Destination_path in the Linux filesystem without creating duplicates.
Syntax:
mv [source_file_name_1] [source_file_name_2] [source_file_name_ .....] [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. How to Rename a directory in Linux Using mv Command in LinuxThe mv Command
in Linux renames a directory by changing its Old_directory_name(s) to the specified New_directory_name, and if the file name exists, it gets overwritten without confirmation.
Syntax:
mv [Old_directory_name(s)] [New_directory_name]
For Example:
If we have a directory "name = jkj" and want to rename it to "name = new_gfg".
mv jkj new_gfgmv 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 CommandThe mv Command
provides multiple options like -i
(interactive confirmation), -f
(force overwrite), -n
(no-clobber to prevent overwriting), -b
(backup with ~ suffix), and --version
to display the current version, allowing fine control over file and directory move operations.
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 geeksforgeeksmv -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 geeksforgeeksmv -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 newfilemv -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_filemv -b first_file second_file
Here we used `ls` command to see the files and directories in the following location.
5. --versionThis option is used to display the version of mv which is currently running on your system.
Syntax:
mv --versionmv --version Conclusion
The mv command is a useful tool for managing directories and files in Linux 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.
mv-Renaming and moving files in Linux
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4