How to Move a File or Directory in Linux: The mv Command

How to Move a File or Directory in Linux: The mv Command

9 mins readComment
Updated on Nov 22, 2023 18:48 IST

If you're new to the world of Linux, you might wonder how to move a file or directory from one location to another. Or, if you're already a Linux user, you might be looking for some tips to make the process smoother and more efficient. Moving files and directories is one of Linux's most basic and commonly performed tasks. So, it's important to know how to do it properly. Whether you're moving a single file or an entire directory, you can use a few different methods.

In this article, we'll cover everything you need to know about moving files and directories in Linux. We'll walk you through the different methods and explain the commands you'll need to use. So, whether you're a Linux newbie or a seasoned pro, read on to learn how to move your files and directories like a pro!

We will be covering the following sections:

  • Moving a File or Directory in Linux Using the mv Command
  • Moving Multiple Files or Directories
  • The mv Command Options

Moving a File or Directory in Linux Using the mv Command

The mv, aka the move command is the most common way to move files and directories in Linux. Here’s the syntax:


 
$ mv [SOURCE] [DESTINATION]
Copy code

The SOURCE for moving files or directories in Linux can be either a single file or directory or multiple files or directories. Similarly, the DESTINATION can also be a single file or directory.

Points to remember:

  • The DESTINATION must be a directory when moving multiple files or directories as the SOURCE. This means that all the SOURCE files will be moved to the specified target directory.
  • If a single file is specified as the SOURCE and the DESTINATION is an existing directory, the file will be moved to that specified directory.
  • On the other hand, if a single file is specified as the SOURCE and a single file is specified as the DESTINATION, then the file is simply renamed.
  • If the SOURCE is a directory and the DESTINATION does not exist, the directory will be renamed to the specified DESTINATION. However, if the DESTINATION already exists, then the SOURCE directory will be moved inside the DESTINATION directory.

It's important to note that in order to move a file or directory, you must have write permissions for both the SOURCE and DESTINATION. You will receive a permission denied error if you do not have the necessary permissions.

For example, let’s say you want to copy folder 1 into a Templates directory. Here's how you will do it:

  • Open your terminal window.
  • Type the following command:

 
$ mv folder1 Templates
Copy code
  • Press the Enter key.

Awesome, you successfully moved folder1 into another directory on Linux!

The syntax used for moving directories is identical to that used for moving files. For instance, consider the following example:


 
$ mv directory1 Public
Copy code

If the directory named Public already exists, executing the command will move the directory named directory1 into Public. However, if Public does not exist, the directory named directory1 will be renamed to Public.

Awesome, you successfully moved a directory into another directory on Linux!

 Moving Multiple Files or Directories

To move multiple files or directories in Linux, you need to specify all the SOURCE files or directories and the DESTINATION directory where they will be moved to. It's important to note that when moving multiple files or directories, the DESTINATION must be a directory, not a file.

The syntax for moving multiple files or directories in Linux is as follows:


 
$ mv SOURCE1 SOURCE2 SOURCE3 ... DESTINATION
Copy code

Here, the SOURCE1, SOURCE2, and SOURCE3 represent the files or directories you want to move, and the DESTINATION represents the directory where you want to move them to.

For example, let's say you have three files named file1, file2, and file3, and you want to move them all to a directory named new_dir. You can use the following command to move them all at once:


 
$ mv file1 file2 file3 new_dir
Copy code

 

With the mv command in Linux, you can also employ pattern matching to move files. To illustrate, you can move all .pdf files from the current directory to the ~/Documents directory using the following command:


 
$ mv *.pdf ~/Documents
Copy code

The mv Command Options

The mv command in Linux comes with various options that can make moving files and directories even more powerful and efficient. Here, we will discuss some of the most commonly used options of the mv command.

-i option

This option stands for "interactive". If you use the -i option, the mv command will prompt you before overwriting any existing files with the same name as the file you are moving

Here's an example of how to use -i with the mv command:

Let's say you have a file named example.txt in your current directory, and you want to move it to a new directory called new_folder. However, there is already a file in new_folder with the same name. Now, to move the text file to new_folder while using the -i option, you would run the following command:


 
mv -i example.txt new_folder
Copy code

After running this command, the mv command will prompt you with a message like this:


 
mv: overwrite 'new_folder/example.txt'?
Copy code

If you want to overwrite the existing file in new_folder with the file you are moving, you can type "y" and press Enter. If you do not want to overwrite the existing file, you can type "n" and press Enter.

This -i option gives you an extra layer of protection against accidentally overwriting files, especially when moving multiple files simultaneously.

-f option

This option stands for "force". The -f option allows you to overwrite any existing files with the same name as the file you are moving without being prompted. Use this option with caution, as it can result in data loss if used improperly.

Here's an example of how to use -f with the mv command:

Let's say you have a file named example.txt in your current directory, and you want to move it to a new directory called new_folder. However, there is already a file in new_folder with the same name. Now, to move the text file to new_folder while using the -f option, you would run the following command:


 
mv -f example.txt new_folder
Copy code

After running this command, the mv command will overwrite the existing file in new_folder with the file you are moving, without prompting you to confirm.

It is important to be careful when using the -f option, especially when moving multiple files simultaneously, as it can result in data loss if you accidentally overwrite the wrong files.

-v option

This option stands for "verbose". If you use the -v option, the mv command will display a message for each moved file or directory, providing more detailed information about the process.

Here's an example of how to use -v with the mv command:

Let's say you have a file named example.txt in your current directory, and you want to move it to a new directory called new_folder. To move the file and see a verbose output of the process, you would run the following command:


 
mv -v example.txt new_folder
Copy code

After running this command, the mv command will move "example.txt" to "my_folder" and display a message as shown below:

The message indicates that the file example.txt has been moved to the directory new_folder and shows the source and target files' full path.

The -v option can be especially useful when moving multiple files or directories, as it allows you to see a clear output of the process and confirm that everything has been moved correctly.

-n option

This option stands for "no clobber". The -n option prevents the mv command from overwriting any existing files with the same name as the file you are moving. If a file with the same name already exists in the destination directory, the mv command will simply skip that file.

Here's an example of how to use -n with the mv command:

Let's say you have a file named example.txt in your current directory, and you want to move it to a new directory called new_folder. However, there is already a file in new_folder with the same name. Now, to move the text file to new_folder while using the -n option, you would run the following command:


 
mv -n example.txt new_folder
Copy code

 

After running this command, the mv command will not overwrite the existing file in new_folder with the file you are moving. Instead, it will keep the existing file and not move the source file.

If you want to move example.txt and keep the existing file in new_folder, you can rename the source file using the -n option like this:


 
mv -n example.txt new_folder/example-new.txt
Copy code

After running this command, the mv command will move example.txt to the new_folder with a new name, example-new.txt, without overwriting the existing file in the new_folder.

The -n option can be useful when you want to avoid accidentally overwriting files, especially when moving multiple files at once.

-u option

This option stands for "update". If you use the -u option, the mv command will only move files newer than those in the destination directory. This can be useful when you want to update files in one directory with newer versions from another directory.

Here's an example of how to use -u with the mv command:

Let's say you have a file named example.txt in your current directory, and you have made some changes to it. Now, you want to move it to a new directory called new_folder, but only if the file in new_folder is older than the one you have modified. To move the text file to new_folder while using the -u option, you would run the following command:


 
mv -u example.txt new_folder
Copy code

After running this command, the mv command will check the file's modification time in new_folder with the same name as example.txt. If the file in new_folder is older than the one you have modified, the mv command will move the file to new_folder. If the file in new_folder is newer or has the same modification time as the source file, the mv command will not move the file.

The -u option can be useful when you want to move files only if they have been modified or if the destination file is older than the source file. It can also be useful when you are moving multiple files at once and want to avoid overwriting newer files with older ones.

Endnotes

The mv command is a powerful tool that can be used to move files and directories in Linux. Whether you need to rename a file, move a file to a new location, or move multiple files simultaneously, the mv command has options and arguments to make the process efficient and effective. You can control how the mv command behaves and avoid accidentally overwriting or deleting files by using options like -i, -f, -v, -n, and -u.

I hope this article was useful for you. To explore more such articles, you can check out our page.

Contributed By: Prerna Singh

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio