Find out how to rename a number of folders in Linux utilizing command line
I am new to Linux programming and bash shell instructions. I’ve a folder construction as dir1, dir2, dir3 and I want to rename it as dir001, dir002, dir003. How do you rename a number of folders in Linux?
The mv command (mv supply goal) renames the file/folder named by the supply operand to the vacation spot path named by the goal operand. Nonetheless, the mv works with a single file title and listing/folder title on Linux and Unix-like system. Due to this fact, this web page exhibits alternate strategies to rename a number of folders on Linux and Unix-like programs.
Adblock detected ????
My web site is made potential by displaying on-line commercials to my guests. I get it! Adverts are annoying however they assist hold this web site operating. It’s laborious to maintain the location operating and producing new content material when so many individuals block adverts. Please contemplate donating cash to the nixCraft by way of PayPal/Bitcoin, or develop into a supporter utilizing Patreon.
Find out how to rename a number of folders in Linux
To rename a number of folders, one can use rename command from MariaDB/MySQL or Linux utilities package deal.Another choice is to make use of the bash for loop.
Linux rename a number of folders utilizing rename command
The syntax is:
rename expression substitute file
rename command examples
Allow us to see some examples. Create some information as follows utilizing the contact command
contact file.txtz
Listing these information:
ls file*
Now appropriate file extension from .txtz .txt i.e. repair the extension of your .txtz information:
rename -v .txtz .txt *.txtz
Confirm with the assistance of ls command
ls file*
The place,
-v : Verbose output.txtz Match all .txtz extension.txt Change with .txt*.txtz Work on all *.txtz file within the present working listing
Engaged on folders
Allow us to create some folders utilizing the mkdir command:
mkdir dir1..5
ls -d dir*
Now rename dir1, dir2 as dir001, dir002, and so forth:
rename -v dir dir00 dir?
ls -d dir*
Please notice that the rename command has no safeguards choices by default. Due to this fact, you would possibly get the incorrect file/folder names. It’s advisable that you just first backup all information and do dry run as follows:
rename -n -v dir dir00 dir?
Don’t make any modifications by passing the -n choice (dry run) and we added -v choice to see what can be made.
Getting assistance on rename command
Run man command as follows:
man rename
OR
rename –help
Pattern outputs:
Choices:
-v, –verbose clarify what’s being executed
-s, –symlink act on the goal of symlinks
-n, –no-act don’t make any modifications
-o, –no-overwrite do not overwrite current information
-i, –interactive immediate earlier than overwrite
-h, –help show this assist
-V, –version show model
Choices:
-v, –verbose clarify what’s being executed
-s, –symlink act on the goal of symlinks
-n, –no-act don’t make any modifications
-o, –no-overwrite do not overwrite current information
-i, –interactive immediate earlier than overwrite -h, –help show this assist
-V, –version show model
Renaming a number of folders utilizing bash for loop
Say you’ve dirs as follows:
complete zero
drwxrwxr-x. 2 vivek vivek 40 Might 19 11:18 ‘mp four dir 1‘
drwxrwxr-x. 2 vivek vivek 40 Might 19 11:18 ‘mp4 dir 2‘
drwxrwxr-x. 2 vivek vivek 40 Might 19 11:18 ‘music dir 1‘
drwxrwxr-x. 2 vivek vivek 40 Might 19 11:18 ‘music dir 2 ‘
complete zero
drwxrwxr-x. 2 vivek vivek 40 Might 19 11:18 ‘mp four dir 1’
drwxrwxr-x. 2 vivek vivek 40 Might 19 11:18 ‘mp4 dir 2’
drwxrwxr-x. 2 vivek vivek 40 Might 19 11:18 ‘music dir 1’
drwxrwxr-x. 2 vivek vivek 40 Might 19 11:18 ‘music dir 2 ‘
We want take away the white areas within the folder names. Run:
for i in *
do
mv -v “$i“ “$“
executed
for i in *
do
mv -v “$i” “$”
executed
On this remaining instance will use POSIX shell rename all *.PY file to *.py:
for j in *.PY
do
mv -v — “$j“ “$j%.PY.py
executed
for j in *.PY
do
mv -v — “$j” “$j%.PY.py
executed
mmv – a Command line instrument for Renaming a number of information in Linux
First set up mmv utilizing the apt command/apt-get command/yum command/apt-get command as per your distro:
sudo yum set up mmv ### <–CentOS/RHEL and co ###
sudo apt set up mmv ### <– Ubuntu/Debian and co ###
sudo dnf set up mmv ### <– Fedora Linux ###
Pattern outputs from Fedora 30 field:
Dependencies resolved.
=============================================================================
Bundle Structure Model Repository Measurement
=============================================================================
Putting in:
mmv x86_64 1.01b-30.fc30 fedora 38 ok
Transaction Abstract
=============================================================================
Set up 1 Bundle
Whole obtain measurement: 38 ok
Put in measurement: 67 ok
Is that this okay [y/N]: y
Downloading Packages:
mmv-1.01b-30.fc30.x86_64.rpm 23 kB/s | 38 kB 00:01
—————————————————————————–
Whole 13 kB/s | 38 kB 00:03
Operating transaction examine
Transaction examine succeeded.
Operating transaction take a look at
Transaction take a look at succeeded.
Operating transaction
Getting ready : 1/1
Putting in : mmv-1.01b-30.fc30.x86_64 1/1
Operating scriptlet: mmv-1.01b-30.fc30.x86_64 1/1
Verifying : mmv-1.01b-30.fc30.x86_64 1/1
Put in:
mmv-1.01b-30.fc30.x86_64
Full!
Dependencies resolved.
=============================================================================
Bundle Structure Model Repository Measurement
=============================================================================
Putting in:
mmv x86_64 1.01b-30.fc30 fedora 38 ok Transaction Abstract
=============================================================================
Set up 1 Bundle Whole obtain measurement: 38 ok
Put in measurement: 67 ok
Is that this okay [y/N]: y
Downloading Packages:
mmv-1.01b-30.fc30.x86_64.rpm 23 kB/s | 38 kB 00:01
—————————————————————————–
Whole 13 kB/s | 38 kB 00:03
Operating transaction examine
Transaction examine succeeded.
Operating transaction take a look at
Transaction take a look at succeeded.
Operating transaction
Getting ready : 1/1
Putting in : mmv-1.01b-30.fc30.x86_64 1/1
Operating scriptlet: mmv-1.01b-30.fc30.x86_64 1/1
Verifying : mmv-1.01b-30.fc30.x86_64 1/1 Put in:
mmv-1.01b-30.fc30.x86_64 Full!
The mvm command transfer, copy, append/hyperlink a number of information/dirs by wildcard patterns. The syntax is:
mmv from to
mmv [options] from to
Say you wan to make all folder names uppercase, strive:
mmv “dir*” “#u1”
In case you wan to make all folder names lowercase, strive:
mmv “dir*” “#l1”
You’ll be able to change a suffix i.e. extension from *.C to *.cpp as follows:
mmv ‘*.C’ ‘#1.cpp’
the extension “.bar” is misplaced and the extension “.foo” is added.
The mmv command is highly effective cli instrument, and it has many extra choices. Thus, learn the person web page of the mmv:
man mmv
Use thunar GUI instrument to rename a number of folder without delay in Linux
The thunar is a simple to make use of file supervisor for the Xfce Desktop Setting. You’ll be able to set up it as follows:
sudo yum set up thunar ### <–CentOS/RHEL and co ###
sudo apt set up thunar ### <– Ubuntu/Debian and co ###
sudo dnf set up thunar ### <– Fedora Linux ###
Pattern session from thunar:File Supervisor for the Xfce Desktop Setting with the majority renamer
Conclusion
You discovered rename a number of folders in Linux utilizing varied choices. See my web page “Linux Rename A number of Information At a Shell Immediate” for extra data. The rename command is a part of the util-linux package deal and is accessible right here.