How To Rename Files & Folders In Linux | Complete Tutorial

Whether you’re a veteran Linux user or just picked up a distro like PopOS or Ubuntu, you still need to know how to manage your files and folders. That means knowing that there are many ways you can rename directories or files in Linux. In the Linux world, folders are called directories, too. They’re interchangeable.

There are two scenarios when renaming files and folders. Either you’re renaming a single file or folder, or you want to rename many files or folders at once. Just like Windows or Mac, Linux has several ways you can do either.

Rename a Single File or Directory in Linux Using File Manager

Most distributions, or distros, of Linux have a graphical file manager similar to File Explorer in Windows or Finder in MacOS. Most of them will function in the same way, but there may be differences.

Rename Using Rename

  1. Right-click on the file or folder.
  2. Select Rename or press F2.

  1. The name field of the file or folder becomes editable. Make the change you want and select the Rename button or press Enter.

Rename Using File Properties

This method is odd and you’re unlikely to use it, but it’s still possible.

  1. Right-click on the file or folder.
  2. Select Properties or press Ctrl + I.

  1. Select the name field and make edits. Then close the Properties window and the file or folder is renamed.

Rename Many Files or Folders in Linux Using File Manager

This feature may not be available in all the file managers available in the different distros of Linux. This is in PopOS.

  1. Select multiple files or folders and then either right-click and select Rename or press F2.

  1. You can Rename using a template or Find and replace text.

  • Rename using a template allows you to do things like sequentially number files and folders or add text to the front, back, or both sides of the original filename.

It can apply the template to the files based on the original name or modified date.

  • Find and replace text allows searching out a specific sequence of text and replacing it with something else. This is great for correcting spelling mistakes.

Get Help in Linux

There are a lot of ways to use the commands and utilities below. If you’re not sure what you need to do, ender the command man (for manual) and the command or name of the utility you need help with. For example, man mv will show the manual for using the mv command.

After renaming files or directories in Linux, always check them by either looking in the File Explorer or using the ls command to list them.

Rename a Single File or Folder with the MV Command

The MV command is for moving files and folders, yet it works well for renaming too. The syntax for the MV command is: mv [OPTIONS] source destination

  1. Navigate to the folder where files or folders you want to rename are located.

  1. Use the mv command to rename the folder or file. If the name has spaces in it, surround the name with quotes. Let’s take the 01- off the name.

Type in mv “01-Work Documents” “Work Documents” and press enter.

Listing the files shows it’s renamed.

Rename Multiple Files or Folders Using Bash Script

To create a bash script, you need to work in a plain text editor. Let’s say we had several HTML files that we accidentally saved as plain text files. We need to change the file extension from .txt to .html. We can use this bash script to rename them:

for file in *.txt; do
mv — “$file” “${file%.txt}.html”
done

  1. Enter that in the text editor and save it as rename-txt.sh in the same folder as the files to change.

  1. In the terminal, navigate to that folder and enter the command bash rename-txt.sh and press Enter.

  1. Check using ls or look in the File Manager to see if it worked.

How did that work? The first line is looking for any file that ends in .txt. The asterisk (*) is a wildcard, so anything before .txt in a filename will match. The do tells it to do the command as long as there are matching files. This is a loop. The second line has the mv command.

The double-dash () tells it there are no options for the command, get ready for some regular expression or regex. The $file is a variable that tells it to work with any file picked up by the first line. The % tells it to replace the .txt if it’s at the tail of the name with the value outside the curly bracket, which is .html.

How to Safely Rename Files and Folders with Linux Utilities

The rest of the article is about utilities used in the Linux shell. It can be easy to make a mistake and rename critical files that may stop programs or Linux from working. Always use the -n option. It tells the command to not overwrite an existing file.

Using it in a utility command may look like: mmv -n “*” “#l1. See below how it shows a preview of what the command will do. Yet if you list (ls) the files you’ll see none of them have changed. If it’s not what you were expecting, adjust your command and try again.

Rename Multiple Files and Folders with Rename

Rename is a Linux utility. Think of it as a small program that doesn’t have a graphical user interface. Your Linux distro might not have it, but it’s easy to install.

In the terminal, enter the command sudo apt-get install rename and press Enter. It may ask for your password, enter it, and press Enter. It will start installing.

Once installed, you can start using Rename.

  1. Navigate to the location where you want to change file or folder names.

  1. Just like in the bash script, you’ll need to use regex to select files and define what’s going to be done to them. Here’s an example: *rename ‘s/.html/.txt/’ .html

If you guessed that will change the file extensions on our files back to .txt from .html, you’re right!

Rename Files and Folders Using MMV

MMV is another Linux utility, similar to Rename. It can be installed with the command sudo apt install mmv. Once it’s installed, you can create your own commands.

  1. The example MMV command we’ll use will change all the filenames in the directory from lower case to UPPER CASE: mmv -r “*” “#u1

  1. The -r tells it to rename. The asterisk tells it to change any file in the directory. The #u1 is something special. It’s Markdown code. This tells it to change the text to uppercase.

Is That All the Ways to Rename Directories and Files in Linux?

If one of the methods here doesn’t work for you, you could use a bulk renaming tool that has a graphical user interface.

There are several to choose from. Thunar and KRename are just a couple to start with. (Source: helpdeskgeek)

Happy learning!

3 Likes