Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Recursive search and delete

by roboticus (Chancellor)
on Nov 16, 2012 at 11:16 UTC ( #1004173=note: print w/replies, xml ) Need Help??

in reply to Recursive search and delete


You already have several good answers, so I'm not really adding much. However, I've not played much with opendir or readdir and I thought I'd like to play with them a bit. I didn't want a recursive solution, so I did it this way:

$ cat #!/usr/bin/perl use strict; use warnings; use 5.10.0; use autodie; my $filespec = "abc*.xml"; my @dirs = ('ABC'); while (@dirs) { my $curdir = pop @dirs; opendir(my $dh, $curdir); for my $f (readdir($dh)) { next if $f =~ /^\.\.?$/; next unless -d "$curdir/$f"; print "pushing $curdir/$f\n"; push @dirs, "$curdir/$f"; } for (glob("$curdir/$filespec")) { print "processing $_\n"; } } $ perl pushing ABC/DEF pushing ABC/GHI pushing ABC/JKL processing ABC/abcde.xml processing ABC/JKL/abc_x.xml processing ABC/JKL/abc_y.xml processing ABC/JKL/abc_z.xml pushing ABC/GHI/baz pushing ABC/GHI/foo pushing ABC/GHI/bar processing ABC/GHI/abc_5.xml processing ABC/GHI/abc_6.xml processing ABC/GHI/bar/abc_1.xml processing ABC/GHI/bar/abc_2.xml processing ABC/GHI/bar/abc_3.xml processing ABC/GHI/bar/abc_4.xml processing ABC/GHI/foo/abc_8.xml processing ABC/GHI/foo/abc_9.xml processing ABC/GHI/foo/abc_a.xml processing ABC/GHI/foo/abc_b.xml processing ABC/GHI/baz/abc_{7}.xml

It's not particularly pretty, but it amused me for a few minutes. I didn't actually let it do file deletions because I'm loath to do automatic deletes until I've well tested such a script. It's a good thing, too. On my earlier attempt, a pair of bugs conspired to make the script "process" all "abc*.xml" files on my hard drive, rather than just those in the ABC directory.


When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^2: Recursive search and delete
by tobyink (Abbot) on Nov 16, 2012 at 13:03 UTC

    "I didn't actually let it do file deletions because I'm loath to do automatic deletes until I've well tested such a script."

    One nifty trick I sometimes use in situations like that is to make my script output text like this to STDOUT:

    rm ABC/JKL/abc_x.xml rm ABC/JKL/abc_y.xml rm ABC/JKL/abc_z.xml

    Then when I've played with it a few times, and I'm confident it's outputting the right commands, pipe the output into sh:

    perl | sh

    This is also good if you need to do a bunch of complex database changes. Just write SQL to STDOUT, and once you're sure it's OK, pipe it into your favourite command-line SQL tool.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1004173]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2018-05-25 11:38 GMT
Find Nodes?
    Voting Booth?