Linux-like commands in Perl script

July 1, 2010 at 11:45:15
Specs: Win XP, P4/1GB
What is the best way to get the equivalent power of Linux shell scripting (e.g., bash) on Windows (XP)? I can write Perl scripts, but what would be nice would be a "cookbook" of code fragments to accomplish various tasks. For instance, in Linux I can write a single command something like:

for f in `find / -name "*.jpeg" -print`; do
  p=pathname $f
  b=basename $f
  mv $f $p/$b.jpg

to rename all *.jpeg on my system to *.jpg (as an example -- I haven't actually debugged it, and there may be simpler ways to do it). I would be looking for a cookbook of fragments to write the equivalent Perl script running in the DOS window. In pseudocode, it might be something like

get list of all *.jpeg files (recursively)
  put into array
  loop through array of file names one at a time
    copy src name to dst name
    use string functions to change extension of dst name
    rename src dst

I don't want to install Cygwin or any other major system (I do have Perl already), and I would prefer not having to learn yet another shell/batch language (I hear there is a new one in Windows). I would be happy just to not have to reinvent the wheel when working in Perl and be able to do common tasks that are so simple with a good shell language. Any suggestions, preferably for an online resource? Thanks!

P.S. This would often be "batch", but a nice bonus would be interactive Perl programming, where I can be asked for input.

See More: Linux-like commands in Perl script

Report •

July 1, 2010 at 13:18:55
Have you looked at using CPAN?

Perl's implementation of the *nix find command is File::Find and its cousins (sub classes) such as File::Find::Rule

For coping/moving files, look at File::Copy and File::Copy::Recursive

For a "Cookbook" look at Perl Cookbook, Second Edition. If you subscribe to Safari, you can read O'Reilly books online.

Report •

July 2, 2010 at 17:06:20
Thanks, that looks like it will be very useful.

Report •

Related Solutions

Ask Question