How to Rename a File in Linux With Simple Command Line Options
Developed in the early 1990s, Linux has remained a popular operating system for both personal use and commercial servers. Today, there are over 600 Linux distributions (versions) available, though only a handful of them remain in common use. Based on Unix, the Linux system is prized for its portability, reliability, and stability. There are many individuals (primarily industry professionals) who prefer using Linux over any other operating system.
That said, Linux does require some advanced knowledge to use. While today most Linux distributions have a GUI (graphical user interface), many don’t. Working with Linux requires prior knowledge because most commands are given through a command-line text.
If you’ve worked in DOS or have programmed in C, you will know how to use a command line. But with Linux, it’s all about knowing individual commands — including the commands to rename and move files.
Today, we’re going to take a look at how to rename a file in Linux. We’ll look at the rename tools that Linux offers, renaming a single file, how to rename files and directories, and renaming multiple files through batch rename.
Last Updated March 2023
Linux Administration (Ubuntu and CentOS) for Beginners. Get the Linux skills to boost your career and get hired. | By Andrei Dumitrescu, Crystal Mind AcademyExplore Course
How to rename a single file in Linux
If we need to rename a single file in Linux, we have two options: we can create a copy of the file with a new name (and delete the old one) or we can rename the file by moving it (with the MV command).
Renaming a file by copying and deleting it
Linux users copy a file by using the “cp” command. When you copy a file, you give the source files and rename the files.
$ cp old_file new_file
As an example, if we were to rename “august.png” to “september.png”:
$ cp august.png september.png
This will create a copy of the file with a new name. But now you’ll have two copies. You’ll resolve this by deleting the old file. You delete with “rm.”
$ rm august.png
You may be wondering why this is so cumbersome compared to Windows, where you’d just rename a file. Linux has a lot of power, but it manages that power by putting it in the user’s hands. Linux users will perform more operations incrementally, but they have complete control over how those operations are performed.
This gives way to another issue: We could have named that second file september.jpg or september.doc. And then the file would no longer work! Linux provides a lot of power and a lot of freedom.
Renaming a file by moving it
You may be relieved to know that there is an easier method. But it’s a counterintuitive one. You can rename a file by “moving it” to a new file name.
$ mv august.png september.png
This actually does what you would think of as a “rename” command and in a single step. But it should be noted that it’s also the general “move” command. So, you are moving the file at the same time. If you move it to a different directory, it will be moved as well as renamed.
As with the cp command, you have to be aware of file extensions. For example, say we introduced a typo and typed:
$ mv august.png september.pg
That action would break our file.
File extensions and renaming
For the most part, Windows protects you from changing the extension of a file. When you click a file in Windows to rename it, it only highlights the actual name, not the extension. You need to do a little work to change the extension of a file.
Linux doesn’t protect you. The extension is part of the file name and can be changed at any time. For that reason, you should be very aware of file extensions. .JPEG is not necessarily the same as .JPG, even if they both denote “JPG” files.
If a file is no longer working after you’ve moved, renamed, or copied it, it’s very likely that it’s because of an error with the file extension.
How to rename multiple files in Linux
What if you wanted to rename multiple files in Linux? This is where we start to see the advantages of using a command-line system.
Let’s say we did make a mistake. We changed everything to JPGs, but instead, they need to be PNGs. We can do this like so:
$ mv *.JPG *.PNG
What this does is it uses a wildcard operator (*) to select any file that ends with JPG and changes that to a PNG. We had better know what we’re doing, though, because if there were any actual JPGs, they’re broken now.
Wildcards are one of the simplest forms of regular expressions. If we had wanted to rename the files in Windows, we would have had to click on them one by one. To rename the files in Linux, we can just use an expression to highlight them all. For that reason, power users in Linux can perform operations very quickly.
Renaming a directory in Linux
One of the quirks of Linux and Unix is that not much changes whether you’re using files or directories. A directory is more or less a blank file that contains other files. If we had a directory called “2021” and we wanted to rename it to “2022,” we would do this:
$ mv 2020 2021
It’s as simple as that. Existing directories are treated like existing files that do not have a file extension by the MV command.
Rename a file while moving it in Linux
Earlier, we noted that we are using the MV command to rename files and that we could also move the file to a different directory if we so chose.
Now let’s say we want to rename august.png to september.png and also move it into the 2022 directory.
$ mv august.png 2022/september.png
As again, both the directory and the file name (and its extension) are just pointers to where data is. This remains consistent throughout Linux.
Using flags (options) for your operations
Linux makes it possible to use something called “flags,” or options, with its commands. This controls how the command is performed.
Two commands you may want to know are -i and -v.
- -i. This is the interactive option, and it makes it possible for you to “interrupt” the sequence to confirm actions.
- -v. This is the verbose option, and it makes it possible for you to see everything that happens with the command as it happens.
Both of these option flags can be helpful when using the MV command in Linux.
Using the interactive renaming function in Linux
We’ve noted a couple of times that it’s possible to make large, potentially irrevocable mistakes in Linux. But there’s also a way to get around this. If you add the “-i” option with the MV command, you’ll be prompted before anything is done.
Let’s say we type:
$ mv -i august.jpg september.png
But the problem is, we’ve already created september.png. It already exists! The “interactive” function will let us know we’re about to overwrite an existing file. We can then type “Y” or “N” to either confirm the action or deny it.
If we knew that it was going to be overwritten anyway, we may be fine with it. But otherwise, we might want to know that there’s another file in danger.
Using the verbose option to track what’s happening
In addition to interactively interrupting your commands, you may also want to just know what operations are occurring. With Linux, you can always use the -v or -verbose option.
$ mv -v august.png september.png
This will tell you exactly what it has done to the source files and the new files. And you can also:
$ mv -v -i august.png september.png
This will tell you about all the operations and also allow you to interrupt them. Whenever you’re using a new command, consider using -v for verbose. The -v flag should work for nearly any command.
Mass moving and renaming files in Linux
Sometimes you need to move and rename a large number of files in Linux. Consider that you might have an entire directory of files that have to be either changed or moved. This could happen if you installed something into the wrong directory.
For this, Linux has another command altogether: MMV. MMV can be used to move, link, and append files. It’s a non-destructive method of doing so; it will try to identify any potential errors or collisions.
This is particularly valuable because you don’t want to accidentally ruin a lot of files at once.
$ mmv *.png *.jpg
The above would be another way of changing all PNGs to JPGs, in the event that for instance, a large number of files had the wrong extension. This could happen if you had copied the original file over incorrectly.
But you’ll notice that we could have done this with the MV command, too. The only difference is that MMV is a more careful way of doing this.
If you wanted to mass copy a lot of files, you could even copy over the entire directory:
$ cp 2021 2022
And that would copy 2021 to 2022 while still leaving 2021. Copying is less dangerous than moving because copying doesn’t inherently delete the original files. Moving does. This is one reason why when renaming a file, we looked at the CP function before the MV function — there is less that can go wrong with the operation because it’s not inherently destructive.
Installing rename in Linux
At this point, you might be thinking “Why can’t I just rename?” Actually, you can. There is a “rename” command in Linux, but you would need to install it.
On your administrative account for Ubuntu or Debian:
$ sudo apt install rename
For Fedora or CentOS:
$ sudo yum install rename
(If you have a different distribution, look up your installation syntax.)
This will install the “rename” package. After you install rename, you’ll be able to use complex regular expressions to rename your files.
Complex regular expressions are a topic in and of themselves. We saw that * can be used as a wildcard. But with regular expressions, we can also select things like “any file that starts with letters but ends with numbers” or “any file that has 59 in it.” Regular expressions can be used to identify email addresses or other patterns — they are very powerful but take some practice.
The rename command will not operate much differently from MV, MVV, or even CP. The difference is that you’ll have a dedicated function called “rename” that you can use. There are many function packages for different Linux distributions that you can install. If you’re trying to complete an operation and you can’t find it in vanilla Linux, consider looking for a package.
Getting started with Linux
Linux has an internal manual ($ man) that can be used to look up commands. To look up more information about the move command in Linux, you can also always type in:
$ man move
Or look up the $ man entries for anything else.
Linux requires a lot of memorization. For the most part, you just need to know what’s possible in the system. Once you know what’s possible, you should be able to reference the manual to figure out how.
But you should always remember that Linux does what it’s told — there are fewer safeguards with the Linux system. While Windows might alert you that you’re overwriting a file, Linux isn’t likely to alert you unless you specifically ask that it does. If you’re just learning Linux now, a great way to do so is through a Linux bootcamp or workshop. There are so many distributions of Linux, you’ll also want to decide which distribution you want to use first. Check out our guide to finding the best Linux distro!
ARGV Perl Command Lines
Ifconfig DHCP Command Line Network Settings
How to Handle Command Line Arguments in Python
Learn How to Copy a Directory in Linux
Top courses in Linux
Linux students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.