Rename a File in Linux – Bash Terminal Command
Zaira Hira
Renaming files is a very common operation whether you are using the command line or the GUI.
Compared to the GUI (or Graphical User Interface), the CLI is especially powerful. This is in part because you can rename files in bulk or even schedule the scripts to rename files at a certain point in time.
In this tutorial, you will see how you can rename files in the Linux command line using the built-in mv command.
How to Use the Linux mv Command
You can use the built-in Linux command mv to rename files.
The mv command follows this syntax:
Here are some of the options that can come in handy with the mv command:
- -v , —verbose : Explains what is being done.
- -i , —interactive : Prompts before renaming the file.
Let’s say you want to rename index.html to web_page.html . You use the mv command as follows:
Let’s list the files and see if the file has been renamed:
How to Name Files in Bulk Using mv
Let’s discuss a script where you can rename files in a bulk using a loop and the mv command.
Here we have a list of files with the extension .js .
Next, you want to convert them to .html .
You can use the command below to rename all the files in the folder:
Let’s break down this long string to see what’s happening under the hood:
- The first part [ for f in *.js ] tells the for loop to process each “.js” file in the directory.
- The next part [ do mv — «$f» «$
.html ] specifies what the processing will do. It is using mv to rename each file. The new file is going to be named with the original file’s name excluding the .js part. A new extension of .html will be appended instead. - The last part [ done ] simply ends the loop once all the files have been processed.
Conclusion
As you can see, renaming files is quite easy using the CLI. It can be really powerful when deployed in a script.
How Do I Rename a File in the Ubuntu Terminal?
However, to use effectively this CLI application, you should have strong knowledge about basic Linux commands and fundamentals such as create, delete and renaming an existing file. Different commands are available in the Ubuntu Linux system to rename a file that we will explore in this article.
We will provide comprehensive details in this tutorial on how you can rename a file in Ubuntu using the command-line application Terminal. All commands have implemented for the demonstration on the Ubuntu 20.04 Linux system.
Renaming Files in Ubuntu 20.04 LTS System Using Terminal
The two different commands ‘mv’ and ‘rename’ are available in the Ubuntu Linux system to rename a file via terminal or command-line approach. Let us discuss each command in detail.
Rename File in Ubuntu Using the mv Command
Before using the ‘mv’ command, you should know how it works on your system. The basic syntax of the ‘mv’ command is given below:
The most popular ‘mv’ command options are provided below:
-f – Displays no message or alerts before overwriting a file name.
-i – Displays prompt confirmation or warning messages before renaming a file.
-u – It moves a file if the file does not exist on the specified destination or in case of a new file.
The file source can be the destination of one or more files. The destination only represents a single file.
Example
For example, to rename the file ‘testfile1.txt’ to ‘testfile2.txt, you need to run the following command:
How to Use the mv Command to Rename Multiple Files?
Usually, you can only rename a single file using the move command. To rename multiple files using the mv command, you can use the mv command to combine with different commands. Let us say, mv command can be used along with for loop, while loop, and find command.
Let us explain with the help of an example. Here, we want to rename all .txt extension files of the current directory replaced with another .html extension. In this case, the following code will help us:
The above code will iterate using for loop through the files list having the .txt extension. After that, in the second line, it will replace each file extension .txt with .html. In the end, ‘done’ indicated the end of the for loop segment.
How to Use the Rename Command to Rename Files in Ubuntu?
Using the rename command, you can rename multiple files of a current directly at once. This command contains more advance features as compared to the ‘mv’ command. For renaming files using the rename command, you should have basic knowledge about regular expressions usage.
In most Linux distributions, the ‘rename’ command is installed by default. However, if you have not installed the rename command on your Ubuntu system then, it can be easily installed on Ubuntu and its derivatives by running the following command:
Syntax of Rename Command
Using the following syntax, you can use the rename command:
The rename command will rename files according to the specific regular Perl expressions.
Example
In the following example, we want to change the extension of all text files. So, we will change or replace all files with extension .txt to .html by executing the following command:
When you use the rename command followed by option ‘-n’, it also displays the file names to be renamed and renaming them as follows:
The above command displays the following result on the terminal window:
By default, the rename command does not overwrite an existing file. However, if you pass option -f along with the rename command then, it will help you to overwrite the existing files. Execute the following command to use the rename command followed by the -f option:
To change or rename the file name using rename command use the following command:
Example
For example, we want to rename a single file with the name ‘testfile.txt’ to newtestfile.txt. In this case, the above command will be modified into the following form:
To see more usage of rename command, let us try the following examples:
For example, to rename all those files, which contain spaces in file name and you want to replace it with underscores. In this case, the rename command will help you in the following way:
Using rename command, you can convert the file name in all lowercase letters as follows:
Similarly, to convert the file name to all uppercase letters, use the following command:
To explore more options and uses of rename command, type the following terminal command:
Conclusion
We discussed in this article how to rename files in Ubuntu 20.04 LTS distribution using the terminal application. Moreover, we explored the working and uses of the ‘mv’ and ‘rename’ commands for renaming a file. From the above discussion, we concluded mv command is useful for renaming a file but, rename command offers more advanced options for file renaming in the Ubuntu system.
About the author
Samreena Aslam
Samreena Aslam holds a master’s degree in Software Engineering. Currently, she’s working as a Freelancer & Technical writer. She’s a Linux enthusiast and has written various articles on Computer programming, different Linux flavors including Ubuntu, Debian, CentOS, and Mint.
How to rename a file in Terminal? [duplicate]
How do I rename my_static to static and remove static (2)?
2 Answers 2
That command is used for moving and renaming files and directories.
A simple way to rename files and folders is with the mv command (shortened from “move”). Its primary purpose is moving files and folders, but it can also rename them since the act of renaming a file is interpreted by the filesystem as moving it from one name to another.
where “file1.ext” is the “old” name of the file, and “file2.ext” the new name.
How to Rename Files in Linux
Linux provides several options for renaming files, including using the GUI and multiple dedicated terminal commands. This makes it relatively easy to rename individual files, but it can be challenging to rename multiple files at once.
In this tutorial, we will go over different commands you can use in the Linux terminal to rename files in Linux.
- A system running a Linux distribution
- An account with sudo privileges
- Access to the terminal window/command line
- Access to a text editor, such as Vim or Nano
Rename Files with the mv Command
The Linux mv (move) command is used to move files and directories from the terminal. It uses the following syntax:
If you specify a directory as the destination when using the mv command, the source file moves to that directory. If the destination is another file name, the mv command renames the source file to that name instead.
Note: Learn more about using the mv command in our guide to moving directories in Linux.
Rename a Single File with the mv Command
Using the mv command with its default syntax allows you to rename a single file:
For example, if we want to rename example1.txt into example2.txt, we would use:
Since there is no output if the command is successful, we are using the ls command to check if the name is changed:
Rename Multiple Files with the mv Command
On its own, the mv command renames a single file. However, combining it with other commands allows you to rename multiple files at the same time.
One method is to use the find command to select multiple files with a similar name, then use the mv command to rename them:
Using this syntax, the find command defines an element of the current file name as the search parameter. Next, -exec executes the mv command on any files that match the search, changing their current filenames to the new one.
For instance, if we have example1.txt, example2.txt, and example3.txt and want to change the extension to .pdf:
Another method is to use the mv command as a part of a <strong>for</strong> loop in a bash script.
Using the same example, start by creating and opening a bash script file using a text editor such as Nano:
Add the following lines to the script:
In the script above:
- The first line instructs the script to search for all the files in the current directory ending with .txt.
- The second line uses the mv command on each file found to replace the .txt extension with .pdf.
- The third line ends the loop segment.
Press Ctrl+X, then type Y and press Enter to save the changes to the script and exit.
Use the sh command to execute the script:
Note: Learn how to compare two files using the diff command.
Rename File with the rename Command
The rename command is used to rename multiple files or directories in Linux. It offers more features than the mv command but can be more challenging to use since it requires basic knowledge of Perl expressions.
How to Install the rename Command
On many Linux distributions, the rename command is not available by default. If your system is missing the rename command, install it with:
- For Ubuntu and Debian, use sudo apt install rename
- For CentOS and Fedora, use sudo yum install prename
- For Arch Linux, use sudo pacman -S rename
rename Command Syntax and Options
There are three types of Perl regular expressions: match, substitute and translate. The rename command uses substitute and translate expressions to change file and directory names.
Substitute expressions replace a part of the filename with a different string. They use the following syntax:
With this syntax, the command renames the file by replacing the first occurrence of the filename element with the replacement. In the command above:
- rename : Invokes the rename command.
- [options] : Provides an optional argument that changes the way the command executes.
- s : Indicates a substitute expression.
- [filename element] : Specifies the part of the filename you want to replace.
- [replacement] : Specifies a replacement for the part of the current filename.
- [filename] : Defines the file you want to rename.
A translate expression translates one string of characters into another, character for character. This type of expression uses the following syntax:
An example of a rename command using a translate expression:
In this example, every a character in the filename is replaced by an x, every b by a y, and every c by a z.
The rename command uses the following options:
- -a : Replaces all the occurrences of the filename element instead of just the first one.
- -f : Forces an overwrite of existing files.
- -h : Displays the help text.
- -i : Displays a prompt before overwriting existing files.
- -l : Replaces the last occurrence of the filename element instead of the first one.
- -n : Performs a dry run, making no permanent changes. Best combined with the verbose output ( -v ).
- -s : Renames the target instead of the symlink.
- -v : Shows a verbose version of the output.
- -V : Displays the command version.
rename Command Examples
1. Change File Extension
Returning to our last example, to change the file extension from .txt to .pdf, use:
2. Replacing a Part of a Filename
Replacing a different part of the filename follows the same syntax. To rename example1.txt, example2.txt, and example3.txt to test1.txt, test2.txt, and text3.txt, use:
3. Delete a Part of a Filename
The rename option also allows you to delete a part of the filename by omitting the replacement part of the expression. For instance, if we want to shorten example into ex:
4. Rename Files with Similar Names
Another use for the rename option is to rename files with similar names. For instance, if we want to rename files with example and sample in their name to test:
5. Rename Files Character-by-Character
The rename command also allows you to use translate expressions to rename files on a character-by-character basis. For instance, if you want to rename multiple files named example file by replacing the blank space with an underscore (_):
6. Convert Lowercase Characters
To convert lowercase characters in filenames into uppercase characters, use:
7. Convert Uppercase Characters
The reverse also works if we switch the order of the uppercase and lowercase characters in the expression:
Note: Be careful when changing the character case, as this also changes the file extension.
After reading this tutorial, you should be able to rename files using the mv and rename commands in Linux.