[pm-h] File::Find and system
Lanny Ripple
lanny.ripple at gmail.com
Tue Sep 20 16:40:49 PDT 2011
First to Russell --
perldoc is your friend. `perldoc File::Find` will give you the
documentation on File::Find and has several examples which pretty much
cover everything you are looking for.
The File::Find module, part of the standard perl install, should put
`find2perl` in your path so you can execute the following
find2perl . -type f -exec dos2unix
which will give you a perl script doing what you want and ready for
changes to implement your search-and-replace processing. Change "." to
your target directory if you want to hard-code that. If not then you'll
need to be in your target directory when you run the script.
Mark --
You can golf your readdir down. See `perldoc -f readdir`.
Enjoy,
-ljr
On 09/20/2011 05:16 PM, Mark Allen wrote:
> Find::File is definitely more cross platform and tested for edge cases, but something like this works pretty well too.
>
> == SNIP ==
>
> use strict;
> use warnings;
>
> my $base = $1 || $ENV{'PWD'};
>
> my @dirs = ( $base );
>
> foreach my $dir ( @dirs ) {
> opendir DIR, $dir;
> while ( my $file = readdir DIR ) {
> if ( -d "$dir/$file" ) {
> next if ( $file eq "." or $file eq ".." );
> push @dirs, "$dir/$file";
> next;
> }
>
> # do stuff, like maybe
> print "Now processing $dir/$file...";
> `/usr/bin/dos2unix $dir/$file`;
> print "done\n";
> }
> closedir DIR;
> }
>
> == SNIP END ==
>
> Mark
>
>
>
> ________________________________
> From: Russell L. Harris<rlharris at oplink.net>
> To: houston at pm.org
> Sent: Tuesday, September 20, 2011 4:42 PM
> Subject: [pm-h] File::Find and system
>
> Running Linux, I need to execute various system utilities (including
> "dos2unix") on multiple files in multiple directories. The file
> structure is similar to the following:
>
> foo/jan/01.txt
> foo/jan/02.txt
> foo/jan/03.txt
> ...
> foo/feb/01.txt
> foo/feb/02.txt
> foo/feb/03.txt
> ...
> foo/dec/01.txt
> foo/dec/02.txt
> foo/dec/03.txt
> ...
> bar/jan/01.txt
> bar/jan/02.txt
> bar/jan/03.txt
> ...
> bar/feb/01.txt
> bar/feb/02.txt
> bar/feb/03.txt
> ...
> bar/dec/01.txt
> bar/dec/02.txt
> bar/dec/03.txt
> ...
> foobar/jan/01.txt
> foobar/jan/02.txt
> foobar/jan/03.txt
> ...
> foobar/feb/01.txt
> foobar/feb/02.txt
> foobar/feb/03.txt
> ...
> foobar/dec/01.txt
> foobar/dec/02.txt
> foobar/dec/03.txt
> ...
>
> Regrettably, not all Linux utilities have a recursive option, and I do
> not wish to take the time to re-write and debug functions which
> already are available as a standard utilities.
>
> A very tedious approach would be to "cd" to "foo/jan/" and run
> "dos2unix *.txt", then "cd" to "foo/feb/" and run "dos2unix *.txt",
> etc.
>
> I know that a Perl script can automate the process. I just discovered
> the Perl "File::Find" module and the Perl "system" function, and now I
> am perusing the O'Reilly Perl books, trying to understand how to
> combine the two into a script.
>
> Afterward, I need to do involved search-and-replace processing on
> these files which cannot be handled with system utilities. I
> previously have used Perl scripts for similar tasks, but never on a
> multi-level directory.
>
> So, learning how to run system utilities with "File::Find" appears to
> me to be the logical first step.
>
> RLH
> _______________________________________________
> Houston mailing list
> Houston at pm.org
> http://mail.pm.org/mailman/listinfo/houston
> Website: http://houston.pm.org/
>
>
>
> _______________________________________________
> Houston mailing list
> Houston at pm.org
> http://mail.pm.org/mailman/listinfo/houston
> Website: http://houston.pm.org/
More information about the Houston
mailing list