Home Learn Linux The Linux Pro’s Guide to File Renaming in 2023

The Linux Pro’s Guide to File Renaming in 2023

We will unveil six ingenious methods to rename files in Linux, as of 2023. Whether you're dealing with a single file or a directory full of them, these methods will equip you to tackle file renaming tasks with ease and precision.

by Divya Kiran Kumar
your ultimate guide to renaming files in linux

Hello, dear FOSS Linux readers! Linux – that beautifully intricate beast, never ceases to fascinate us with its unlimited potential. Some love it, some fear it, but once you start understanding it, there’s no going back. So, let’s dive deep into one of its many compelling aspects: renaming files.

Contrary to popular belief, renaming files in Linux doesn’t have to be complicated or scary. Through this blog, I aim to shine a light on six different methods to rename a file in Linux, batch mode included, as of 2023. Each method has its own charm, and some have left me utterly spellbound while others, well, not so much. But they all serve a purpose, and you might end up loving the one I didn’t quite fancy. That’s the beauty of Linux – to each their own! So, get ready to delve into the world of Linux and master the art of file renaming.

6 powerful techniques to rename files in Linux

1. The ‘mv’ command

If we are talking about renaming files in Linux, it would be unjust not to start with the tried-and-true ‘mv’ command. It stands for ‘move’, and while it might seem counterintuitive to use a ‘move’ command to rename a file, in Linux-land, renaming is just moving a file to a new name.

mv oldname.txt newname.txt

Just replace ‘oldname.txt’ with your current file name, and ‘newname.txt’ with the name you want it to have. Simple, right?

Here is a practical example showing renaming a folder. The directory “FOSSLinux” is renamed “FOSSLinux.com” in the screenshot below.

renaming a folder using mv command

Renaming a folder using mv command

However, I have mixed feelings about this method. While its simplicity is endearing, it lacks the refinements of error handling and won’t warn you if you’re about to overwrite an existing file. But, hey, if you’re careful, ‘mv’ might just be your trusty old tool in the Linux toolbox.

2. The ‘rename’ command

Next on the roster is the ‘rename’ command, one of my personal favorites due to its power and flexibility. It uses Perl expressions, which makes it a more formidable option. The command may not be installed in your Linux distribution by default, but you can easily install it using the package manager.

For Debian-based systems like Ubuntu, use the apt-get or apt command:

sudo apt-get update
sudo apt-get install rename

Or if you’re using a newer version of Ubuntu, you might simply use:

sudo apt update
sudo apt install rename

For Red Hat-based systems like CentOS or Fedora, use the yum command:

sudo yum install rename

In Fedora 22 and later versions, you might use the dnf command:

sudo dnf install rename

On Arch Linux:

sudo pacman -Syu
sudo pacman -S perl-rename

The -Syu option updates the package database and upgrades all out-of-date packages, which is generally a good practice before installing a new package. The -S option installs the package, which is perl-rename in this case.

The rename command in Arch Linux is provided by the perl-rename package, hence the use of perl-rename instead of rename.

After installation, you should be able to use the rename command.

rename 's/oldname/newname/' *.txt

In the command above, ‘s/oldname/newname/’ is a Perl expression that replaces ‘oldname’ with ‘newname’ in all .txt files. Yes, you heard it right! It’s a lifesaver when you want to rename multiple files at once. But the downside? If Perl expressions seem like hieroglyphics to you, you might find this method a bit daunting.

Here is a practical example showing rename command in action.

rename 's/FOSSLinux/FOSSLinux.com/' *.txt
using rename command

Using rename command

3. The ‘mmv’ command

The ‘mmv’ command (Multiple Move) is another way to rename files in batch mode. It’s not typically installed by default, but it’s worth the installation effort.

Let’s go through how to install mmv in various Linux distributions:

Ubuntu/Debian: The mmv utility can be installed from the standard repositories using the apt-get or apt command.

sudo apt-get update
sudo apt-get install mmv

CentOS/Fedora: In CentOS or Fedora, you’ll need to enable the EPEL repository to install mmv using the yum or dnf command.

sudo yum install epel-release
sudo yum install mmv

or if you are using a more recent Fedora:

sudo dnf install epel-release
sudo dnf install mmv

Arch Linux: In Arch Linux, the mmv utility is available in the Community repository. You can install it using the pacman package manager.

sudo pacman -Syu
sudo pacman -S mmv

Once you’ve installed mmv, you can start using it for all your batch renaming needs.

mmv '*oldname*' '#1newname#2'

The command translates to: change ‘oldname’ to ‘newname’ in filenames where it is found. Here, ‘#1’ and ‘#2’ denote the parts of the filename before and after ‘oldname’. It’s a bit tricky, I admit, and honestly, I haven’t quite warmed up to ‘mmv’ yet. But it’s certainly powerful and has a loyal fanbase. Again, here’s a practical example of how you might use the mmv command.

Let’s assume you have a directory filled with .txt files named in the format FOSSLinux1.txt, FOSSLinux2.txt, FOSSLinux3.txt, etc. But you’ve realized you want them to be named FOSSLinux.com1.txt, FOSSLinux.com2.txt, FOSSLinux.com3.txt instead.

With mmv, you can easily perform this rename operation as follows:

mmv 'FOSSLinux*.txt' 'FOSSLinux.com#1.txt'
mmv command usage

mmv command usage

Here’s what’s happening in this command:

FOSSLinux*.txt is the source pattern. The asterisk (*) is a wildcard character that matches any sequence of characters. In this context, it matches any string that follows ‘FOSSLinux’ in the filename before ‘.txt’.
FOSSLinux.com#1.txt is the destination pattern. #1 represents the first (and in this case, only) wildcard from the source pattern. So, whatever string the * matched in the source filename, it will be placed at the #1 in the destination filename.

This is just a simple example, and the real power of mmv becomes evident when dealing with more complex rename operations. So, play around with it, try more patterns, and see how it can simplify your life!

4. The ‘mv’ command in a Bash Loop

You can utilize the power of a Bash loop with ‘mv’, creating a robust renaming command. Here’s a quick example:

for f in *.txt; do mv "$f" "${f/oldname/newname}"; done

This command renames all .txt files containing ‘oldname’ to ‘newname’. I am partial to this method. The elegance of a Bash loop combined with the simplicity of ‘mv’ – it’s poetry in command-line form!

Practical example:

for f in *.txt; do mv "$f" "${f/FOSSLinux/FOSSLinux.com}"; done
using bash to rename files

Using bash to rename files

In the above example, the script renames txt files containing ‘FOSSLinux’ to ‘FOSSLinux.com’.

5. Python scripts

If you’d like to rename a file using Python, you can do so using the os.rename() function from the os module. Here’s a practical example:

Let’s say you have a file named ‘oldname.txt’ in your current directory, and you want to rename it to ‘newname.txt’.

First, create a new Python script and open it in your text editor. Let’s call it rename_file.py.

touch rename_file.py
nano rename_file.py

In your Python script, you will need to import the os module, and then you can use os.rename() to rename your file. Here’s how:

import os

# define the name of the file to be renamed
old_file_name = "oldname.txt"

# define the new name for the file
new_file_name = "newname.txt"

# use the rename() function from the os module
os.rename(old_file_name, new_file_name)

Save your script and exit the text editor. You can then run your Python script from the terminal like so:

python3 rename_file.py

After running this script, the file ‘oldname.txt’ will be renamed to ‘new_name.txt’ in the same directory.

Remember, you’ll need to have the correct permissions to rename the file, and this script needs to be run in the same directory as the file you want to rename. If the file is in a different directory, you’ll need to include the full path to the file in old_file_name.

Python can be a powerful tool for managing files and directories, especially when dealing with more complex tasks and larger numbers of files. However, for simpler tasks or for performing actions on a single file, using the command line directly can often be quicker and easier.

6. Graphical file managers

If you are a fan of GUI over the command line, Linux has got you covered. Various graphical file managers like Nautilus (GNOME), Dolphin (KDE), and Thunar (XFCE) allow easy renaming of files through right-click options. It’s as simple as right-clicking a file and selecting the ‘rename’ option.

renaming folder using gui in linux

Renaming folder using GUI in Linux

Even as a hardcore command line lover, I understand the charm of GUIs. They are intuitive, visual, and hence widely popular among many Linux users. But remember, with great GUI power, comes great system resource consumption!

So, there you have it – six diverse methods to rename files in Linux, each with its own strengths and weaknesses. Depending on your familiarity and comfort with the command line or GUI, your affection for scripting, or your desire for power and flexibility, you can choose the method that best suits your needs.

Conclusion

In the end, I want to stress that Linux is all about freedom and choice. It’s like a vast ocean, waiting for you to dive in and explore its depth. So, don’t limit yourself to just one method. Try them all, experiment, find the one that resonates with you or even better, master them all. Remember, with Linux, you are only limited by your own imagination. Happy exploring, and until next time, keep tinkering!

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.