To rename files in a directory should I use Bash script or a Perl Script ?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
To rename files in a directory should I use Bash script or a Perl Script ?
I am using Suse 10.2 on a HP Compaq NX6325 Notebook with 2gigs of ram. I have a USB Ext. drive 500G. with over 1,000 files that have to be renamed in succeeding order of numbers meaning 1,2,3,4, with a date (Date that the file was renamed).
My question is should I create a Perl script or a Bash Script to rename the files and should I rename them in blocks of 200 and move the renamed files to another directory?
The catch is I will have to use the system in question to do my day to day work like email creating docs and surfing the web.
So the script once running can not be taxing on the system and has to be very fast since the files in question has to be renamed with in a 3 day period.
Not quite sure what you mean by 'renamed in succeeding order of numbers meaning 1,2,3,4', but Perl is faster (compiled rather than interpreted) and it's better if the selection/matching criteria gets complicated.
Otherwise, use bash ... your choice.
Either lang should do want you want within the time limit.
Note that the mv cmd enables you to rename at the same same eg
mv afile.dat bfile.dat
iirc, this (mv) can only be used between dirs on the same (physical) disk partition, otherwise you have to copy (cp), then delete (rm) the original version.
There's no need to use blocks of 200, just do them one at a time as you go eg (pseudo-code)
Code:
for file in list
do
newfile = create newfilename
mv file newfilename
done
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
Renaming 1000 files will not put any stress on your system. If you are only renaming them, they are, well, renamed and not copies, no large data transfer involved. I routinely rename batches of 100 files on my system and the slowest is the verbose output to my screen. 5-10 seconds?
I usually write a script to echo the command to stdout, then capture and run it
through a shell, so you have a record of what you have done if you need
to reverse it:
iirc, this (mv) can only be used between dirs on the same (physical) disk partition, otherwise you have to copy (cp), then delete (rm) the original version.
This is not true. mv works fine regardless of filesystems. Of course behind the scenes the physical copying will take place if you mv across filesystems, but you don't have to do it manually.
info mv
"Prior to version `4.0' of the fileutils, `mv' could move only regular files between file systems."
From perl docs re (Perl) rename cmd
"Behavior of this function varies wildly depending on your system implementation. For example, it will usually not work across file system boundaries, even though the system mv command sometimes compensates for this."
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.