Managing files in Linux: mv command

M

Command mv (short of move) is used both to move files and folders from one place to the other, and also to rename them. Linux does not distinguish between these two types of operations (move and rename), as users do.

The syntax of the mv command is as follows:

mv [option] destination source

The mv command accepts almost the same options as the cp command. However, mv can not be used with –preserve (-p), –recursive (-R), and –archive (-a).

It can get a relative or absolute path for both parameters and as most used options it has:
• -f (force) which automatically overwrites if necessary without requiring confirmation from the user;
• -u (update) moves the destination source only if it is a new one or if it didn’t exist as destination before;
• -v (verbose) lists the output for each operation performed.

Commands that overwrite a file (cp, mv, rm) require confirmation when used by regular users but not when executed by root. To avoid accidental deletion, you can use the -i (interactive) option. The confirmation response can be y (yes) or n (no).

Practical examples of the mv command:

To move files or folders, specify the source files, then a file name for the destination option:

$ ls file.txt
file.txt

$ mv file.txt temp/

$ ls file.txt
ls: cannot access ‘file.txt’: No such file or directory

$ ls temp/
file.txt

The above commands move the file.txt file into the temp directory. As you can see, unlike the cp command, the original file is moved to the new directory, and no other copy remains in the original directory.

If the destination directory is on another partition or disk, Linux must read the original file, rewrite it to the destination, and then delete the original. These operations slow down the mv command pretty much, especially for large files.

Renaming a file with the mv command works almost the same as moving a file, except that for renaming, both the source file and the destination file must be in the same directory. In the example below we renamed the file.txt file into the new.txt file. When displaying the contents of the directory, we only notice the existence of the new.txt file:

$ ls *.txt
file.txt

$ mv file.txt new-file.txt

$ ls *.txt
new-file.txt

You can combine moving and renaming files. The following form of the command moves and simultaneously renames the file file.txt from the current directory into the temp/ subdirectory, with the new-file.txt name:

$ mv file.txt temp/new-file.txt

We can move or rename entire directories using the mv command: just specify a directory as the source parameter. In the example below we renamed the temp directory into temp1, then moved the newly renamed directory temp1 to a temp2 subdirectory:

$ mv temp temp1

$ mv temp1 /temp2/

The two commands above can be combined into one:

$ mv temp /temp2/temp1

Optionally, to prevent typing errors and to indicate clearly that this is a directory, we can add a slash (/) to these directory names.

More options can be found by reading the manual command page: man mv.

About the author

Ilias spiros
By Ilias spiros

Recent Posts

Archives

Categories