CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
rename a batch of files in a folderr - how to do this in commandline

 



dilbert
User

Jan 25, 2017, 12:16 PM

Post #1 of 6 (1436 views)
rename a batch of files in a folderr - how to do this in commandline Can't Post

 
have to rename files in a folder.

the files are named like so:
01
02
03
04
...
49
50

each file has got a number as a file name.

each file should get a character

- like so: r01, r02, r03, r04 and so on . .... r50

how can we do this on commandline


FishMonger
Veteran / Moderator

Jan 25, 2017, 2:59 PM

Post #2 of 6 (1431 views)
Re: [dilbert] rename a batch of files in a folderr - how to do this in commandline [In reply to] Can't Post

What OS are you on?

What have you tried?

What errors/warnings are you getting?

In what way does your code fail to do what you want?


dilbert
User

Jan 25, 2017, 3:26 PM

Post #3 of 6 (1430 views)
Re: [FishMonger] rename a batch of files in a folderr - how to do this in commandline [In reply to] Can't Post

hello dear fishmonger,

as most standard shells provide a way to do simple text substitution within shell variables. i had a quick look at the linux documentation project - see here http://tldp.org/LDP/abs/html/parameter-substitution.html explains as follows:

${var/Pattern/Replacement}

First match of Pattern, within var replaced with Replacement.

So use this script to loop through all the appropriate files and rename each of them:



Code
for file in (1 to 50 ) *_opt 
do
mv -i "${file}" "${file/-1 to 50 -/-r 1-to 50 -}"
done



well - i have a little thing to the code:
i have added a -i option so you have the chance to confirm each renaming operation. As always, you should make a backup of all your files before doing any large amount of renaming or deleting.


But - dear Fishmonger - i failed.

i need your help


FishMonger
Veteran / Moderator

Jan 25, 2017, 5:21 PM

Post #4 of 6 (1425 views)
Re: [dilbert] rename a batch of files in a folderr - how to do this in commandline [In reply to] Can't Post

That's shell code and this is a perl forum. If you want a shell solution, then you might want to post your question in a shell forum. If you want a perl solution, then write your perl script and if you need help troubleshooting it, post your code and update your question.

Perl has the File::Copy module you can (and probably should) use but you can also make system calls to the systems mv command.

http://search.cpan.org/~shay/perl-5.24.1/lib/File/Copy.pm

There are several ways to get the list of files and the built-in glob function is the most common approach.

http://perldoc.perl.org/functions/glob.html


dilbert
User

Jan 25, 2017, 11:35 PM

Post #5 of 6 (1416 views)
Re: [FishMonger] rename a batch of files in a folderr - how to do this in commandline [In reply to] Can't Post

many thanks dear fishmonger - will digg into all things you mentioned.

thanks again. grretings dilbert


Laurent_R
Veteran / Moderator

Jan 26, 2017, 5:45 AM

Post #6 of 6 (1412 views)
Re: [dilbert] rename a batch of files in a folderr - how to do this in commandline [In reply to] Can't Post

Note that there is also a rename Perl internal command which may do just what you need.

It has a few gotchas when trying to move files around, but if you're just renaming files, it should probably be OK, provided you check that there is no existing file having the new name (existing files having the target name would be clobbered).

See the documentation on it here: http://perldoc.perl.org/functions/rename.html

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives