In this tutorial, you will learn how to use the
mv command to move and renames files and directories on Linux.
Files and directories on Linux as very similar, from a filesystem point of view. This means the operations done on one can be also done on the other, with very few exceptions.
As such, you will notice that commands used to perform actions on files are identical with directories.
Rename files on Linux
To rename a file in Linux you use the
mv command. The command accepts two or more arguments. For renaming files, only two arguments are needed, which are the source file and the target file.
mv command will take the source file specified and rename it to the target file.
mv old-filename new-filename
To rename a file named student1 to student10, for example, you would run the following command.
mv student1 student10
Provided the file target is the same directory, all file attributes will remain, including permissions.
Moving a file on Linux
To move a file to another location we use the same method as renaming a file, except the file path should be different.
mv source-file /new/path
For example, to move a file from
/var/labs/student1/lab-work.log, you would run the following command.
mv /home/student1/lab-work.log /var/labs/student1/lab-work.log
Moving and Renaming files on Linux
A file can be renamed during a move process using the
mv command. You simply give the target path a different name. When
mv moves the file, it will be given a new name.
mv old-filename /new/path/new-filename
For example, to move a file named student1.txt to
/var/students and rename it to
class1-student1.txt, you would run the following command.
mv student1.txt /var/students/class1-student1.txt
Moving Multiple files on Linux
mv command accepts multiple source files, which means we can move two or more files at the same time. When executing the
mv command, each file listed will be considered a source with the last path being the exception. The last path will be treated as the target.
mv source-file-1 source-file-2 target-path
For example, to move
/var/students, you would run the following command.
mv student1.txt student2.txt /var/students
Moving Directories on Linux
Moving directories work the same as moving files. We specify the source directory and give a target directory.
mv source-directory target-directory
For example, to move a directory path /tmp/logs to ~/data/logs you would run the following command.
mv /tmp/logs ~/data/logs
Moving Multiple Directories on Linux
As with files, multiple directories can be moved to a new location. We simply specially all of the directories to be moved, and then give a target directory for them to be moved to.
mv /dir/1 /dir/2 /dir/3 /target/path
Verbose Output Flag
mv command will perform its operations silently. No output will be printed to the screen while files or directories are being moved or renamed.
To instruct the
mv command to print out a log of actions being taken, you can use the
-v flag. This flag enabled verbosity, which is helpful for auditing.
mv -v student1.txt student2.txt
Do Not Overwrite Existing Files
To force the
mv command to not overwrite existing files when moving or renaming a file, use the
In the example below, if the
student2.txt file already exists, then the
mv command will not rename the file and it will exit with an error.
mv -n student1.txt student2.txt
Do Not Prompt to Confirm Overwrites
If you want to forcefully move files or directories and overwrite paths that already exist, you can use the
-f flag. This is effective for overwriting old, stale files or directories with new ones with the same name.
mv -f /var/data/logs /tmp/data/logs