[pm-h] simple perl directory+file task

Lanny Ripple lanny.ripple at gmail.com
Tue Oct 2 08:36:01 PDT 2012


Hi Russell,

You might review the docs for find2perl[1] and also examining the output of

  find2perl . -type f -print

Building your project in perl will give you a lot of added flexibility but just because you have a swiss army chainsaw doesn't mean every problem is a... thing you would drive into a piece of wood with a chainsaw.  (Doesn't just trip off the tongue does it?)

Consider

  $ cat > /tmp/name_me <<'END'
  #!/bin/bash
  echo $(basename $1) >> $1
  END
  $ chmod 755 /tmp/name_me
  $ cd datafiles
  $ find . -type f -print | xargs -n 1 /tmp/name_me

and your task is also accomplished.  Perl can do everything bash and sed can (and awk too but use perl instead) but sometimes you don't need perl when simpler tools will do.

  Enjoy,
  -ljr

[1] http://perldoc.perl.org/find2perl.html

On Oct 2, 2012, at 10:00 AM, "Russell L. Harris" <rlharris at oplink.net> wrote:

> I am looking forward to the meeting on the 11th.  It is simple tasks
> of the following sort which I hope the presentation is going to teach
> me how best to approach:
> 
> --------------------------
> SITUATION: A directory "datafiles/" contains files named "red.doc",
> "green.dat", "yellow.tex", "blue.txt", etc.
> 
> NEED:  Open each file in the directory, and append to the end of each
> file the file name, so that file "red.doc" reads:
> 
>     <existing text, if any>
>     red.doc
> 
> file "yellow.tex" reads:
> 
>     <existing text>
>     yellow.tex
> 
> and so forth. 
> --------------------------
> 
> Of course, the next step is recursion into sub-directories.  From my
> reading I think I see how to handle recursion, but obtaining the
> filename and stepping through a directory is the immediate hurdle.  
> 
> It seems to me that the books and tutorials typically do not spend
> enough time explaining the fundamental concepts.
> 
> RLH
> _______________________________________________
> 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