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


Views: 2806
rename a batch of files in a folderr - how to do this in commandline

 
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


Views: 2801
Re: [dilbert] rename a batch of files in a folderr - how to do this in commandline

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


Views: 2800
Re: [FishMonger] rename a batch of files in a folderr - how to do this in commandline

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


Views: 2795
Re: [dilbert] rename a batch of files in a folderr - how to do this in commandline

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


Views: 2786
Re: [FishMonger] rename a batch of files in a folderr - how to do this in commandline

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


Views: 2782
Re: [dilbert] rename a batch of files in a folderr - how to do this in commandline

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