How to Rename Files and Directories in Linux Using the mv Command
In Linux, working with files and directories often includes tasks like renaming, organizing, or moving them between locations. One of the most efficient ways to rename a directory is by using the mv (move) command, which allows you to rename files or directories by assigning them a new name within the same path.
This guide explains how to rename files and directories in Linux using the mv command.
Prerequisites
- Access to a Linux-based system as a non-root user with sudo privileges.
mv Command Syntax
The mv command in Linux is used to move or rename files and directories. When applied for renaming, it changes the name without altering the content or location.
Syntax:
mv [OPTION]... SOURCE DEST
Parameters
- SOURCE: The current name of the file or directory to rename or move.
- DEST: The new name you want to assign to the file or directory.
Common Options
- -i or –interactive: Prompts before overwriting an existing file or directory.
- -n or –no-clobber: Prevents overwriting existing files or directories.
- -v or –verbose: Shows detailed output of the operation.
- -T or –no-target-directory: Treats the target as a standard file or directory name instead of a container for the source.
Renaming a Directory with mv
To rename a directory, simply provide the current name followed by the new name. The following examples demonstrate different scenarios using mv along with useful flags.
Basic Rename
Example: rename current_dir
to renamed_dir
:
$ mv current_dir renamed_dir
If renamed_dir
already exists, current_dir
will be moved into it rather than renamed.
Force Target as Normal Directory
$ mv -T current_dir renamed_dir
This treats the target as a standard directory name. The -T
option only works if the target is empty.
Prompt Before Overwriting
$ mv -iT current_dir renamed_dir
If the target exists, you’ll be asked:
mv: overwrite 'renamed_dir'?
— Enter Y to confirm or N to cancel.
Prevent Overwriting
$ mv -nT current_dir renamed_dir
This prevents overwriting if the target already exists.
Show Verbose Output
$ mv -vT current_dir renamed_dir
Example output: renamed 'current_dir' -> 'renamed_dir'
Important Note
If renaming directories you don’t own or system directories, prepend sudo
to the command:
$ sudo mv current_dir renamed_dir
Never rename critical system directories (e.g., /etc
, /var
, /usr
) unless you are certain of the consequences.
Renaming a Subdirectory with mv
Renaming subdirectories works the same way, using either a relative or absolute path.
Using a Relative Path
$ mv parent_dir/sub_dir parent_dir/renamed_sub_dir
This changes sub_dir
to renamed_sub_dir
inside parent_dir
.
Using an Absolute Path
$ mv /home/user/project/parent_dir/sub_dir /home/user/project/parent_dir/renamed_sub_dir
This method is useful when you are in a different location or working in scripts that require full paths.
Note
Ensure the target path does not already exist unless you intend to overwrite or move into it. Use options like -i
, -n
, -T
, or -v
for safer operations.
How to Rename Files and Directories in Linux Using the mv Command
In Linux, tasks such as renaming, organizing, or relocating files and directories are common. One of the most efficient ways to rename a directory is by using the mv (move) command, which can rename files or directories by assigning a new name within the same path.
This guide explains how to rename files and directories in Linux using the mv command.
Prerequisites
- Access to a Linux-based system as a non-root user with sudo privileges.
mv Command Syntax
The mv command is used to move or rename files and directories. When used for renaming, it changes the name without altering the location or content.
Syntax:
mv [OPTION]... SOURCE DEST
Parameters
- SOURCE: Current name of the file or directory to be renamed or moved.
- DEST: The new name to assign.
Common Options
- -i or –interactive: Prompts before overwriting an existing file or directory.
- -n or –no-clobber: Prevents overwriting an existing file or directory.
- -v or –verbose: Shows detailed output of the operation.
- -T or –no-target-directory: Treats the target as a normal directory name rather than a container for the source.
Renaming a Directory with mv
To rename a directory, specify its current name followed by the new name. The examples below show different use cases with helpful flags.
Basic Rename
$ mv current_dir renamed_dir
If renamed_dir
exists, current_dir
will be moved into it rather than renamed.
Force Target as a Normal Directory
$ mv -T current_dir renamed_dir
Ensures the target is treated as a directory name rather than a container. The -T
option works only if the target is empty.
Prompt Before Overwriting
$ mv -iT current_dir renamed_dir
If the target exists, you will be prompted with:
mv: overwrite 'renamed_dir'?
— Press Y to confirm or N to cancel.
Prevent Overwriting
$ mv -nT current_dir renamed_dir
Skips the operation if the target already exists.
Show Verbose Output
$ mv -vT current_dir renamed_dir
Example output: renamed 'current_dir' -> 'renamed_dir'
Important Note
For directories you do not own or system directories, prepend sudo
:
$ sudo mv current_dir renamed_dir
Avoid renaming critical system directories like /etc
, /var
, /usr
unless certain of the outcome.
Renaming a Subdirectory with mv
Subdirectories can be renamed using relative or absolute paths.
Using a Relative Path
$ mv parent_dir/sub_dir parent_dir/renamed_sub_dir
Changes sub_dir
to renamed_sub_dir
inside parent_dir
.
Using an Absolute Path
$ mv /home/user/project/parent_dir/sub_dir /home/user/project/parent_dir/renamed_sub_dir
Useful when working from another location or within scripts that require full paths.
Note
Ensure the target path does not exist unless you intend to overwrite or move into it. Use options like -i
, -n
, -T
, or -v
for controlled operations.
Renaming Files with mv
Renaming files uses the same syntax as directories. Provide the current filename and the desired new name.
Basic File Rename
$ mv file.txt new_filename.txt
Preserve the file extension unless you intend to change it.
Warning
If the target file exists, it will be overwritten without warning unless -i
is used.
Prompt Before Overwriting
$ mv -i file.txt new_filename.txt
Prompts for confirmation before overwriting the target file.
Prevent Overwriting
$ mv -n file.txt new_filename.txt
Skips renaming if the target file exists.
Show Verbose Output
$ mv -v file.txt new_filename.txt
Example output: renamed 'file.txt' -> 'new_filename.txt'
Use sudo for Restricted Files
$ sudo mv file.txt new_filename.txt
Only use sudo
when necessary; renaming system or configuration files incorrectly can cause issues.
Conclusion
You have learned how to rename directories, subdirectories, and files in Linux using the mv command. You also learned how to apply options like interactive (-i
), verbose (-v
), no-clobber (-n
), and no-target-directory (-T
) for better control, use both relative and absolute paths for subdirectories, and apply sudo
when required. For further details, run the man mv
command in your Linux terminal to view the manual page.