How to mv in linux

Content on WhatAnswers is provided "as is" for informational purposes. While we strive for accuracy, we make no guarantees. Content is AI-assisted and should not be used as professional advice.

Last updated: April 4, 2026

Quick Answer: The `mv` command in Linux is used to move or rename files and directories. To move a file, you specify the source file and the destination directory. To rename a file, you specify the original name and the new name.

Key Facts

What is the `mv` command in Linux?

The `mv` command, short for 'move', is a fundamental utility in Linux and other Unix-like operating systems. Its primary purpose is to move files and directories from one location to another within the file system. However, it also serves a second crucial function: renaming files and directories. When you use `mv` to rename an item, you're essentially moving it within the same directory, but with a new name.

How to Move Files and Directories

The most common use of `mv` is to move one or more files or directories to a different directory. The basic syntax for moving is:

mv [options] source destination

Moving a Single File

To move a file named `report.txt` from your current directory to a directory called `documents`, you would use:

mv report.txt documents/

If the `documents` directory exists, `report.txt` will be moved into it. If `documents` does not exist, and you are moving a single item, `mv` will interpret `documents` as the new name for `report.txt` and rename it, which is usually not the intended behavior. Always ensure the destination directory exists before moving files into it.

Moving Multiple Files

You can move multiple files at once by listing them before the destination directory:

mv file1.txt file2.jpg image.png documents/

This command moves `file1.txt`, `file2.jpg`, and `image.png` into the `documents` directory.

Moving a Directory

Moving a directory works exactly the same way as moving a file:

mv my_folder/ backup/

This command moves the directory `my_folder` and all its contents into the `backup` directory.

How to Rename Files and Directories

Renaming is a special case of moving where the source and destination are in the same directory, but the destination specifies a new name.

Renaming a File

To rename a file named `old_name.txt` to `new_name.txt` in the current directory:

mv old_name.txt new_name.txt

Here, `old_name.txt` is the source, and `new_name.txt` is the destination, which is simply the new name for the file.

Renaming a Directory

Similarly, to rename a directory:

mv old_directory_name new_directory_name

Useful Options for `mv`

The `mv` command comes with several options that can modify its behavior:

Important Considerations

Permissions: You need write permissions in the destination directory to move files into it. You also need write permissions on the parent directory of the source file/directory to remove it from its original location.

Overwriting: By default, if the destination file or directory already exists, `mv` will overwrite it without asking. This is a critical point to remember. Always double-check your destination, or use the `-i` option to prevent accidental data loss.

Symbolic Links: `mv` moves the actual file or directory, not the symbolic link itself, unless the link is the source and the destination is a new link name. If you move a file that a link points to, the link will continue to point to the original file's new location.

File Systems: On the same file system, `mv` is very fast because it only updates the directory entry (inode information) pointing to the data, essentially a rename operation. If you move files across different file systems, `mv` has to perform a copy operation followed by a delete operation, which takes longer and consumes more resources.

In summary, the `mv` command is an indispensable tool for managing files and directories in Linux, offering flexibility in both moving and renaming operations. Understanding its options and default behaviors, particularly regarding overwriting, is key to using it effectively and safely.

Sources

  1. Mv (Unix) - WikipediaCC-BY-SA-4.0
  2. mv(1) - Linux man pagesCC-BY-SA-4.0

Missing an answer?

Suggest a question and we'll generate an answer for it.