How to Rename Files in Linux

Published:1 September 2021 - 9 min. read

Michael Franklin Image

Michael Franklin

Read more tutorials by Michael Franklin!

Are you new to the Linux operating system? If so, common tasks such as renaming files and folders may be different than what you are used to. But don’t fret. The concept of renaming files in Linux is similar to how it’s done in other operating systems.

In this tutorial, you’ll learn many ways to rename single and multiple files in Linux, both via GUI and running commands in a terminal.

Let’s get started!

Prerequisites

To follow along within this tutorial, make sure you have the following:

Rename Files in Linux with File Manager

If you are used to managing files via GUI, let’s start this tutorial to rename files with a file manager. GUI file managers are neat since you can see the files in plain sight. One of those file managers available in Linux is GNOME’s former file manager, Nautilus. This tutorial demonstrates renaming files using the Nautilus file manager.

Linux’s other file managers are Konqueror, Dolphin, Krusader, Thunar, Nemo file manager, and Sunflower file manager. These file managers perform the same task but renaming files or directories may be slightly different.

Launch your terminal, then run the command below to install Nautilus (install nautilus) with the apt package manager if you don’t have it yet on your machine. The sudo command elevates the command with sudo/administrator rights.

sudo apt install nautilus

Renaming a Single File

Renaming a single file in Nautilus takes only a few clicks, like in Windows File Explorer.

To rename a single file, open Nautilus, right-click on the file you want to rename, and choose Rename, as shown below.

This example focuses on renaming files, but the process is the same to rename both files and directories.

Renaming a Single File
Renaming a Single File

Now type the new file name to replace the original file name, press Enter or click on Rename, and that’s it.

Entering New File Name
Entering New File Name

Renaming Multiple Files using Nautilus File Manager

Like Windows, Ubuntu gives you more options on how you want to rename multiple files at once.

To rename multiple files, select all files you want to rename, right-click on any of them and choose Rename. A pop-up window will appear where you can choose how you want to rename the files you selected.

Renaming Multiple Files Using Nautilus.
Renaming Multiple Files Using Nautilus.

Below, there are two options to choose from depending on how you want to rename the selected files.

  • Rename using a template – This allows you to rename files in increments.
  • Find and replace text – Allows you to find and replace certain texts in file names.

Renaming Files using a Template

To rename files using a template, select the Rename using a template option.

Now enter the replacement name in the empty field to rename files with a template, and click on the +Add button and choose from the templates. For this tutorial, select the first one (1,2,3,4) and click Rename. This option adds incrementing numbers to each file after the replacement filename (Text1, Text2,…).

Renaming Files using a Template
Renaming Files using a Template

Notice the file names changed to Text, followed by incrementing numbers.

Verifying File Names Changed to "Text" with Incrementing Num
Verifying File Names Changed to “Text” with Incrementing Num

Finding and Replacing Words in the File Names

To rename multiple files with a specific file name, select the Find and replace text option.

Enter the text in the Existing Text field and the replacement text in the Replace With field, then click Rename to replace the text. This option finds and replaces certain texts in file names.

Replacing Texts in File Names
Replacing Texts in File Names

Now, you can see below that you’ve replaced the word “Text” with “File” in each file name.

Verifying the word "Text" was replaced with the word "File" on Each File
Verifying the word “Text” was replaced with the word “File” on Each File

Renaming Files with the mv Command

If running commands is your thing instead of navigating in a GUI, prepare yourself to learn how to rename files in a terminal. Renaming files in the Linux terminal is similar to doing it in other command-line environments, such as PowerShell. You run the commands to rename the original file name with the target file name, and that’s it.

There are two scenarios in renaming a file. The first is renaming a file from the working directory, and the second is by specifying the full path. Take a look at the mv command syntax below to rename a file, where the source is the original name, and the destination is the replacement name.

# Rename a file or directory in the working directory
mv <source> <destination>
# Rename a file by specifying the full path
mv <~/mydir/source> <~/mydir/destination>

Renaming a Single File with the mv Command

While the mv command syntax is still fresh in your mind, let’s try renaming a single file.

To demonstrate how to rename a single file and directory:

1. Launch your terminal, then run the series of commands below to create a directory and two text files.

# Create a directory named 'test'
mkdir test 
# Change directory to ~/test
cd test 
# Create text files named 'file1.txt' and file2.txt
touch file1.txt file2.txt 

2. Next, run the ls command to list the files inside the working directory. Below, you can see that file1.txt and file2.txt exist in the ~/test directory.

Listing Files in the Working Directory
Listing Files in the Working Directory

3. Run either of the mv commands below to rename file1.txt as file10.txt.

# Rename a file in the working directory
mv file1.txt file10.txt

# Rename a file by specifying the full path
mv ~/test/file1.txt ~/test/file10.txt

Renaming a directory is the same as renaming a file, like this: mv ~/test ~/demo. The ~ symbol denotes the home directory.

4. Finally, rerun the ls command to list the files in the working directory.

Below, you can see that file1.txt is now renamed as file10.txt.

Confirming file1.txt is renamed as file10.txt
Confirming file1.txt is renamed as file10.txt

Renaming Multiple Files with a For Loop

If you’re too busy with other tasks at hand and want to rename all files in one go, you’ll need to use a for loop statement like the one below.

Copy the code below as-is, paste it in your terminal, and press Enter. The code below scans each text file in the working directory and renames the files with “File” followed by an incrementing number.

# Declares variable with value set to 1
i=1 
# Scans each text file in the working directory
for file in *.txt; 
# Iterate the command below until all files are scanned
do 
  # Renames each file with "File" followed by incrementing number ($i)
	mv -- "$file" "File$i.txt"
  # Increments the variables current number value by 1
  i=$((i+1)) 
done

Pasting the code into your terminal works fine, but as good practice, it’s better if you create a script file to run instead.

Below, you can see in action how the code renames all files.

Renaming Files with a For Loop
Renaming Files with a For Loop

Renaming Files in Linux with the rename Utility

If you’re renaming a single file in your daily routine, then the mv command will suffice. But perhaps you’re aiming to rename multiple files with the same pattern at once? For example, renaming files that start with a certain word (file). In that case, install the rename utility instead.

The rename utility renames multiple files, changes file name formats, and overwrites files.

To install the rename utility, run any of the commands below depending on the Linux distro you’re working on.

# Install rename utility with the apt package manager for Ubuntu, Debian, and Linux Mint
sudo apt install rename
# Install rename utility with the dnf package manager for CentOS, Fedora, and Red Hat
sudo dnf install prename
# Install rename utility with the pacman package manager for Arch Linux / Manjaro Linux
sudo pacman -S perl-rename

You can see below the rename command syntax.

rename <options> <perlexpr> <files>

Renaming Multiple Files

With the rename command syntax in mind, you can now rename files that have the same pattern. What’s great about the rename command is that you don’t need to declare a loop function to rename multiple files as you did in the “Renaming Multiple Files with a For Loop” section.

Perhaps you want to rename files with certain words on them. If so, take a look at the Perl regular expression ('s/file/myfile/') in the sample command below. Perl regular expressions define a string pattern based on the Perl programming language.

To avoid mistakenly renaming other files, list all the affected files before renaming them by adding the -n option, as shown below.

rename -n 's/file/myfile/' *.txt
Listing Affected Files Before Renaming
Listing Affected Files Before Renaming

Now run either of the rename commands below to replace the word file with myfile in each text file’s name inside the working directory or the ~/test/*.txt directory.

In the Perl expression ('s/file/myfile/'), the s denotes substitution, which means you’re renaming files. Notice the -v option is added to return which files the command renamed.

# Replace 'file' with 'myfile' in each file's name in the working directory
rename -v 's/file/myfile/' *.txt
# Replace 'file' with 'myfile' in each file's name in the ~/test directory
rename -v 's/file/myfile/' ~/test/*.txt

Below, you can see that each “file” word in the text files is renamed to “myfile.”

Renaming a Files with the rename Command
Renaming a Files with the rename Command

If you prefer to rename all files instead, remove the file extension when you specify the file path, for example, rename -v 's/file/myfile/' ~/test/*

Changing File Extensions

Other than file names, the rename command also renames file extensions. Perhaps you’ve created files with the wrong file extension. In that case, you’ll specify the file extension to change in a Perl expression when you run the rename command.

Run the rename command below to change all files’ (*) extension from .txt to .html in the ~/test directory. The $ symbol in the Perl expression matches the end of a string (.txt) to each file name, so the command only changes .txt file extensions. And like with renaming multiple files, add the -v option to list each change the command makes.

rename -v 's/.txt$/.html/' *.txt

To remove all file extensions instead, remove the replacement string in the Perl expression, like this: rename -v 's/.txt$//' *.txt

As you see in the image below, the .txt extensions changed to .html

Changing File Extensions from .txt to .html
Changing File Extensions from .txt to .html

Changing File Names to Uppercase and Lowercase

Instead of incorrect file extensions, you may be dealing with filenames in lowercase and vice versa. Perhaps you’re trying to find a file specifically in uppercase, not knowing the file name is in lowercase.

Linux is case-sensitive, so let’s ensure you have each file name’s letter case right. Letter case is basically the distinction of texts between lowercase and upper case.

Run either of the rename commands below to change all lowercase (a-z) file names in the working directory (*) to uppercase (A-Z) and vice versa.

# Change filename from lowercase to uppercase
rename -v 'y/a-z/A-Z/' *
# Change filename from uppercase to lowercase
rename -v 'y/A-Z/a-z/' *

You can see below both results of changing a file name letter case.

Changing File Name from Lowercase to Uppercase and Vice Versa
Changing File Name from Lowercase to Uppercase and Vice Versa

Replacing and Removing Spaces in File Names

Like a file name’s letter case, you may have trouble finding files if they have spaces in their file names. Let’s fix that by either replacing spaces with a character or removing the spaces altogether.

Run the rename command below to replace spaces in all file names with an underscore (_) in the working directory. Since underscore is a metacharacter, escape it with a backslash (\\_) to declare the underscore literally. Metacharacters are special characters with special meanings in a computer program.

Notice the g in the Perl expression tells the command to replace all matches (spaces), and not just the first one.

rename -v 's/ /\\_/g' *

Below, you can see that the command replaced all spaces with underscores.

Replacing spaces with underscores in a file name
Replacing spaces with underscores in a file name

To remove all spaces in a file name instead of filling them up with a character, run the command below.

In the Perl expression below, the asterisk after the blank space (/ *) matches all occurrences of spaces in a file name and not just the first one. The replacement character is empty (//), so the rename command removes all (g) spaces.

rename -v "s/ *//g" *

Now you can see below that all spaces are removed from each file name.

Removing all spaces in filenames
Removing all spaces in filenames

Capitalizing the First Letter of Each Word in a File Name

If you prefer to keep the spaces and capitalize the first letter of each word in a file name instead, you’ll need a more complex Perl expression. Take a look at the sample command below.

The Perl expression below matches any single character (\\w) in a word boundary (\\b) so long as the character is not a period nor apostrophe (?<![.'\\'']). The rename command will then translate that letter to uppercase (u$&).

rename -v 's/(?<![.'\\''])\\b\\w*/\\u$&/g' *

Below, you can see that each word in the file names starts with a capital letter while leaving the file extensions untouched.

Capitalizing First Letter of Each Word in File Names
Capitalizing First Letter of Each Word in File Names

Conclusion

Throughout this tutorial, you’ve learned many ways to rename Linux files via GUI and command-line terminal. You now have a solid understanding of how to manipulate names for single and multiple files at once.

Why not learn Linux shell scripting, and create a script to automate renaming files in Linux?

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!