SOLVED: How to PROPERLY rename file(s) in Linux


Tips and Tricks

Renaming files can feel like a daunting task if you're new to Linux. Luckily there are plenty of ways to change the names of your files and folders. The steps are easy if you're running a GUI (graphical user interface), such as Nautilus or Dolphin. If you’re using the terminal (command-line interface), there is a more complicated way to rename your files and folders. The process will be fast and efficient no matter which method you choose! If you're not sure how to rename files in Linux, don't panic! This post will give you a step-by-step guide on renaming files on different Linux distributions.

 

Rename a Single File with the mv Command

The mv command (short form of move) plays two main roles on Linux system:

  • Rename Files
  • Move a file from one location to another

The basic syntax of the mv command is as follows:

mv [options] [old_file_name] [new_file_name]
or
mv [options] [file_name] [new_destination]

We will execute the command below to rename a single file called 'my_cv.pdf' to 'John_Doe_CV.pdf.

mv my_cv.pdf John_Doe_CV.pdf

SOLVED: How to PROPERLY rename file(s) in Linux

Renaming a single file with the mv command is pretty easy, as you can see in the example above. However, the stress starts when you try renaming multiple files with the mv command.

 

Rename Multiple Files with the mv Command

To better understand why it gets complicated when you try renaming multiple files with the mv command, let's try the example below. We will try renaming the files 'image1.jpg,' 'image2.jpg,' and 'image3.jpg' to 'image1.png,' 'image2.png,' and 'image3.png.' We will use the syntax below.

mv [file_names] [new_file_names]

SOLVED: How to PROPERLY rename file(s) in Linux

From the image above, you can see we are getting the error, "mv: target 'image3.png' is not a directory."  Why is that? When you pass in several files in the mv command, it will understand that you are trying to move these files to a new location which is the last argument passed. That's why in the above error, the mv command specifies that the 'image3.png' is not a directory.

From the above example, we can deduce that we cannot use the mv command alone to rename multiple files, and we will need to use it in conjunction with other commands like find, for loops, and while loops. Take a look at the example below.

for f in *.jpg; do mv -- "$f" "${f%.jpg}.png"; done

SOLVED: How to PROPERLY rename file(s) in Linux

From the results in the image above, you can see we successfully rename all the '.jpg' files in the directory to '.png.' If you are not well versed with bash scripting, you might be wondering how that one-liner command works. Well, let's do a simple walkthrough.

 

Understanding the mv Script to Rename Multiple Files

To start with, let's write that one-liner script in a better readable format.

for f in *.jpg; 
    do
      mv -- "$f" "${f%.jpg}.png"
done

Before going through the script line by line, you need to know that the part '*.jpg,' means that we are looking for any files file that have a '.jpg' extension.

  • The first line, 'for f in *.jpg;' loops through the directory searching for any files that have a '.jpg' extension.
  • The second line, 'do' specifies the start of the action on all the files found by line one (body of the loop).
  • The third line renames all the files from the '.jpg' extension to '.pngg.' The "${f%.jpg}.png" is a shell parameter expansion to rename only the file extension.
  •  The fourth line, 'done' specifies the end of the loop.

From the example above, it's evident that renaming multiple files with the mv command can be pretty hectic. That brings us to a new utility called Rename.

 

Rename Files with the rename Command

A much better solution for renaming multiple files is the rename command. However, it also requires some basic knowledge of regular expression to get the maximum out of it. There are two versions of the rename command, each using a different syntax. For this post, we will use the Perl version. To install the utility, execute the command below depending on your distribution.

Debian-based distributions (Ubuntu etc.)

sudo apt install rename

SOLVED: How to PROPERLY rename file(s) in Linux

RHEL-based distributios (RHEL, CentOS, Fedora)

sudo yum install prename

ArchLinux

yay perl-rename
or
sudo pacman -Syu perl-rename

The basic syntax of the rename command is as follows:

rename [options] 's/[filename_element]/[replacement_element]/' [fileName]
  • rename: This parameter invokes the rename command. In other distributions, you might need to use prename.
  • options: These are the arguments that you can specify to change/ manipulate the output of the rename command.
  • filename_element: This parameter represents the part of the filename you wish to replace.
  • replacement_element: Thi parameter represents the replacement
  • fileName: This parameter specifies the target file you want to rename

Using the syntax above, let's use the same method to rename the image files as we did in our first example in this post. We will use the command below.

rename 's/.jpg/.png/' *.jpg

SOLVED: How to PROPERLY rename file(s) in Linux

From the image above, you can see we successfully renamed all '.jpg' files to '.png.' Before looking at more rename command examples, let's look at the various options you can use with the rename command.

 

rename Command Options

  • -a: Replaces all instances of the filename element rather than just the first.
  • -f: Overwrite existing files: this option allows existing files to be overwritten.
  • -h: This parameter shows the help page. You can also use --help.
  • -i: This parameter allows the program to prompt you before renaming any files
  • -l: The last occurrence of the filename element is replaced instead of the first.
  • -n: Performs a dry run with no permanent modifications. The output is best when coupled with the verbose output (-v).

Let's now look at several rename command examples.

1. Rename File Extensions

Let's rename the files below from '.txt' to '.pdf.'

fileOne.txt
fileTwo.txt
fileThree.txt

We will use the command below:

rename 's/.txt/.pdf/' *.txt

SOLVED: How to PROPERLY rename file(s) in Linux

 

2. Rename Part of a Filename

We want to rename the files fileOne.pdf, fileTwo.pdf, and fileThree.pdf to documentOne.pdf, documentTwo.pdf, and documentThree.pdf. We will use the command below. We will also use the -v (verbose) parameter to see what we are doing.

rename -v 's/file/document/' *.pdf

SOLVED: How to PROPERLY rename file(s) in Linux

 

3. Delete/ Remove Part of a Filename

We want to rename the files fileOne.pdf, fileTwo.pdf, and fileThree.pdf to one.pdf, two.pdf, and three.pdf. We will use the command below.

rename 's/file//' *.pdf

SOLVED: How to PROPERLY rename file(s) in Linux

 

Conclusion

Up to this point, you should now be able to rename files with much ease on Linux systems. If you want to rename a single file, the mv command will come quite in handy. However, if you are dealing with several files, we highly recommend the rename command. Do you have any questions or suggestions regarding this topic? Well, let us know in the comments below.

 

Further Reading

man page for rename command

 

Deepak Prasad

Deepak Prasad

Deepak Prasad is the founder of GoLinuxCloud, bringing over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, Networking, and Security. His extensive experience spans development, DevOps, networking, and security, ensuring robust and efficient solutions for diverse projects.

Certifications and Credentials:

  • Certified Kubernetes Application Developer (CKAD)
  • Go Developer Certification
  • Linux Foundation Certified System Administrator (LFCS)
  • Certified Ethical Hacker (CEH)
  • Python Institute PCAP (Certified Associate in Python Programming)
You can connect with him on his LinkedIn profile and join his Facebook and LinkedIn page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment