Hence, it is a valuable approach for users to maintain the order and control over their digital assets. However, as a Fedora beginner, you may not know how to rename the files using the commands. In this article, you will get to know the ways to rename a file from the terminal in Fedora Linux.
How to Rename a File from the Terminal in Fedora Linux
Although there are various ways to rename the files, using the “mv” and “rename” commands are the simplest commands that you can use for it. Let’s take some examples of using them:
The Mv Command
Although there are various ways to rename the files, using the “mv” and “rename” commands are the simplest commands that you can use for it. Let’s take some examples of using them:
ls -l
As you can see in the previous image, the “Documents” directory has “Fedora.pdf”, “Fedora.txt”, and “Linux.txt” files. Now, we rename the file named “Linux.txt” to “Linuxhint.txt”.
Rename Multiple Files
If two files in the same directory have the same extension, you can change their extension using the “mv” command. Here is how you do it:
As we can see in the given example, we have two files of the “.txt” extension (“Fedora.txt” and “Linux.txt”). Here, we change the names of these two files by changing their extensions with the following “mv” command:
In the previous command:
- find → It searches or finds an element of the current directory or file.
- -exec → It executes the “mv” command on files that are similar to the search and converts the current filename with the new one.
Instead of the previous command, you can also use the following command to change the extension of multiple files and rename a file:
In the given example, we changed the “.txt” extension to “.html”.
Rename Multiple Files Using the Bash Script
You can change multiple filenames using the Bash script by changing their extensions. You need to create a Bash file and add the following lines to it:
for f in *.<current file_extension>; do
mv -- "$f" "${f%.<current file_extension>}.<new file_extension>"
done
For example, we change the file extension from “.txt” to “.png”.
As you can see in the previous image, there are three files in which two of them have a “.txt” extension. Here, we change the extension of the files from “.txt” to “.png”.
Add the following lines in the Bash script:
for f in *.txt; do
mv -- "$f" "${f%.txt}.png"
done
Using the following “sh” command, save and run the script as follows:
The previous command does not give you any output but changes the file’s extension.
The Rename Command
The “rename” command is more advanced than the “mv” command. However, it is not a pre-installed utility in Fedora, so you need to install this first using the following command:
Now, you can rename the files with the same extension:
Conclusion
This is all about the simple commands that you can try to rename a file from the terminal in Fedora Linux. The “rename” command is more advanced and user-friendly than the “mv” command, but it is not pre-installed in Linux, so you must install it manually. Overall, both commands are helpful and as a beginner, you can use them to rename your files in your Fedora system.