Renaming a listing in Linux is simple, and there are many methods to go about it. From renaming a single listing to discovering and renaming many, right here’s methods to do it.
Your Knowledge Is Protected
Renaming directories is one thing all of us must do on occasion.
We would create a listing and misspell its title, and we need to put it proper. Usually, the aim of a listing adjustments over time or by the lifetime of a undertaking, and also you need to alter the title to replicate its new use. Maybe you’ve decompressed an archive file and it’s created a listing tree with the listing names in uppercase and also you’d like them in lowercase.
Regardless of the motive. renaming a listing doesn’t do something to the information held inside it. It adjustments the path to that information, however the files and directories inside your renamed listing aren’t touched.
Don’t rename system directories. Altering the trail to system recordsdata and instructions goes to have a detrimental impact on the operating of your laptop, to say the least. If it’s worthwhile to use
sudo to rename a listing—except you actually know what you’re doing—the probabilities are you shouldn’t be renaming it.
Utilizing the mv Command
In essentially the most simple instances, all we actually want is the
mv command. That is an integral a part of each Linux distribution, so there may be nothing to put in.
mv command is over 50 years outdated on the time of writing. It hails from the daybreak of Unix, when quick and cryptic instructions had been in vogue, most likely to cut back the variety of characters that needed to cross alongside gradual serial traces from teletypes and dumb terminals to the precise laptop.
It truly stands for “transfer”, and it may be used to maneuver recordsdata from listing to listing. When you transfer a file to the identical location that it’s already in and provides it a brand new title, you’ve renamed the file. And we are able to do the identical with directories.
There are two subdirectories on this listing.
To rename a listing we use the mv command. We have to present the present title of the listing and the brand new title.
mv old-work archive-2
If the listing you need to rename just isn’t in your present listing, present the trail in addition to the listing title.
mv ~/htg/old-work ~/htg/archive-2
Utilizing the File Browser
File browsers are capable of rename directories. The keystroke within the GNOME Information software is F2. Highlighting a listing and tapping the F2 key opens the “Rename Folder” dialog.
Kind within the new title, and click on the inexperienced “Rename” button.
The listing is renamed for you.
It’s so simple as that.
The rename Command
In case your wants are extra sophisticated than the easy renaming of a listing you would possibly want to make use of the
rename command. This lets you use Perl expressions to rename recordsdata and directories. It supplies an altogether extra highly effective and versatile method to rename directories.
We’re going to be speaking in regards to the Perl-based
rename command. There’s one other, older command known as
rename which is a part of the Linux core utilities. You’ll most likely want to put in the Perl
rename command we need to use.
To keep away from title clashes with the prevailing
rename command, the Perl
rename command known as
prename on Fedora, and
perl-rename on Manjaro. On Ubuntu, the
prename instructions are each symbolic hyperlinks that resolve to a binary known as
So, on Manjaro the command you’ll want to make use of
perl-rename, and on Fedora it’s
prename . On Ubuntu, you should utilize
To put in Perl rename, on Ubuntu it’s worthwhile to sort:
sudo apt set up rename
On Fedora, the command is:
sudo dnf set up prename
On Manjaro the bundle known as
sudo pacman -Sy perl-rename
Ensure you use the suitable command to your distribution if you wish to work by the examples.
First Steps With rename
rename command takes Perl common expressions and applies them to a file or listing, or group of recordsdata or directories.
In our listing, we’ve got a group of different directories.
Their names are a mix of lowercase, uppercase, and combined case. We will convert all of them to lowercase with an acceptable expression.
rename 'y/A-Z/a-z/' *
All of the directories are actually in lowercase, whether or not they had been wholly uppercase beforehand, or contained the odd uppercase letter.
All of the magic is contained within the expression. The expression is wrapped in single quotes “
'“. That is what the complete command means.
- y: This implies seek for any character within the first vary of characters, and substitute it for the corresponding character from the second vary of characters.
- /A-Z/a-z/: The primary vary is all of the letters from “A” to “Z”, and the second vary is all of the characters from “a” to “z.”
- *: The asterisk wildcard means apply this to all directories.
In different phrases, the command reads as “for all directories, swap any uppercase letters for the equal lowercase letter.”
Clearly, you’ll be able to rename a single listing with
rename, though it does smack of overkill. You’ll be faster utilizing
rename 's/gamma/epsilon-2/' *
The “s” on this expression means substitute. It checks every listing to see if its title is “gamma”. Whether it is, it replaces it with “epsilon-2.” Bear in mind although, that this could even have matched a listing known as “gamma-zeta”, for instance, renaming it to “epsilon-2-zeta.”
We will keep away from this by including the beginning of string “
^” and finish of string “
$” metacharacters to the primary clause of the expression.
rename 's/^gamma$/epsilon-2/' *
This leaves the listing “epsilon-2” untouched.
Utilizing rename With Different Instructions
We will use different instructions to find the directories we would like
rename to work on. If we’ve got a set of nested directories and we need to rename any that finish in “-old” in order that they finish in “-archive”, we are able to obtain that by utilizing
We have to use
xargs as a result of
rename doesn’t settle for piped input. The
xargs command overcomes that downside by accepting the piped enter and including to the command line of one other command as a command line parameter.
Our command appears like this:
discover . -depth -type d -name "*-old" | xargs -r rename "s/outdated$/archive/"
- .: We inform discover to begin looking within the present listing. This might be any path, after all.
- -depth: Use a depth-first search. This implies the contents of deeper nested subdirectories are processed earlier than increased ones.
- -type d: Seek for directories, not recordsdata.
- -name “*-old”: The search clue. We’re on the lookout for directories with names ending in “-old.”
- |: We’re piping the output from discover into the
- xargs -r: The
-r(no run if empty) means don’t run the command if there are not any matching directories.
- rename “s/outdated$/archive/”: The
renamecommand to be run.
Our listing tree appears like this earlier than the command.
We run our command:
And we are able to see that the entire matching directories together with the nested ones have been renamed.
Horses for Programs
Renaming a listing doesn’t want something greater than
mv. When you want GUI purposes you should utilize your file browser. When you’ve acquired numerous directories to rename, and particularly in the event that they’re scattered all through a listing tree, you’re going to wish the flexibleness of
RELATED: How to Manage Files from the Linux Terminal: 11 Commands You Need to Know