How to Rename Files in Linux and also Directories in Linux? [Latest 2023]
How to Rename Files in Linux and also Directories in Linux?-Note that for the folder containing files, the mv command needs write permission. For device files and folders, 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 own 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 closing single 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, simply 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.
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 obviously free to install renamed py from repositories using the command:
sudo apt-get install pyrenamer
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, 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.
Basically 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 of 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.
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!