Table of Contents
Share this Resource

Renaming Files in Linux

Before We Begin

1) Linux filenames and paths are case-sensitive
2) mv can rename both files and directories
3) Batch renaming often uses loops or rename utilities
4) rename syntax can vary across Linux distributions
5) Preview or verify changes before bulk renaming

Let’s say that you’ve just downloaded a set of files with names like “file001” and “file002,” and you need to rename them to something more meaningful. But how do you handle this efficiently, especially on Linux? Renaming files might seem like a small task, but it’s one that can quickly become overwhelming without the right tools or techniques. This is where learning How to Rename Files in Linux becomes a game-changer.

In this step-by-step blog, we’ll simplify how to rename files in Linux and walk you through practical, easy-to-follow methods to keep your workflow organised and efficient.

Understanding File Paths in Linux

Linux organises files and directories in a hierarchical structure that begins at the root directory (/). Before renaming a file, you need to know its name and location, either from the current directory or through its full path.

For example:

/home/user/documents/file.txt

This path indicates that file.txt is located inside the documents directory under the user's home directory. Understanding paths helps ensure that you rename the intended file without accidentally moving or modifying another one.

Linux Training

Renaming Files Using a Graphical File Manager

Linux desktop environments also allow files to be renamed without using the command line. In file managers such as GNOME Files or KDE Dolphin, select the file, choose Rename or press F2, enter the new filename, and confirm the change.

Graphical file managers are particularly useful for simple renaming tasks and can also support batch renaming in some environments. For example, GNOME Files and KDE Dolphin provide options for renaming multiple selected files.

Renaming Files Using the mv Command

The mv command, short for “move”, is commonly used to move or rename files and directories in Linux. When a source and a new destination name are provided in the same location, mv effectively renames the item.

1) Renaming a Single File

To rename a single file, use the mv command followed by the current filename and the desired new filename. For example, to rename oldname.txt to newname.txt, run:

mv -- oldname.txt newname.txt

The -- marks the end of command options, helping mv safely handle filenames that begin with a hyphen.

2) Renaming Multiple Files

The mv command renames one source file to one destination name at a time. For multiple files, you can combine it with a shell loop.

For example, a shell loop can rename every .txt file in the current directory by adding archive_ to the beginning of each existing filename.

for file in *.txt; do
    mv -- "$file" "archive_$file"
done

This loop processes each .txt file in the current directory and adds archive_ to the beginning of its filename. Quoting "$file" ensures filenames containing spaces are handled correctly.

Want to become a proficient Linux System Administrator? Sign up for our Administering Linux Systems Course now!

How to Rename Files on Linux Using the Rename Command?

The rename utility can differ between Linux distributions. Check your installed version using rename --version or consult man rename before using pattern-based commands.

For Perl-based rename, commonly available as the rename package on Ubuntu:

rename 's/old/new/' *.txt

This replaces old with new in matching .txt filenames.

On Ubuntu, the package can be installed with:

sudo apt install rename

Ubuntu currently identifies this package as a Perl extension for renaming multiple files. 

For the util-linux implementation, the syntax is different:

rename old new *.txt

The current util-linux syntax is rename [options] substring replacement file.... 

For safer batch operations, preview changes first where supported or use options such as --no-act, --interactive, or --no-overwrite with the util-linux version.

Quick Safety Check

Before running a bulk rename:
1. Confirm you are in the correct directory
2. Preview changes where the utility supports it
3. Quote filename variables in shell loops
4. Check whether destination filenames already exist
5. Test the command on a small set of files first

Using Wildcards and Shell Loops for Batch Renaming

Wildcards are pattern-matching characters that help select groups of filenames. When combined with commands or shell loops, they can be used to perform batch renaming operations. For instance, the "*" wildcard matches any sequence of characters, while "?" matches a single character.

You can perform batch renaming operations efficiently by combining wildcards with the mv command. For example, to rename all files starting with "photo" to add a consistent prefix to each filename”, you can use the following command: 

for file in photo*.jpg; do
    mv -- "$file" "holiday_$file"
done

This prefixes each matching filename with holiday_, for example photo01.jpg becomes holiday_photo01.jpg.

Build confidence in command-line automation and scripting with our Linux Shell Programming Course – Start learning today!

How to Handle Filename Conflicts

When renaming a file, check whether the intended destination name already exists. With GNU mv, using the -i option prompts you before an existing destination file is overwritten, helping prevent accidental replacement.

For example, you can use mv -i oldname.txt newname.txt when you want confirmation before overwriting an existing file. 

mv -i -- oldname.txt newname.txt

The -i option asks for confirmation before overwriting an existing file, while -- marks the end of command options.

GNU mv also provides options such as --no-clobber and backup controls for managing destination conflicts. 

Can You Undo File Renaming in Linux?

The mv command does not include a built-in undo feature. If you know the original filename, you can usually reverse the rename by running another mv command, as long as a file with the original name does not already exist:

mv newname.txt oldname.txt

For batch operations, preview changes whenever the chosen renaming utility supports it and keep a record of the original filenames. Graphical file managers can also provide undo functionality. GNOME Files and KDE Dolphin, for example, support undoing recent rename actions with Ctrl+Z. 

Advanced Techniques for File Renaming

Linux offers advanced techniques for file renaming, providing even more flexibility and customisation options. These techniques are:

1) Using Regular Expressions

Regular expressions can support advanced filename transformations when used with compatible rename utilities or within shell scripts. Because rename implementations differ across Linux distributions, always verify the supported syntax before applying a complex pattern.

2) Writing Bash Scripts for Renaming

Writing bash scripts can automate the process of complex and repetitive renaming tasks. You can create custom renaming scripts tailored to your needs by combining Linux commands and scripting constructs.

How do I Rename a Folder in Linux?

To rename a folder in Linux, use the mv command: 

mv old_folder_name new_folder_name

This renames the directory when new_folder_name does not already identify an existing directory. If the destination is an existing directory, mv can instead move the source directory inside it.

Trainer’s Pro Tip

Before renaming multiple files, test your command on a small sample and check the expected filenames first. For larger batch operations, use preview or interactive options where available so you can catch naming errors or conflicts before they affect many files.
Nilotpal Sarmah
Nilotpal Sarmah

Senior Content Writer

Nilotpal Sarmah is a Senior Content Writer with 11+ years of overall experience spanning engineering, operations and content development. His technical knowledge and extensive writing experience enable him to simplify specialised topics across IT and Tech, Business Skills, Project Management, Health and Safety, and ISO and Compliance.

View Detail icon
cross

BIGGEST HALLOWEEN SALE!

halloween-sale GET UPTO 40% OFF!

* WHO WILL BE FUNDING THE COURSE?

close

close

Thank you for your enquiry!

One of our training experts will be in touch shortly to go over your training requirements.

close

close

Press esc to close

close close

Back to course information

Thank you for your enquiry!

One of our training experts will be in touch shortly to go overy your training requirements.

close close

Thank you for your enquiry!

One of our training experts will be in touch shortly to go over your training requirements.