Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

How do I execute a shell command against each file in a directory and it's subdirectories? (was: looking for a quickie)

by Anonymous Monk
on Mar 18, 2003 at 18:29 UTC ( #244104=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Howdy ya'll,

I'm so out of touch with perl and I need an answer to this cause it's killing me :o! I've got to execute a shell command agianst each file in a directory and it's subdirectories ... for example

for each file available in "dir /s *.foo" i've got to execute "doit -switches $filename"

I know that perl can probably do this in a handful of lines but haven't used perl in about 3 years could someone please post a 'gimmie' ?

At one time I was like a 6th level monk here, now i can't even remember what my log in name was (buddha or something)

Edited 2003-03-19 by mirod: changed the title

Comment on How do I execute a shell command against each file in a directory and it's subdirectories? (was: looking for a quickie)
Re: looking for a quickie
by dragonchild (Archbishop) on Mar 18, 2003 at 18:33 UTC
    No perl required, actually.

    dir /s *.foo | xargs doit -switches

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      thanks ... seems however that on a windows machine XARGS isn't available
Re: looking for a quickie
by talexb (Canon) on Mar 18, 2003 at 18:34 UTC

    I wouldn't bother doing this with Perl; combining find and xargs should do the trick.

    $ find /usr/home/foobar -name *.foo | xargs doit -switches
    --t. alex
    Life is short: get busy!
Re: looking for a quickie
by jacques (Priest) on Mar 18, 2003 at 18:41 UTC
    You can also use backticks from the command-line. For example, to do a recursive grep you could do this:

    grep -i jacques `find / -type f -print`
    UPDATE: Next time, tell us that you are on windows machine in the beginning. That sort of changes things. :)
      LOL ... ok ok ... i'm a jerk!
      should have told you that it's a windows machine
Re: looking for a quickie
by BrowserUk (Pope) on Mar 18, 2003 at 19:08 UTC

    No perl, cygwin or 4NT required:)

    for /F %f in ('dir /s /b') do doit -switches %f

    Examine what is said, not who speaks.
    1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
    2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
    3) Any sufficiently advanced technology is indistinguishable from magic.
    Arthur C. Clarke.
      ++BUK for getting me to look at the cmd.exe docs!

      Here's another snippet -

      for /R %f in (*) do doit -switches %f

Re: looking for a quickie
by graff (Chancellor) on Mar 19, 2003 at 04:29 UTC
    Just for fun, check my home node here on perlmonks -- there's a pointer there to a script called "shloop", which provides a perl-based approach to what you want to do, and also has some handy extras (like what if you want to do something like "some_proc < orig/file.orig > new/file.new" on hundreds of files...); and it works on windows. Your particular case could be handled -- if you have the windows port of the GNU "find" utility -- as follows:
    find . -name '*.foo' | shloop -e 'doit -switches'
    (If you don't have the windows port of GNU find, you can write something in perl using File::Find, of course...)

    update: fixed the usage on gnu find.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://244104]
Approved by logan
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2014-12-17 23:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (40 votes), past polls