How to Rename Files and Folders in Linux
Both single as well as multiple
Whether you’re a veteranLinux useror just picked up a distro like PopOS orUbuntu, you still need to knowhow 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 graphicalfile manager similar to File Explorer in Windowsor Finder in MacOS. Most of them will function in the same way, but there may be differences.
This method is odd and you’re unlikely to use it, but it’s still possible.
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.
It can apply the template to the files based on the original name or modified date.
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 commandman(for manual) and the command or name of the utility you need help with. For example,manmvwill show the manual for using themvcommand.
After renaming files or directories in Linux, always check them by either looking in the File Explorer or using thelscommand 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
Type inmv “01-Work Documents” “Work Documents”and press enter.
Listing the files shows it’s renamed.
Rename Multiple Files or Folders Using Bash Script
Tocreate 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; domv — “$file” “${file%.txt}.html”done
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. Thedotells it to do the command as long as there are matching files. This is aloop. The second line has themvcommand.
The double-dash (—) tells it there are no options for the command, get ready for some regular expression or regex. The$fileis a variable that tells it to work with any file picked up by the first line. The%tells it to replace the.txtif 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-noption. 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 commandsudo apt-get install renameand pressEnter. It may ask for your password, enter it, and pressEnter. It will start installing.
Once installed, you can start using Rename.
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 commandsudo apt install mmv. Once it’s installed, you can create your own commands.
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 abulk renamingtool that has a graphical user interface.
There are several to choose from.ThunarandKRenameare just a couple to start with.
Guy has been published online and in print newspapers, nominated for writing awards, and cited in scholarly papers due to his ability to speak tech to anyone, but still prefers analog watches.Read Guy’s Full Bio
Welcome to Help Desk Geek- a blog full of tech tips from trusted tech experts. We have thousands of articles and guides to help you troubleshoot any issue. Our articles have been read over 150 million times since we launched in 2008.
HomeAbout UsEditorial StandardsContact UsTerms of Use
Copyright © 2008-2024 Help Desk Geek.com, LLC All Rights Reserved