The Wayback Machine - https://web.archive.org/web/20240828110734/https://www.geeksforgeeks.org/mv-command-linux-examples/
Open In App

How to Move File in Linux | mv Command

Last Updated : 01 Apr, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The `mv` command in Linux is like a superhero tool that can do a bunch of cool stuff with your files and folders. Think of it as a digital moving truck that helps you shift things around in your computer. Whether you want to tidy up your folders, give your files new names, or send them to different places, `mv` is the go-to friend for the job. It’s a basic tool that every person using Linux should know about. In this article, we’ll take a closer look at how you can use the mv command for different tasks, and I’ll show you step-by-step examples to make it super easy to understand. So, buckle up, and let’s explore the many tricks the mv command has up its sleeve!

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. 

Syntax of mv command in Linux

The Basic Syntax for mv command in linux is mentioned below.

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. How to Rename a file in Linux Using mv Command

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

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. How to Move a File in Linux Using mv Command

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/

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 Command

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/

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

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

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

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

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

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

mv –version

How to Move File in Linux | mv Command – FAQs

How do I move a file from one folder to another using the mv command in Linux?

To move a file from one folder to another, you can use the mv command followed by the source file name and the destination folder. For example:

mv filename /path/to/destination/

Can I use the mv command to rename a file in Linux?

Absolutely! The mv command is not only for moving files but also for renaming them. Just provide the current filename and the desired new name as the destination:

mv old_filename new_filename

How can I move an entire directory in Linux with the mv command?

Moving a directory is as simple as moving a file. Use the mv command with the source directory name and the destination directory:

mv directory_name /path/to/destination/

What should I do if a file with the same name already exists in the destination folder when using mv?

If a file with the same name is present in the destination folder, mv will prompt you for confirmation before overwriting. To bypass the confirmation and overwrite without asking, you can use the -f (force) option:

mv -f source destination

Can I move multiple files at once using the mv command in Linux?

Absolutely! You can move multiple files in a single command by providing all the file names followed by the destination folder:

mv file1 file2 file3 /path/to/destination/

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.



Previous Article
Next Article

Similar Reads

Shortcut to Move to the Beginning or Ending of a Command in Linux Terminal
In this article, we will learn how to move to the start and end of a command. We will demonstrate the key combinations in a Git terminal. The same key combinations work in other Unix style terminals too. Moving to the Starting of the Command Ctrl + A : The 'Ctrl+A' key combination moves the pointer to the start of the command. In the below gif imag
1 min read
Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a command-line tool for Linux and OSX, which is similar to the cat command in Linux. But the difference between cat and ccat is that the ccat shows the content of the file with the syntax highlighted. Currently, ccat supports the following programming languages. JavaScriptJavaRubyPythonGoCJSONInstallation of Ccat First, we are going to see
2 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
4 min read
file command in Linux with examples
file command is used to determine the type of a file. .file type may be of human-readable(e.g. ‘ASCII text’) or MIME type(e.g. ‘text/plain; charset=us-ascii’). This command tests each argument in an attempt to categorize it. It has three sets of tests as follows: filesystem test: This test is based on the result which returns from a stat system cal
3 min read
How to Save Output of Command in a File in Linux?
When we run a command on a terminal on Linux it generates some output of that command. Sometimes we need the output result of the commands. Today we are going to see how to save the output of the command. Using Redirections in Linux: We can use the redirections operators to save the output of commands into files. Redirection operators redirect the
3 min read
How to Create a Text File Using the Command Line in Linux
There are a couple of quick ways to create a text file from the Linux Command Line or Terminal. Some of them have been explained in the following article. 1) touch commandThis is the most standard command to quickly create an empty text file. The command is quite simple to type and makes it quite easier to create several text files at once. The com
6 min read
Extract Emails From a Text File Using Grep Command in Linux
Let's consider we have a text file that contains lots of text and in that text file there are some email IDs present, and we have to find the all email IDs present in that text file. So what we can do? How can we find all email IDs present in that text file?. One way to find all email IDs manually, but this is a very time-consuming and boring proce
3 min read
How to compress file in Linux | Compress Command
Linux, renowned for its powerful command-line utilities, provides users with various tools to streamline file management tasks. One such tool, the compress `command`, is specifically designed to compress individual files, reducing their size for efficient storage and transfer. In this comprehensive guide, we'll explore the ins and outs of the compr
5 min read
How to Mount File System in Linux | mount Command
All files in a Linux filesystem are arranged in form of a big tree rooted at '/'.These files can be spread out on various devices based on your partition table, initially your parent directory is mounted(i.e attached) to this tree at '/', others can be mounted manually using GUI interface(if available) or using mount command. mount command is used
6 min read
Use the Lines of a File as Arguments of a Linux Command
Handling multiple arguments for a command can be difficult, especially when there are a lot of parameters. One way to make this easier is to put the arguments in a text file and use each line of the file as an argument for the command. This can simplify the process and save time. For example, let's say you have a text file with a list of 100 client
6 min read
Unmount File System in Linux Using the umount Command
Unmounting a file system is a vital procedure for securely deleting storage devices or rendering system resources accessible. A vital instrument in this procedure is the "umount" command, which enables users to softly unmount mounted devices or partitions. Before beginning the unmounting process, this step ensures data integrity and protects any po
7 min read
How to Display Path of an Executable File in Linux | Which Command
In Linux finding the exact path of an excutable file can be crucial for the system adminstration, scripting and as well for troubleshooting. The `which` command helps with providing a simple and effective way to locate the executable files within the directories that are listed in your system. In this article, we are going to discuss on what is whi
6 min read
How to Find a File in Linux | Find Command
Linux, renowned for its robust command-line interface, provides a suite of powerful tools for efficient file and directory management. Among these, the "find" command stands out as an indispensable asset, offering unparalleled versatility in searching for files based on diverse criteria. This article explores the prowess of the find command, sheddi
9 min read
How to Change File Ownership in Linux | chown Command
In the Linux operating system, file ownership is a crucial aspect of system security and user management. The `chown` command, short for "change owner," is a powerful tool that allows users to change the owner of files and directories. This command is particularly useful in scenarios where administrators need to grant or revoke access to specific r
9 min read
How to Create an Empty File in Linux | Touch Command
The touch command is a standard command used in the UNIX/Linux operating system which is used to create, change and modify the timestamps of a file. Basically, there are two different commands to create a file in the Linux system which are as follows: touch command: It is used to create a file without any content. The file created using the touch c
8 min read
How to View the Content of File in Linux | cat Command
The cat command in Linux is more than just a simple tool; it's a versatile companion for various file-related operations, allowing users to view, concatenate, create, copy, merge, and manipulate file contents. Let's delve into the details of some frequently used cat commands, understanding each example along the way. Table of Content Basic Syntax o
7 min read
How to Rename File in Linux | rename Command
Changing the names of files in Linux is something we often do, and the "rename" command is like a helpful friend for this job. This guide is like a journey to becoming really good at renaming files on Linux, showing you how handy and useful the "rename" command can be. Whether you're just starting out or you're already good at working with files, t
8 min read
Check If File Exist Inside Tar File Using Tar And Grep Command
Linux users are familiar with the popular command-line tool Tar (tape archive), which is used to archive directories and files into a single file called a "tarball." Occasionally, you might need to verify whether a certain file is present in a tar archive without extracting the full archive. This detailed article will show you how to effectively se
4 min read
How to Fix - Cp: Cannot Create Regular File 'File': File Exists
In Linux, while performing copying operations using the cp command, the error "Cp: Cannot Create Regular File 'File': File Exists" may occur when attempting to copy a file to a destination where a file with the same name already exists. This error is a safeguard to prevent accidental overwriting of files and potential data loss. To resolve this iss
3 min read
Move and Copy different file types in different folders using Shell Scripts
Shell scripts are powerful tools that allow you to automate repetitive tasks and perform complex operations on your file system. One common use case for shell scripts is moving and copying different file types to different folders. In this article, we will explore different examples of shell scripts that can be used to move and copy different file
4 min read
select command in Linux with examples
select command in Linux is used to create a numbered menu from which a user can select an option. If the user enters a valid option then it executes the set of command written in select block and then ask again to enter a number, if a wrong option is entered it does nothing. If user enters nothing and simply press 'enter' the option menu gets print
1 min read
atrm command in Linux with examples
atrm command is used to remove the specified jobs. To remove a job, its job number is passed in the command. A user can only delete jobs that belong to him. Only superuser can delete any job even if that belongs to another user. Syntax: atrm [-V] job [job...] Options: -V : Used to print the version number atrm -V job : Job number of the job which i
1 min read
comm command in Linux with examples
comm compare two sorted files line by line and write to standard output; the lines that are common and the lines that are unique. Suppose you have two lists of people and you are asked to find out the names available in one and not in the other, or even those common to both. comm is the command that will help you to achieve this. It requires two so
5 min read
cmp Command in Linux with examples
cmp command in Linux/UNIX is used to compare the two files byte by byte and helps you to find out whether the two files are identical or not. When cmp is used for comparison between two files, it reports the location of the first mismatch to the screen if difference is found and if no difference is found i.e the files compared are identical.cmp dis
5 min read
join Command in Linux
The join command in UNIX is a command line utility for joining lines of two files on a common field. Suppose you have two files and there is a need to combine these two files in a way that the output makes even more sense.For example, there could be a file containing names and the other containing ID's and the requirement is to combine both files i
7 min read
expand Command in LINUX with examples
Whenever you work with files in LINUX there can be a situation when you are stuck with a file containing many tabs and whatever you need to do with a file requires that file with no tabs but with spaces. In this situation, the task looks quite simple if you are dealing with a small file but what if the file you are dealing with is very big or you n
3 min read
df Command in Linux with examples
There might come a situation while using Linux when you want to know the amount of space consumed by a particular file system on your LINUX system or how much space is available on a particular file system. LINUX being command friendly provides a command line utility for this i.e df command that displays the amount of disk space available on the fi
8 min read
help Command in Linux with examples
If you are new to LINUX operating system and having trouble dealing with the command-line utilities provided by LINUX then you really need to know first of all about the help command which as its name says help you to learn about any built-in command. help command as told before just displays information about shell built-in commands. Here's the sy
3 min read
fgrep command in Linux with examples
The fgrep filter is used to search for the fixed-character strings in a file. There can be multiple files also to be searched. This command is useful when you need to search for strings which contain lots of regular expression metacharacters, such as "^", "$", etc. Syntax: fgrep [options] [ -e pattern_list] [pattern] [file] Options with Description
3 min read
od command in Linux with example
od command in Linux is used to convert the content of input in different formats with octal format as the default format.This command is especially useful when debugging Linux scripts for unwanted changes or characters. If more than one file is specified, od command concatenates them in the listed order to form the input.It can display output in a
5 min read