Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

multiple file operations using perl one-liners

by sflitman (Hermit)
on May 17, 2009 at 19:12 UTC ( #764541=snippet: print w/replies, xml ) Need Help??
Description: Sometimes you have a really repetitive task to do on a bunch of files. Sound familiar? You first need to specify the files, then you need to do the same operation on each one. The shell's traditional pipeline can nicely collect the paths, and then use the perl one-liner to do the operation.

Real life example: I needed to identify all the zipfiles in a directory tree and delete all *.exe files hiding inside the zipfiles.

First collect the files:

du -b */*/ | sort -n | xcol 1 >oldfiles
This shows all the zipfiles that were in subdirectories of subdirectories, then sorts them for size so I can decide to leave out the small ones. xcol is a perl script I posted to Code section to extract the column of filenames to file oldfiles.

Then use this perl one-liner:

perl -e 'while (<>) { chop; system qq{zip -d "$_" "*exe*"} }' oldfiles

Love that Perl!


  • Comment on multiple file operations using perl one-liners
Replies are listed 'Best First'.
Re: multiple file operations using perl one-liners
by ikegami (Pope) on May 17, 2009 at 19:37 UTC

    Safer and no longer:

    perl -e'while(<>) { chomp; system zip => "-d", $_, "*exe*" }' oldfiles

    Error checking (like a simple '-Mautodie' or 'or die "system: $?\n"') wouldn't hurt either.

Re: multiple file operations using perl one-liners
by graff (Chancellor) on May 18, 2009 at 06:18 UTC
    In terms of doing shell commands on a list of items, you might want to check out this old thing that I posted here long ago. It has saved me uncountable hours of time over the years in doing routine shell stuff on arbitrary sets of files -- I still use it daily: shloop -- execute shell command on a list.
Re: multiple file operations using perl one-liners
by morgon (Curate) on May 17, 2009 at 19:43 UTC
    Am I missing something or is this far too complicated?

    Why are you sorting your files when you want to process all of them anyway?

    And I can't see a real reason for your "xcol" or an intermediate file, you can do all of it one go like this (-n does the looping here):

    du -b */*/ | perl -ne 'chomp; my $f = (split /t/)[1]; system qq +{zip -d $f "*exe*"}'
      -n + chomp can be replaced with -nl.

      That should be /\t/, I think? -a would be better than split

      du -b */*/ | perl -F\\t -lane'system zip => "-d", $F[1], "*exe* +"'

      Or shorter and simpler yet:

      du -b */*/ | perl -ne'system zip => "-d", /\t(.*)/, "*exe*"'

      My earlier comment on error checking still applies.

        That last one totally rocks, Ikegami!

        I always learn something when I post here.


        -n + chomp can be replaced with -nl
        Thanks - quite useful to know.

        Gotta re-read the perlrun-manpage.


        perl -e'system zip => -d => /\t(.*)/, "*exe*" for `du -b */*/`'
Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://764541]
[ambrus]: Corion: well sure, but that's the similar to any printed document, and the margin is diminishing with today's technology. Video games no longer have to assume that the CRTs won't show the edges of the screen.
[Corion]: If you cram your slides with that much information, they might work in an "offline" situation but not very well for a live presentation IMO
[ambrus]: Corion: in that case I also ask the people who rent the conference rooms to tell conference organizers about the available tech.
[Corion]: ambrus: No, you're misunderstanding. If you place content too far on the left/right/top/ bottom, people might not see it because the view is obstructed ;)(
[Corion]: In Amsterdam, the screen went down to the bottom of the stage (60cm above ground) and the seating was on the ground, meaning that the rows in the back couldn't see the bottom of slides.
[Corion]: There also were some columns that meant that maybe you couldn't see the left/right edge of a slide.
[ambrus]: Corion: Sure. I've had a course in a 50 seat lecture hall that has two fucking columns in the middle.
[Corion]: Talking about it, the top should be fairly visible in the situations I've experienced at least. The top is uncomfortable for people in the first three rows, but that's life ;)
[ambrus]: The pillars are there because this is in the 6th floor of building R of BME, which is an attic that was built in after the original building, which is also why the elevator doesn't go that high and the windows are tiny.
[Corion]: ambrus: Hehe ;) Yeah - such real life stuff is far more inconveniencing than wasting display area due to screen ratio problems :)

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (12)
As of 2017-09-26 10:14 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (293 votes). Check out past polls.