How to Rename Files in Linux

December 12, 2022

How to Rename Files in Linux

This guide is part of the “Linux Commands” series. This series is focused on providing an in-depth overview of Linux commands and tools, in an easy-to-follow manner!


How to Rename a Single File

To rename a file in Linux, you can use the mv command. This command allows you to move a file from one location to another and can also be used to rename a file.

Here's an example of how to use the mv command to rename a file:

mv old-file-name new-file-name

In this example, old-file-name is the current name of the file, and new-file-name is the new name that you want to give the file.

It's important to note that the mv command will only work if you have permission to rename the file. If you're not the owner of the file, you may need to use the sudo command to gain the necessary permissions.

Here's an example of using sudo to rename a file:

sudo mv old-file-name new-file-name

In this example, sudo gives you the necessary permissions to rename the file. If you're not logged in as root (technically, you never should be) - you will be asked to enter your password to access the sudo command.


How to Rename Multiple Files

To rename multiple files, there are two options: manual and automated.

The manual version would look like this:

mv old_file_name_1 new_file_name_1 old_file_name_2 new_file_name_2

In other words, doing more than 2 files at once is not practical.

To rename multiple files in a directory with a specific file extension, you can use the find command in combination with the mv command. The find command allows you to search for files in a directory based on their name, size, or other attributes.

# Find all files with the .txt extension in the current directory
find . -name "*.txt" -type f

# Rename all files with the .txt extension to the .text extension
find . -name "*.txt" -type f -exec mv {} {}.text \;

Note that the find and mv commands will not rename files that are in subdirectories of the current directory. If you want to search and rename files in subdirectories as well, you can use the -r flag with the find command to search recursively, like this:

# Find all files with the .txt extension in the current directory and subdirectories
find . -name "*.txt" -type f -r

# Rename all files with the .txt extension to the .text extension in the current directory and subdirectories
find . -name "*.txt" -type f -r -exec mv {} {}.text \;

How to Rename Multiples Files AND Remove Original Extension

The examples above will retain the original extension that you rename from.

If you want to remove the original file extension and replace it with a new one, you can use the basename and dirname commands in the mv command.

The basename command allows you to extract the base name (i.e., the file name without the file extension) of a file, and the dirname command allows you to extract the directory name of a file.

Here is an example of how to use the basename and dirname commands to rename files and remove their original file extensions:

# Find all files with the .txt extension in the current directory
find . -name "*.txt" -type f

# Rename all files with the .txt extension to the .text extension, removing the original file extension
find . -name "*.txt" -type f -exec sh -c 'mv "$1" "$(dirname "$1")/$(basename "$1" .txt).text"' _ {} \;

How to Rename Files Using 'rename'

The rename command is a package that can be installed on your Linux system:

# Debian, Ubuntu, Linux Mint and others:

sudo apt install rename

# CentOS 7 or RHEL:

sudo yum install rename

# Arch Linux:

yay perl-rename

All major package managers should have this package available by default. With it, you can do a lot of the renaming without having to write scripts, and once you're done - you can uninstall it.

Below are some examples of how to use the rename command once you have it installed.

To rename all files with the .txt extension to .doc:

rename 's/.txt$/.doc/' *.txt

To rename all files that start with old_ to start with new_:

rename 's/^old_/new_/' old_*

To rename all files that end with a number to have the number doubled,:

rename 's/(\d+)$/$1$1/' *[0-9]

To rename all files to be lowercase:

rename 'y/A-Z/a-z/' *

To rename all files to have the current date appended to the end of their names:

rename "s/$/.$(date +%Y-%m-%d)/" *

Note: The rename command may have different syntax and options depending on your Linux distribution and version. Consult the rename command's documentation for more information.