Renaming a file in Linux is easy, once you know how. You can do it from either the command-line or using the GUI, though the latter is faster and uses fewer resources.
In this tutorial, I’ll show you how to rename not just one file, but multiple files at the same time. In addition, I’ll show you some alternative tools that you can use to rename Linux files to overcome some limitations of the default rename command, as well as to take advantage of some powerful features for advanced users. Let’s say you just bought one of our VPS packages and we are looking to get your hands dirty, this should be the perfect guide!
So let’s get started!
How to Rename a File in Linux – Command Line
The basic syntax to rename a file in Linux is simple:
mv old_filename new_filename
In the screenshot below, I change the name of “testfile” to “newtestfile”:
And that’s it. And in case you’re wondering, yes – “mv” stands for “move” and this command is also used to move a file in Linux from one location to another with a different name. However, this doesn’t mean that the system is destroying the file and recreating it again with a new name. When the “mv” command is used to rename a file, the system is smart enough to realize that the source and destinations are the same, and it doesn’t waste resources. It simply changes the metadata to reflect the new name.
In fact, this behavior isn’t limited to just moving files within the same directory. Linux changes the metadata of the file to point to the new location even when using the “mv” command to move the file to a new location.
The Command for Renaming Directories is the Same
If you want to rename a director instead of a file on Linux, the command is the same. This is because, internally, Linux treats files and directories the same. A directory is simply a file containing the names of other files. So you can use the “mv” command in the same way to rename an entire folder.
Common Flags for the “mv” Command while Renaming Files
The “mv” command in Linux has a number of flags. While using “mv” to rename files, not all these flags are useful. However, here are the ones that are:
-i or –interactive
If you’re renaming a file, and an existing file already exists with the new name, then the “-i” flag will ask you for confirmation whenever there’s a conflict between the new filename and an existing one. If you press “y” during the prompt, Linux will overwrite the existing file.
Here’s an example:
mv -i old_name new_name
-n or –no-clobber
The “-n” flag ensures that Linux never overwrites existing files if they exist with the new filename. This is useful to ensure that you don’t accidentally mess up and delete an existing file. Here’s what the command looks like:
mv -n old_name new_name
-v or –verbose
The verbose command gives you detailed information about what’s happening, which files were renamed, and what the new filename is. You can use it like this:
mv -v old_name new_name
Here’s the output of the above command in verbose mode:
As you can see, “mv” is aware that you’re only trying to rename a file and it tells you as much. So if you want to ensure that your file renaming operation is being carried out according to your instructions, the verbose mode can help.
Renaming Multiple Files in Linux
You can use the “mv” command to rename multiple files at the same time using a loop. For example, let’s say you want to iterate through all the text files and append the word “new_” to each file with the “.txt” extension.
The following command will accomplish this:
for file in *.txt; do mv "$file" "new_$file"; done
The above command is a “for do” loop that identifies all files with the extension “*.txt”. It then uses the $file variable to craft successive “mv” commands that append the string “new_” to each file. Running the above command, I get this:
As you can see, if you have a pattern for the files that you want to target, you can rename all of them at the same time without needing to do it individually. This kind of power is typical of the Linux command line. If you wanted to achieve the same result using a GUI, it wouldn’t have been possible.
Using Alternative Command Line Tools to Rename Linux Files
While the “mv” command is quite powerful and, as seen above, we can use it in conjunction with “for” loops to target files that match a certain pattern, the operation is still limited in terms of what it can target. For example, it can’t target file size. If we need to quickly rename files above a certain size limit, then a simple for loop wouldn’t do the trick.
Using “find” in Conjunction with “mv”
The “find” command is able to target files with a much larger set of attributes. For example, let’s say I want to identify all files above a certain size limit – 1 MB for example – and rename them. The following code will do just that:
find . -type f -name "*.txt" -size +1M -exec mv "{}" "${file%.txt}_large.txt" \;
The above code will identify all files with a size greater than one MB, thanks to the “-size” flag, and then use the “exec” command to invoke “mv” and append the text “large” to the first portion of the file name.
The “find” command can also target other attributes that we can’t otherwise easily target using a regular “for” loop. For example, we can isolate the following types of files and folders:
- Files that have been modified recently
- Directories instead of files
- Files that have a specific set of permissions
- Files that have a specific type of ownership
As you can see, the range of flags you can use with “find”, makes it much more powerful compared to a vanilla “for” loop.
Using “rename” Instead of Find
If you want to use regular expressions to find and rename files, you can still use “find”, but the commands grow progressively more complicated. If you want to consistently use regular expressions in your renaming operations, I suggest you use the “rename” utility instead. There are several, but you can install the Perl rename utility on Ubuntu using the following command:
sudo apt update sudo apt install rename
After installing rename, you can use regular expressions to match specific parts of a file and rename it. For example, let’s say you have some files like this:
- file_01.txt
- file_02.txt
- file_03.txt
And you want to rename them to:
- file1.txt
- file2.txt
- file3.txt
In other words, we want to get rid of the “_0” in the file names. To do this with the “rename” utility, we use:
rename 's/file_0/file/' file_*.txt
This simple command matches “file_0” and replaces it with “file” for all files exhibiting this pattern. Doing the same thing using “find” would be a lot more complicated!
Conclusion
Renaming files in Linux using the command line is easy. The process can be as simple as a single “mv” command, or it can get progressively more sophisticated based on your needs. If you need to use regular expressions, then the “rename” command is your best bet since it improves the clarity of the command, and you’re less likely to make mistakes.
I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!
Leave a Reply