Linux Commands

How to Move a File From One Directory to Another in Linux

One of the most common tasks in a Linux system is to move files from one directory to another. It is one of the simplest tasks anyone can perform with the easy “mv” command. We will be learning more about various commands and their options to copy files from one directory to another.

Moving a File Using the “mv” Command

The “mv” here specifies the move that will help you rename or move the files or directories from one specified location to another desired location within the Linux system. The below is the syntax for using the “mv” command.

mv [OPTIONS] SOURCE DESTINATION

In the above “mv” command syntax, the SOURCE specifies single or more files or directories per the user’s requirement. The specified DESTINATION can be a single file or directory accordingly.

  • If you wish to move more than one file or directory specified as a SOURCE, then the specified DESTINATION must be a directory capable of holding all the source files.
  • Suppose you wish to move a file specified as a SOURCE, and the specified DESTINATION is an existing directory. In that case, the source file will get moved to the mentioned destination directory.
  • But in case if you wish to move a single file and have specified the destination also as a single file, then the “mv” command will rename the file.
  • But when you specify the SOURCE as a directory and the mentioned DESTINATION doesn’t exist, the “mv” command will rename the SOURCE to DESTINATION. Otherwise, in case if specified DESTINATION exists, the source will get moved to the DESTINATION directory.

If you want to move a file, you must have write access to both the source and the destination; otherwise, you will get a permission denied error on the screen.

Suppose we move the file “file1” from the current working directory to the /tmp directory; we will use the following command.

mv file1 /tmp

But, if you want to rename the file, mention the destination file name as shown below.

mv file1 file2

If you want to move the directories, it will be the same process as moving the files, as shown above. Here, we use the example where we use dir1 as the source and dir2 as the destination.

Moving Multiple Files and Directories

If you wish to move more than a single file and directories, mention the files as the source. Considering the below example, we are moving file1 and file2 to the specified directory dir1. You can use the following command syntax to do so.

mv file1 file2 dir1

You can also use the “mv” command for pattern matching if you wish to move all the files with the “pdf” extension from the source to the destination directory “~/Documents”, as shown below.

mv *.pdf ~/Documents

Various “mv” Command Options

You can use various “mv” command options that will somehow affect the behavior of the command. The “mv” command is an alias in some Linux distributions to the “mv” command. But in CentOS, the “mv” command is used as an alias of “mv -i”.

Use the type command with the “mv” command to check if your distribution is using the alias for the “mv” command or not.

type mv

If the “mv” command is an alias, then you will get the below output.

mv is aliased to `mv -i'

Prompt Before Overwriting

If you are trying to move a source file to the already existing destination, then the destination will be overwritten by default. If you want the confirmation for the overwriting, use the “-i” option as shown below.

mv -i file1 /tmp

Output-

mv: overwrite '/tmp/file1'?

Press “Y” to confirm if you want to proceed with the overwrite.

Force Overwriting

If you want to overwrite the read-only file using the “mv” command, then you will get a confirmation if you wish to overwrite the files or not, as we have seen in the above example using the “-i” option with the “mv” command.

But if you do not want that confirmation and want to overwrite the desired files, then use the “-f” option along with the “mv” command.

mv -f file1 /tmp

This option is generally useful when you are moving multiple read-only files.

Do Not Overwrite Existing Files.

If you want no file to be overwritten, use the “-n” option along with the “mv” command, as shown below.

mv -n file1 /tmp

If the file1 file already exists in the /tmp folder, there will be the action of this command, but there is no file1 file in the /tmp folder, then it will move the file to the destination folder.

Backing Up Files

If you want to create a backup of the existing destination file, use the “-b” option along with the “mv” command below.

mv -b file1 /tmp

The backup file will be created with the same name as the original file but with the “~” appended. Verify if the backup of the file is created, run the “ls” command.

mv -b file1 /tmp

You will get the below-mentioned output-

/tmp/file1  /tmp/file1~

Verbose Output

If you want to get the progress of your command on the screen, use the “-v” option for the verbose output.

mv -i file1 /tmp

You will get the below output.

renamed 'file1' -> '/tmp/file1'

Conclusion

In Linux, you can do a task using the command-line interface efficiently. Only you need to have a good understanding of the command-line commands. One of the very common tasks is moving the files or directories from the source to the destination. Also, you can use various options with the move command for several features that will help you control your tasks. We hope you have understood the working of the “mv” command and how it behaves with various options.

About the author

Simran Kaur

Simran works as a technical writer. The graduate in MS Computer Science from the well known CS hub, aka Silicon Valley, is also an editor of the website. She enjoys writing about any tech topic, including programming, algorithms, cloud, data science, and AI. Travelling, sketching, and gardening are the hobbies that interest her.