In Linux, the Command-Line Interface makes it easy to rename our files and is more powerful. With the CLI we can run scripts and change the file names in the future for our ease and need. In Linux/Ubuntu, we have multiple commands which we can use to rename our files.
This article explains different commands to rename files in Ubuntu 22.04.
How to Rename Files in Ubuntu 22.04 Using the Command Line?
Linux provides us with various commands to rename our files, we will go through each of them.
Using the “mv” Command to Rename Files
The “mv” command in Ubuntu 22.04 is used to move or rename files, having the syntax:
The “options” statement works as a flag and you can specify a specific use or need. You can display the names of renamed directories with the flag “-v” and create a backup using the flag “-b”. Similarly, other flags can be used. Using the “mv” command, if the name of the source and destination of a file are the same, the “mv” command renames the source file. Consider the following example where we only have one file “myFile1.txt” in the current directory:
To rename this file, we can use the command:
As the destination is the same as the source, it will rename the existing “myFile1.txt” file:
Bonus Tip 1: Use “mv” Command to Rename a Directory
Using the “mv” command, you can also rename your directory. In the example below, we have a directory “folder”:
To rename this directory, use the current directory name followed by the new name of the directory with the “mv” command:
As it is in the same directory, it will change the directory name:
Renaming Files Using “rename” Command
Files in Ubuntu can also be renamed using the “rename” command. By default, the “rename” command is not installed in Ubuntu, so to install “rename”, use the following “sudo” command:
This will download and install the necessary “rename” packages:
The syntax to use the “rename” command is:
The “rename” command will rename the “filename element” with the “replacement”. The “s” before “filename element” is used for substituting whereas “options” is used as an additional argument for command execution. Multiple Files can be renamed with the “rename” command as well.
Renaming Multiple Files Using the “Rename” Command
In the example below, we have three files in our directory and they can be seen on the terminal as well:
To change the filenames of these text files, use the command:
The above command when executed, will rename all the files having the extension “.txt”:
The rename command can also be used for multiple purposes apart from only renaming our files. It can change the file extension, convert to lower and uppercase, and delete a part of our filename.
Bonus Tip 1 – Changing the File Extension Using “rename”
To change the file extension of our files from “.txt” to “.pdf” in the above example, use the “rename” command as follows:
The command when executed will change the file extension of our files:
Bonus Tip 2 – Converting Filename to Uppercase Using “rename”
Converting File Names to uppercase involves the same syntax as above. Using our previous example, renaming to uppercase, use the command:
Bonus Tip 3 – Converting Filename to Lowercase Using “rename”
Converting File Names to lowercase involves the same syntax as above. Using our previous example, renaming to uppercase, use the command:
Bonus Tip 4 – Delete Part of a Filename Using “rename”
The rename commands also let us delete a part of our file name. Using our previous example, to delete a part of a filename, use the command:
This will remove the “re” part from our “renamed” string:
Bonus Tip 5 – Rename the Directory Name Using “rename”
Directories can be renamed as well using the “rename” command. In the example below, we have a directory “myFolder”:
The syntax to change a directory name using the “rename” command is:
It will change the directory name:
That’s all about renaming files in Ubuntu 22.04 using the command line.
Conclusion
The “rename” and “mv” commands are quite useful for renaming our files in Ubuntu using Command Line. Using the rename command we can also delete part of the file name, convert filenames into uppercase or lowercase, change the file extensions, and much more. This article has explained how we can use both commands in Ubuntu to change the filenames of already existing files in our directory.