Moving files

pll at lanminds.com pll at lanminds.com
Mon Jan 6 12:30:21 EST 2003


In a message dated: Mon, 06 Jan 2003 11:32:55 EST
Michael O'Donnell said:

>>        perl -e 'opendir DIR,".";@f = grep { ! /^\./ && -f $_ } readdir(DIR);
>\
>>          map {($n = $_)=~ s/\s+/_/g; rename ($_, $n) } @f;'
>
>On a more serious note, I (for one) would be grateful for
>a blow-by-blow analysis of what you've done, since I'm in
>trying to learn how to look at Perl code without my eyes
>automatically crossing...

Keep in mind that perl one liners are not necessarilly the easiest 
thing to learn from, however, this is pretty straighforward.  
References are in ()s:

	perl -e	                 # execute the following snipped of code (perlrun)

	opendir DIR,"."          # open the directory "." and assign 
                                 # file handle DIR, same as open() for a file
                                 # (perlfunc)

	@f =                     # assign the following to an array named 'f'
                                 # (perlsyn)

        grep {                   # user perl's grep function to match 
                                 # what comes next in the {} against the list
                                 # of things at the end (perlfunc)

	! /^\./                  # anything not beginning with a '.' (perlre)

	&&                       # and operator (perlop)

	-f                       # file test operator (perlop)

        $_                       # magic var, default "thing" being 
                                 # operated on in a loop context usually (perlvar)

	}                        # end grep block

        readdir (DIR)            # reads from the file handle DIR (perlfunc)

        map {                    # basically creates a 'for' loop 
                                 # around the block contained in {} and
                                 # operates on the list at the end (perlfunc)

       ($n = $_)=~ s/\s+/_/g;    # Assing val of $_ to $n *after* s/\s+/_/g
                                 # (perlre, perlop for =~)

       rename ($_, $n)           # use rename to 'mv' the file from 
                                 # the old to the new name (perlfunc)

       }                         # end map block

       @f                        # the list previously created with the grep call 
			

The one liner might make more sense if formatted more like a real 
program:

    perl -e '\
             opendir DIR,".";\
             @f = grep { ! /^\./ && -f $_ } readdir(DIR);\
             map {($n = $_)=~ s/\s+/_/g;\
             rename ($_, $n) } @f;\
            '

This way it's pretty obvious what the individual steps are:

    perl -e '\                                         # exec a code snippet
         opendir DIR,".";\                             # open the cwd
         @f = grep { ! /^\./ && -f $_ } readdir(DIR);\ # find files that ! ^\.
         map {($n = $_)=~ s/\s+/_/g;\                  # replace ' ' w/ '_'
         rename ($_, $n) } @f;\                        # rename the file
         '
HTH,
-- 

Seeya,
Paul
--
Key fingerprint = 1660 FECC 5D21 D286 F853  E808 BB07 9239 53F1 28EE

	It may look like I'm just sitting here doing nothing,
   but I'm really actively waiting for all my problems to go away.

	 If you're not having fun, you're not doing it right!





More information about the gnhlug-discuss mailing list