Page Contents
How to Rename Files in Linux and also Directories in Linux? An Easy Guide
If you are searching the Internet for a way to rename files in Linux, you are now in the right place, and now sharing an amazing method with you.
Renaming files is not. If you are searching the Internet for a way to rename files in Linux, you are now in the right place, and now sharing a fantastic method with you extremely complex; it generally does not require special equipment until it is done on a small number of files.
However, it might be a good idea to consider time-saving strategies and apps if a complete list of photos from last year’s vacation is waiting to be called.
The script file can be renamed through a command-line control or a separate program.
There are two general approaches. Linux users already know how efficient the CLI can be, so they shouldn’t be surprised if multiple filename commands are opened.
How to Rename Files in Linux with “mv” Command:
The mv command (shortcut for “move”) is an easy way to modify files and folders.
The main function of this program is to transfer and modify data and folders because the naming of files is defined as the transfer of a file from one name to another.
The following syntax is used to rename files with mv:
mv (option) filename1.ext filename2.ext
“filename1.ext” is the original, “old” name of the file, and “filename2.ext” is the new name.
The same pattern works for folder renaming. If the files are not located in the currently active folder, their full path has to be specified:
mv /home/user/Files/filename1.ext /home/user/Files/filename2.ext
Note that the mv command needs write permission for the folder containing files. For device files and folders, the user must be able to rename files by adding mv with sudo or su to get root permission.
The -I (interactive) alternative offers an additional layer of security that allows the consumer to validate the file name change before it is actively used.
There is also the -v (verbose) alternative that lists all the changes made by mv. Options are written before file names after mv, not before.
Using the “rename” command:
This order is a little more advanced than mv because it requires regular expression knowledge or at least basic familiarity.
It may sound scary, but don’t give up on renaming – it can be used simply by following tutorials like this to rename batches.
The rename
syntax looks like this:
rename (option) 's/oldname/newname/' file1.ext file24.ext
The letter “s” stands for “substitute” and is the main part of the regular expression. Single quotes are required. The available options are:
- -v (verbose: print the list of renamed files with their new names)
- -n (“no action”: a test mode or simulation that shows only files that will be modified without touching them)
- -f (forced overwrite of original files)
The ‘rename’ command also accepts wildcards to rename multiple files of the same type and also works on file extensions.
For example, this would change all files with the extension .jpeg to .jpg:
rename 's/.jpeg/.jpg/' *
The generic symbol (*) means that all the files in the folder will be affected.
The regular expression also has its options (modifiers): “g” (global; affects all occurrences of the expression) and “i” (performs a case-insensitive substitution).
They are written to the end of the expression just before the single closing quote and can be combined:
rename -n 's/DSC/photo/gi' *.jpg
The command would also change the part of the file name to “photo”. But, due to the “-n” option, the command will not rename the file, it will simply display them in the console window.
This will be applicable in the “DSC”, “dSC” and “dsc” files. It will happen
The only thing this common phrase can do is not a replacement. There is also a conversion (labeled with a period “y”) that can make file names more complicated.
The name of the case file is changed more frequently:
rename 'y/a-z/A-Z/' *.jpg
This will rename uppercase for all.jpg files. To do this, by contrast, simply modify the “old name” and “new name” of the standard expression.
To obtain the desired result, the rename command combines some simple models. The “-n” option allows users to never compromise their data (or their nerves) by providing a secure and usable overview of what they are called.
Métamorphose2:
A cross-platform script and a massive replacement file are Metamorphoses. Metamorphosis is a very powerful tool for those who want an interface application to rename transactions. It is accessible in both Windows and Linux.
Go to their website to start and download your Distro installation package. Includes a deb file for Fedora, Mandriva, and SUSE for the Debian-based distribution and RPM kit. Only under Arch Linux, it can be used in AUR.
This is the first “selector” tab when you start the device. You choose the directory with the files you want to delete. Note that only one directory is allowed per rename process, but child directories can be accessed in the chosen directory.
Click on the “Rename” tab when choosing the page. You can choose the name change rules from here. You can select the operation you want to perform in the left panel.
For example, “insert” incorporates words in the name, while “length” allows certain characters to cut file names. You can also choose “skip file”, “rotate” and “move”. As you can see, you can rename your files with full options.
Finally, click the “Go” button at the top of the bottom panel to execute the name change action. If you encounter an error after renaming, there is a Cancel option to cancel all changes.
Are you using Windows 10 with a disabled touchscreen? Check out Fix Windows 10 Touch Screen Not Working.
pyRenamer – the easy way out:
And finally, the response expected by all anti-console users: a desktop application that can perform these tasks with a single mouse button.
PyRenamer is a crazy and efficient device written in Python to rename directories. Currently, Ubuntu users and their descendants are free to install renamed py from repositories using the command:
sudo apt-get install py-renamer
The interface consists of four parts:
- a tree-based file finder to select files and folders
- a central preview pane that shows file names before and after the name change
- a tabbed control area to choose the name change criteria
- the side options bar
PyRenamer can remove accents and symbols from file names, change any text string to another, change the file name to uppercase, lowercase, or phrases, and insert or remove spaces and underscores automatically.
Beginners love it because everything can be selected simply by pressing Rename in the tab section, and preview in the main area. PyRenamer can provide you with a cheat sheet to make playing patterns easier.
Advanced users can take advantage of pyRenamer’s ability to rename media files by reading metadata. You can also rename a single file manually if it is not practical to rename a series.
The new name of py folds the mv code into a user-friendly interface and removes the commands. For people who don’t believe in their CLI capabilities, this is a great option.
Of course, there are many ways to modify Linux files, for example, writing a script or using other renowned py tools.
How to Rename Files and Directories in Linux:
One of the most critical activities that you often need in a Linux environment is renaming files and directories. With a GUI file manager or using the command line console, you can rename the data.
Renaming a single file is simple, but it can be difficult to rename multiple files at once, especially for beginning Linux users.
This guide teaches you how to delete files and folders with the mv and rename commands.
Renaming Files with the mv
Command:
The mv (short for move) command is used to rename or move files from one location to another. The syntax of the mv command is as follows:
mv [OPTIONS] source destination
The “source” can be one or more files or directories, and the “destination” can be a single file or directory.
- If you specify multiple files, it must be a directory. In this case, the files are moved to the destination directory.
- If you specify a single file as “source” and the destination “destination” is an existing directory, the file is moved to the specified directory.
- To rename a file, you must specify a single file as the “source” destination and a single file as the “destination” destination.
For example, to rename the file “file1.txt” to “file2.txt”, you should run:
mv file1.txt file2.txt
Renaming multiple files with the mv
Command:
The ‘mv’ command can only rename one file at a time, but it can be used in conjunction with other commands such as’ find’ or inside bash ‘for’ or ‘while’ loops to rename multiple files.
The following example shows how to use the Bash for loop to rename all ‘.html’ files in the current directory by changing the extension ‘.html’ to ‘.php’.
'for f in *.html; do
mv -- "$f" "${f%.html}.php"
done'
Renaming Files with the rename
Command:
The “rename” command is used to rename multiple files. This command is more advanced than “mv” because it requires a basic knowledge of regular expressions.
There are two versions of the “rename” command with different syntax. In this tutorial, we will use the Perl version of the “rename” command.
If this version is not installed on your system, you can easily install it using the package manager of your distribution.
- Install
rename
on Ubuntu and Debian:sudo apt install rename
- Install
rename
on CentOS and Fedora:sudo yum install prename
- Install
rename
on Arch Linux:yay perl-rename ## or yaourt -S perl-rename
The syntax for the “rename"
the command is as follows::
rename [OPTIONS] perlexpr files
The “rename"
the command will rename the “files"
according to the specified “perlexpr"
regular expression. You can read more about Perl’s regular expressions here.
The following example will change all files with the extension “.html"
to “.php"
:
rename 's/.html/.php/' *.html
You can use the ‘-n'
option to print names of files to be renamed, without renaming them.
rename -n 's/.html/.php/' *.html
The output will look something like this:
rename(file-90.html, file-90.php)
rename(file-91.html, file-91.php)
rename(file-92.html, file-92.php)
rename(file-93.html, file-93.php)
rename(file-94.html, file-94.php)
By default, the “rename” command does not overwrite existing files. Pass the “-f” option to authorize the overwriting of existing files:
rename -f 's/.html/.php/' *.html
Conclusion:
In this article, you have learned to use the mv
and rename
commands to rename files. Hope, you liked it. For more useful guides and tools visit our homepage. Thank You!