rename file in ubuntu

Like any other platform, renaming a file in Ubuntu is generally a simple task. You can rename files from the file manager, or use CLI tools like mv. Batch renaming multiple files is a slightly different story. 

If you’ve got simple criteria for renaming (e.g., rename all files in a directory to file1.txt, file2.txt, and so on), renaming is still easy. But if you have complex criteria for renaming files, some scripting knowledge is required.   

Rename Files Graphically

Standard Ubuntu installations ship with GNOME and the Nautilus file manager. Other file managers like KDE’s Dolphin or Xfce’s Thunar are even more flexible, but Nautilus is still fairly capable of batch-renaming files.

For starters, right-click a file and select Rename, or select the file and press F2 to rename a single file.

To rename multiple files together, select the files and press F2. Now, press + Add and check out the various templates available.

  • You can keep the original file names and add new text before or after the original name.
    rename multiple files ubuntu
  • Or, you can add ordered numbers to the filenames sorted based on the selected criteria.
    rename file automatic numbering
  • You can also clear the old filename and rename all the files to something new. For instance, users often want to rename all their vacation photos to simpler filenames like photo1.png, photo2.png, etc.
    rename file ubuntu gnome

If the files follow a certain pattern, and you need to replace parts of the text, use the Find and Replace option.

If you want to batch rename files via the GUI but your file manager isn’t quite cutting it for you, there are great bulk renaming utilities available like Metamorphose or Pyrename. Give a few of them a try and see which one feels like a good fit for you.

Rename Files with Move (mv)

The filesystem interprets your renaming a file as moving it from one handle to another. This is why the mv command is normally used to rename files from the terminal.

Rename Single File

The basic syntax for renaming files with mv is 

mv oldfilename newfilename
mv oregon.png ogtrail.png

You do have to be careful about causing data loss when renaming files with mv. For instance, if a PNG file named ogtrail already existed in the current directory, it would get overwritten in the above example.

You can rename directories in a similar manner too. But do note that if the new name is a directory that already exists, the directory you’re trying to rename will be moved inside the existing directory. After all, you can’t have two files or directories with the exact same name in the same location.

Script to Rename Multiple Files

You can also create a simple script to rename multiple files at once. For instance, the following script uses the for statement to find all .txt files and change their extension to .pdf.

for file in *.txt; do
mv -- "$file" "${file%.txt}.pdf"

Use the Rename Command

Rename uses the substitute and translate Perl expressions to replace parts of a filename. It’s better suited for mass renaming files compared to mv. 

Let’s start with the basic syntax for replacing specific parts of a filename.

rename ‘<expression>/<oldtext>/<replacementtext>/’ <filestorename>

Replace Characters to Rename File

The following example substitutes the string jan with feb in all the pdf files. A file named janreport.pdf would be renamed to febreport.pdf.

rename ‘s/jan/feb/’ *.pdf

You can also add flags like i (case-insensitive), g (global), etc like so

rename ‘s/jan/feb/ig’ *.pdf

For instance, let’s say you have a file named Jan-report-jan. Without the g flag, only the first jan would be replaced with feb. And the i flag ensures that the strings are replaced regardless of case.

Remove Certain Characters

Sometimes, you need to remove certain characters from files instead of replacing them. For instance, let’s say you have a bunch of photos named IMG202303201, IMG202303202, and so on. To remove the IMG part, you could use 

rename 's/IMG//' *

Or let’s say the files are named IMG20230320-IMG1 and so on. In this case, you might only want to remove the first IMG string and not the second one. You can use ^ for this like so

rename 's/^IMG//' *
Anup Thapa

Senior Writer

Anup Thapa is a Linux enthusiast with an extensive background in computer hardware and networking. His goal is to effectively communicate technical concepts in a simplified form understandable by new Linux users. To this end, he mainly writes beginner-friendly tutorials and troubleshooting guides. Outside of work, he enjoys reading up on a range of topics, traveling, working out, and MOBAs.