Fedora

How to Rename a File form the Terminal in Fedora Linux

Renaming a file is an easy activity that you can do for various reasons such as file organization, accessibility, and avoiding conflicts between files. You can also rename a file in the directory to quickly find it in hundreds of files.

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:

cd ~/Documents
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”.

mv Linux.txt 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:

find . -depth -name "*.<current_extension>" -exec sh -c 'f="{}"; mv -- "$f" "${f%.<present_extension>}.<new_extension>"' \;

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:

find . -depth -name "*.txt" -exec sh -c 'f="{}"; mv -- "$f" "${f%.txt}.png"' \;

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:

for f in *.txt; do mv -- "$f" "${f%.txt}.html"; done

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:

#!/bin/bash
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:

#!/bin/bash
for f in *.txt; do
    mv -- "$f" "${f%.txt}.png"
done

Using the following “sh” command, save and run the script as follows:

sh filename.sh

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:

sudo yum install prename -y

Now, you can rename the files with the same extension:

rename <current_extension> <new_extension> *.<current 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.

About the author

Prateek Jangid

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.