- How to Rename Files in Linux
- How to Rename Files in Linux with the mv Command
- Rename File on Linux Using the mv Command
- Rename Multiple Files With the mv Command
- Rename Files on Linux Using the Rename Command
- Remove Rename Command
- Conclusion
- How to Easily Rename Files in Linux
- Renaming Files with “mv” Command
- Using the “rename” Command
- Métamorphose2
- pyRenamer – The Easy Way Out
How to Rename Files in Linux
A command-line terminal is an essential tool for administrating Linux servers. It provides Linux users some of the best productivity tools while saving your machine’s resources.
To effectively use the potential of Linux, you will need to have strong knowledge of the fundamentals – simple commands, like renaming existing files and folders. In this tutorial, you’ll learn how to rename folders in Linux.
How to Rename Files in Linux with the mv Command
Shortened from “move,” the mv command is one of the easiest commands to use. It can do two basic but essential tasks when handling files on Linux. One is moving files from one location to another, and the other is renaming one or more files through the terminal.
First, let’s see how renaming files with mv works on Linux.
To begin, we access our server through the command line using SSH. If you are unsure about SSH and would like to learn more, here’s a helpful tutorial.
To access our server, type the following into your terminal:
If we are using a local computer, instead of a server, then we will have to open the terminal from the main menu.
Afterward, it is important to know how the mv command works. To do this, we run the following:
As we can see in the previous image, the basic use of the mv command is as follows:
Here are some of the most popular mv options:
- -f – shows no message before overwriting a file.
- -i – displays warning messages before overwriting a file.
- -u – only move a file if it is new or if it does not exist in the destination.
- -v – show what the command does.
And the parameters are:
[SOURCE] – the source destination of the file
[DESTINATION] – the destination directory.
Rename File on Linux Using the mv Command
If we want to rename a file, we can do it like this:
Assuming we are located in the directory, and there is a file called file1.txt, and we want to change the name to file2.txt. We will need to type the following:
As simple as that. However, if you are not in the directory, you will need to type a bit more. For example:
Rename Multiple Files With the mv Command
The mv command can only rename one file, but it can be used with other commands to rename multiple files.
Let’s take the commands, find, for, or while loops and renaming multiple files.
For example, when trying to change all files in your current directory from .txt extension to .pdf extension, you will use the following command:
This will create a loop (for) looking through the list of files with the extension .txt. It will then replace each .txt extension with .pdf. Finally, it will end the loop (done).
If you want more advanced features, you’ll need to use the rename command, we’re about to cover.
Rename Files on Linux Using the Rename Command
With the rename command, you will have a bit more control. Many Linux configurations include it by default. But, if you don’t have it installed, you can do it in just a minute with a simple command.
In the case of Debian, Ubuntu, Linux Mint, and derivatives:
On the other hand, if you are using CentOS 7 or RHEL:
And, if you are using Arch Linux:
Now, we can start using the rename command. In general, the basic syntax of the rename command looks like this:
It may seem complex at first, but it’s a lot simpler than it might seem.
In this example, we will create a new folder called filetorename, and using the touch command, we will create 5 files.
With the last ls command, you can view the files that you created.
If we want to rename a single file called file1.txt, the sentence would be like this:
If we wanted to change the extension to all files, for example, to .php. We could do it this way:
We can also specify another directory where the files you want to rename are.
We’d like to mention that rename uses a regular expression of Perl, meaning this command has extensive possibilities.
Finally, it is a good idea to check all the command options. You can view them in the terminal by executing:
Some common examples of how to use the rename command are:
-
- Convert filenames to uppercase:
- Convert filenames to lowercase:
- Replace spaces in filenames with underscores:
Remove Rename Command
If you no longer wish to have rename installed on your system, remove it using the software manager. Or from the terminal.
For Debian, Ubuntu, Linux Mint and derivatives:
And for CentOS and RHEL:
That’s it, rename is removed from your Linux machine.
Conclusion
Renaming files in Linux using the terminal is a simple and practical task but sometimes very important. Knowing how to do it is something every server manager should know.
As we have seen, there are two commands that can do it. One is simpler than the other, but both accomplish the task.
We encourage you to continue researching these commands and improving the quality of your everyday workflow.
Edward is an expert communicator with years of experience in IT as a writer, marketer, and Linux enthusiast. IT is a core pillar of his life, personal and professional. Edward’s goal is to encourage millions to achieve an impactful online presence. He also really loves dogs, guitars, and everything related to space.
Источник
How to Easily Rename Files in Linux
Renaming files is not a particularly advanced operation; as long as it’s done on a small number of files, it usually doesn’t require special tools. However, when there’s an entire folder of photos from last year’s vacation waiting to be renamed, it may be wise to consider some timesaving tricks or apps.
There are two general approaches when you rename files in Linux: via the command-line interface or a standalone application. Linux users already know how powerful the CLI can be, so it shouldn’t be surprising that there are several commands for file renaming.
Also read: How to Copy and Paste Text, Files and Folders in Linux Terminal
Renaming Files with “mv” Command
A simple way to rename files in Linux is with the mv command (shortened from “move”). Its primary purpose is moving files and folders, but it can also rename them, since the act of renaming a file is interpreted by the filesystem as moving it from one name to another.
The following syntax is used to rename files with mv:
“filename1.ext” is the original name of the file, and “filename2.ext” is the new name.
The same pattern works for renaming folders. If the files are not located in the currently active folder, their full path has to be specified:
Note that the mv command requires write permission for the folder containing the files. In the case of system files and folders, the user needs to obtain root permissions to rename files by prepending mv with sudo . An extra layer of protection is provided by the -i (interactive) option, which asks the user to confirm the file rename before it’s actually applied.
There’s also the -v (verbose) option, which lists all changes that have been made by mv . Options are written after mv but before the filenames.
Using the “rename” Command
This command to rename files in Linux is slightly more advanced than mv because it requires the knowledge of, or at least a basic familiarity with, regular expressions. That may sound scary, but don’t give up on rename just yet – it can be used for plain batch renaming simply by following tutorials like this one.
In many distributions of Linux, the rename command may not be available by default. If you are unable to use it in yours, you will have to install it.
In Debian-based Linux, type this in your terminal:
In Arch-based Linux:
The rename syntax looks like this:
The letter “s” stands for “substitute,” and it’s the main part of the regular expression. Single quotes around it are obligatory. Available options are:
- -v (verbose: prints the list of renamed files along with their new names)
- -n (“no action:” a test mode or simulation which only shows the files that will be changed without touching them)
- -f (a forced overwrite of the original files)
The rename command also accepts wildcards to rename multiple files of the same type, and it works on file extensions as well. For example, this would change all files with the extension .jpeg to .jpg:
The wildcard symbol (*) means that all files in the folder will be affected.
The regular expression also has its own options (modifiers): “g” (global: affects all occurrences of the expression) and “i” (performs case-insensitive substitution). They are written at the end of the expression just before the closing single quote and can be combined:
This would apply to all .jpg files that contain “DSC,” “dSC,” and “dsc” – change that part of the filename to “photo.” However, because of the “-n” option, the command wouldn’t actually rename the files but just print them in the console window.
Substitution is not the only thing this regular expression can do. There’s also translation – marked by the letter “y” – which can transform the filenames on a more complex level. It is most often used to change the filename case:
This would change the names of all .jpg files from lowercase to uppercase. To do it in the reverse, just switch the “oldname” and “newname” parts of the regular expression.
Using the rename command boils down to mixing a few basic patterns to achieve the desired result. Thanks to the -n option, the users will never have to put their files at risk (or their nerves at stake) since it offers a safe and useful preview of what the renamed files will look like.
Also read: How to Fix the “No Space Left on Device” Error on Linux
Métamorphose2
Métamorphose is a cross-platform file and folder mass renamer. For those who prefer a GUI tool for carrying out renaming operations, Métamorphose is quite a powerful tool to use. It is available for both Windows and Linux.
To get started, go to its website and download the installer file for your distro. It provides a deb file for Debian-based distro and RPM package for Fedora, Mandriva and SUSE.
Once you run the app, it will be the first “Picker” tab. This is where you select the directory that contains the files you want to batch rename. Do note that it only supports one directory per renaming operation, though it can recurse through child folders within the selected directory.
After selecting the directory, click on the “Renamer” tab. From here you can decide on the renaming rules. On the left pane you can select the action you want to perform. For example, “insert” will insert terms to the name, while “length” allows you to trim the file name to a number of characters. There are also the “move text,” “replace” and “modify” options you can choose. As you can see, it provides extensive options for you to rename your files.
Lastly, just click the “Go” button at the top of the bottom pane to run the renaming action. If you find any error after renaming, there is an Undo option to revert all the changes.
Also read: How to Use the lp Command in Linux to Print Files From Terminal
pyRenamer – The Easy Way Out
Finally, the solution that all the anti-console users have been waiting for: a desktop application where everything can be done with a simple click of the mouse. pyRenamer is a crazy-powerful file renaming tool written in Python, which can be found here. Users of Ubuntu and its derivatives can install pyRenamer from the repositories using the command:
The interface consists of four parts:
- Tree-view file browser for selecting files and folders
- Central preview pane showing the filenames before and after renaming
- Tabbed control area for choosing the renaming criteria
- Options sidebar
pyRenamer can remove accents and duplicate symbols from filenames; replace any string of text with another; 20names to uppercase, lowercase or sentence case; and automatically insert or remove spaces and underscores. Beginners will love it because all of this can simply be selected in the tabbed area, previewed in the main area and confirmed by clicking Rename. If they wish to experiment with patterns, pyRenamer provides a cheatsheet to make it easier.
Advanced users will appreciate pyRenamer’s ability to rename multimedia files by reading from their metadata. It’s also possible to manually rename a single file when a batch rename is unnecessary.
In essence, pyRenamer wraps the functionality of the mv and remove commands into a user-friendly GUI. It’s a great choice for those who aren’t confident in their CLI skills.
Now that you know how to rename files in Linux, you may want to learn how to recursively change file permissions. Read on to learn how to diagnose a slow-running Linux computer or perhaps make use of the Hosts file to block/access websites.
Miguel has been a business growth and technology expert for more than a decade and has written software for even longer. From his little castle in Romania, he presents cold and analytical perspectives to things that affect the tech world.
Источник