Are you looking to rename a file in a Linux computer? It’s tricky, but don’t worry! Nowadays, we have lots of helpful tools to figure things out.
While working hard on this blog post, we discovered it’s more complex than it seems! But don’t give up. We’re here to help you learn some easy ways to rename files in Linux. Keep reading to find out how!
Understanding The Linux File System
Let’s explore the Linux File system before learning to rename files in Linux. So, be ready to jump into the world of Linux.
What is a Linux File System?
The Linux file system is like the skeleton of the computer. It helps keep everything organized by giving a place for files and folders to live. Knowing how to rename a file in Linux, and how these files and folders are arranged is essential to use Linux well.
Tree-like Structure
In Linux, the file system is like a big tree. It starts from a place called the root directory, which is like the tree’s trunk. Then, many branches called directories and subdirectories spread out from the root directory, making up the whole file system.
Hierarchical Organization
In Linux, each folder can have files and more folders inside. They’re arranged like a tree, with folders branching out. This setup helps keep everything organized and easy to handle.
Navigating the File System
Knowing how files and folders are organized in Linux lets you find stuff easily. When you use the command line, you must give the right directions to find what you need. This enables you to do things quickly and find your files easily.
File Identification
In Linux, every file has a unique address called a path. This path shows where the file is kept in the computer. A path includes all the folders the file is in and then its name. For instance, if a file named file.txt is inside a folder called documents inside another user folder, the path would be /home/user/documents/file.txt.
Method 1
Rename File in Linux Using the Command Line
Using the command line might seem scary initially, but it’s helpful and quick to rename a file in Linux. We’ll use two commands called “mv” and “rename,” which work on any type of Linux computer.
Rename Files in Linux Using the mv Command.
The mv command is like a mover for your computer files. It helps you move files and folders around in your computer’s storage. You can also use it to rename a file in Linux. So, it’s like giving a new name to your stuff! When you want to use the “mv” command, you type it like this:
mv <options> <source_file> <destination_file>
Options | Description |
---|---|
–backup | Save a copy of each file you already have. |
-f | Stop the computer from asking before replacing a file. |
-i | Ask before you move a file. |
-u | Only move a file if the one you’re moving from is newer. |
-v | Show what you’re doing right now. |
-n | Don’t replace a file if it’s already there. |
Rename a File in Linux Using the mv Command
If you want to change the name of one file using the mv command, just follow these steps:
mv -v <old_file_name> <new_file_name>
When you type the command, you’ll see a message below to confirm it worked. You can check what changed by using the “ls” command.
renamed ‘<old_file_name>’ -> ‘<new_file_name>’
In the example, we’re changing the name of a file called “mini-course-10.pdf” to “test-1.pdf” using a command called “mv.” It looks like this:
mv -v mini-course-10.pdf test-1.pdf.
Next, you can check if the file name changed by looking at what the computer shows after you use the ‘mv’ command (the one we’re talking about in the picture). You can also check by using the ‘ls’ command. When you do that, you’ll see the file with its new name in the list of files in that folder.
Tip: If you want to rename a file in Linux mv, just type in this rename command: mv old_filename new_filename
Rename Multiple Files using the mv Command.
The “mv” command helps change the names of many files in a folder at once. We’ll learn some extra tricks with it. Sometimes, the command might seem hard to get, so we’ll show you how it works with an example.
For i in *.pdf; do
mv -v “$i” “${i/${i:0:4}/example}”
Done
This code might seem hard at first, but it’s pretty simple. When you run it, it’ll check a list of files repeatedly. These files have names that end with “.pdf”. It will change the first four letters of each file name to “example.” It’ll keep doing this until all the files have new names.
You can rename files in Linux in subdirectories, if you want to change the name of a file inside a folder, just tell the computer where the folder is and what you want to call the file using a command called “mv.”
Tip: If you want to change the names of many files in Linux at once, just type in this command: mv old_filename1 old_filename2 new_filename
Method 2
Rename File in Linux Using Rename Command
The “rename” command helps make renaming files easier compared to the “mv” command. It has simple rules for renaming one file or many files at once. Most computer systems already have the rename command ready to use. If it’s not there, you can add it easily with just a simple command:
Debian-based systems:
sudo apt install rename
Fedora-based systems:
sudo yum install prename
Arch-based systems:
sudo pacman -S install rename
Here’s how you can change the name of something on a computer that uses Debian:
rename <options> ‘s/<old_name>/<new_name>/’ <file_names>
When you change the name of something using the rename command, it uses a particular language called Perl behind the scenes. In Perl, there are three different ways to work with words: match, substitute, and translate.
The rename command uses the “substitute” and “translate” ways. In the command above, the letter “s” tells the computer you want to do a substitute. If you’re using a system like RedHat or Fedora, you’ll need to type the command in this way:
rename <options> <old_name> <new_name> <file_name>
Options | Description |
---|---|
-v | This tells you what’s happening right now. |
-n | It’s like a practice button to see what will happen to the files. |
-f | It’s used to ensure a file gets replaced with a new one. |
Rename a Single File
The rename command is like the mv command when you want to change the name of just one file. To change the name of one file, use this:
rename -v ‘s/<original_name>/<new_name>/’ <file_name>
Rename Multiple Files
Here’s why using the rename command is better than the mv command when you want to rename many files. It’s easier and quicker to use. Here’s how it works:
rename <options> ‘s/<old_name>/<new_name>/’ <files_to_rename>.
We write the command almost the same as when we rename just one file. The only difference is the part where it says “<files_to_rename>.” Instead of typing the exact file name, we can use special symbols called wildcards to change many files at once, like renaming all the files that have something in common in a folder.
Rename Files Partially
Imagine if all your file names have spaces, but now you want to change them to underscores. You can do this using a command called “rename.” Here’s how:
rename -v ‘y/<original_part>/<part_to_replace>/’ <files_to_rename>
Let’s say you have something you want to change; we’ll call it the <original_part>, and you also have a pattern in mind for what you want to change it to. We’ll put that pattern inside another part called <part_to_replace>. We use “y” to show that we’re using the “translate” regular expression.
So, for example, if you want to swap spaces with underscores, you’d use this command:
rename -v ‘y/ /\_/’ <files_to_rename>.
Here, the computer thinks of the underscore as a letter, not a particular part of how commands work.
Change The Case of The Filenames
With the rename command; you can switch small letters to big ones and vice versa. To change small letters to big ones, just follow these steps:
rename ‘y/a-z/A-Z/’ <files_to_replace>
If you want to change big letters to small ones, just follow this pattern:
rename ‘y/A-Z/a-z/’ <files_to_replace>
Chapter 4
Renaming The File in Linux Using the GUI
The GUI method is helpful for people who fear using the command line. We use the GUI method with the Nautilus File Manager and GPrename in Ubuntu 20.04 LTS. But don’t worry; these methods will work on any computer, though we might need to change them depending on your computer type.
Rename Files Using File Manager
This is one of the simplest ways to change file names in Linux. Every type of Linux computer already has a program called a file manager, like Nautilus, Dolphin, Thunar, and others. We’ll show you how to use these programs to rename files. Just keep reading to learn how!
Rename A Single File
1. First, open the File manager you like to use. Then, find the file you want to change the name of. Click on it with the right mouse button.
2. A menu will pop up. Choose the “Rename” option from this menu. Or, you can press the F2 button on your keyboard instead of using the mouse to rename files.
3. You need to type a new name for the file. Then, click on “Rename” or press Enter on the keyboard. That’s all you have to do!
Rename Multiple Files Using File Manager
- Hold the Shift key and click on all the files you want to rename.
- After selecting them, right-click on any of the chosen files.
- A menu will pop up. Choose the “Rename” option.
- Or, just press the F2 button on your keyboard to rename the files.
- When you click on this, a new window will appear to rename stuff, as shown here. You’ll see two choices for renaming files. Let’s learn how to use each one step by step.
If you want to rename files in Linux in bulk at once, just type in this command: mv old_filename1 old_filename2 new_filename
Rename File in Linux Using a Template
- You’ll find a box with words already called [Original file name] when you pick this choice. Type in a name that you want to use for all the files.
- So, we call all our files “test_.” Then, click the “Add” button next to that name and pick the automatic numbering option. This helps each file have its memorable name.
- Next, decide how to name your files. You can choose if you want them to go in order from smallest to biggest, most significant to smallest, or something else.
- Check the changes at the bottom. Then, click “Rename” at the top right.
Find and Replace Text
Here’s a simple way to change a name in a file if it’s already a name many people use. Follow these steps:
- Type the old name in the box that says “Existing Text.” You’ll see it highlighted below.
- In the box that says “Replace With,” type the new name you want to use instead.
- Check if everything looks right below, then click “Rename” at the top to make it official.
Method 3
Rename File in Linux Using GPRename
GPRename is a simple tool for changing many file names simultaneously on a Linux computer. It has different choices to help you when you’re changing names. This tool is suitable for people who struggle to change many file names simultaneously as their usual file program. But you have to install it yourself with these commands:
Debian-based systems:
sudo apt update && sudo apt install gprename
Fedora-bassed systems:
sudo dnf install gprename
Arch-based systems:
sudo pacman -S gprename
Rename A Single File with GPRename
- First, find and click on GPRename in the Applications menu or type “gprename” in the particular place called the Linux Terminal.
- Next, look at the tree on the left side of the screen. This helps you move around to find the folder where your file is. Then, click on the file you want to give a new name on the right side of the screen.
- Look down at the bottom part of the screen. There, you’ll see some choices of what you can do. Say you want to change a word in a file name to a different word. Click on the tab that says “Replace/Remove.” Then, type the word you want to change in the box labeled “Replace” and the new word in the box labeled “With.”
- Click the ” Preview ” button on the right side to check out the changes. If everything looks good, click on the button below that says “Rename.” That’s it! You’re all set.
Rename Multiple Files with GPRename
- Look at the list on the left to find the folder you want. Click on it. Then, pick the files you want to rename. At the bottom, choose what you want to do next.
- To check the changes, tap the preview button on the right side. Then, hit the “Rename” button below it.
Method 4
Rename File in Linux With Hosting Account
In this part, we will take a quick look at how to change file names in Linux using the CPanel Method.
Rename Files in Linux With Cpanel
If you put a file on your Linux Hosting account, you can give it a new name.
- Go to the page for your stuff.
- Under the part called Web Hosting, find the Linux Hosting you want to use, then click on Manage next to it.
- Go to the top left corner of your account Dashboard and click on “File Manager.”
- Go to the cPanel File Manager. Find the file you want to change. Click on it, then choose Rename. Type in the new name for the file and hit Enter/Return.
Conclusion
When you’re just starting, knowing how to handle files in Linux is reasonable. Renaming the file in Linux is a task you’ll do a lot. In this guide, we’ll teach you four ways to rename files: two using commands you type and two using buttons and menus.
Most people will use the file manager to rename files. But some folks like to use special commands called “mv” and “rename” in the Terminal. These commands are handy for renaming files quickly. You can also get a unique control panel tool to help you manage files with buttons and menus.
Leave a Reply