Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Find all JPEG files in a directory

by tech2040 (Novice)
on Aug 11, 2005 at 20:22 UTC ( #483117=perlquestion: print w/replies, xml ) Need Help??
tech2040 has asked for the wisdom of the Perl Monks concerning the following question:

Greetings all, I am trying to automate a few small file processes using perl. I need to create a script that will read a given linux directory, find all of the JPEG files and list the file names in a plain text file. The problem is that I have no idea how to do this in perl. I need help... ...please help me. Any assistance will be greatly appreciated.

2005-08-16 Retitled by Arunbear, as per Monastery guidelines
Original title: 'File IO'

Replies are listed 'Best First'.
Re: Find all JPEG files in a directory
by davidrw (Prior) on Aug 11, 2005 at 20:30 UTC
    First, how do you defing "find JPEG"? By extension only? by content? File::Find (or File::Find::Rule) and File::MimeInfo::Magic are you starting places for doing it in perl. However, could you just do this in the shell instead?
    # just a driectory: ls /some/dir/*.jpg > /tmp/list.txt # recursive solution: find /some/dir/ -name *.jpg > /tmp/list # a directory, but look at content: file /some/dir/* | grep -i jpeg
Re: Find all JPEG files in a directory
by izut (Chaplain) on Aug 11, 2005 at 23:35 UTC

    I think it is useful for making it portable across different plataforms - win32 doesn't have 'find', unless you install cygwin. It is a good exercise for beginners.

    You can use File::Find to find files and then File::MimeInfo or File::MMagic to check its file type. If you want to extract some data from images (size, resolution, etc) you could use PerlMagick .

    Update: Figured out that File::Info is broken under Windows. File::MMagic worked well in my tests with images, but not with mp3 files.

    Igor S. Lopes - izut
    surrender to perl. your code, your rules.
Re: Find all JPEG files in a directory
by salva (Abbot) on Aug 11, 2005 at 20:39 UTC
    you can use the file command to obtain the type of the files:
    $ file -i /foo/bar/* | perl -ne 'print "$1\n" if /(.*):\s*image\/jpeg$ +/'
Re: Find all JPEG files in a directory
by chibiryuu (Beadle) on Aug 11, 2005 at 21:07 UTC
    find2perl might give you some hints to how to do this in Perl.  Which will work on stupid systems like Windows which don't have "find".  (Well, the command "find" really means "fgrep", and "dir/b/s" kinda works like "find"....)
    use File::Find; find( sub {/\.jpe?g$/i and print "$File::Find::name\n"}, @dirs, );
    perldoc File::Find But usually I'd do what all the other comments suggest, use "find", if I were on a non-Windows system.
Re: Find all JPEG files in a directory
by sh1tn (Priest) on Aug 11, 2005 at 22:21 UTC
    Sample subroutine:
    use File::Find; ... sub _find_jpg { my @files; find( sub{ push @files, $_ if /\.jpg$/i }, @_ ); \@files; }

Re: Find all JPEG files in a directory
by gri6507 (Deacon) on Aug 11, 2005 at 20:29 UTC
    I know you want to do this a perl-ish way, but such a task could be just as easily be done from the command line -  find . | grep jpg
      "grep jpg"? No login for you on my systems. :) One of these will actually find most if not all of the files with a JPEG extension without matching files like "not_a_jpg.gif". Whenever you have a fairly simple match, you'll come out ahead using the find built-ins rather than piping to grep.

      find . -name '*.jpg' -o -name '*.jpeg' -o -name '*.JPG' -o -name '*.JPEG'

      find ./ -name '*.[Jj][Pp][Gg]' -o -name '*.[Jj][Pp][Ee][Gg]'

      And if you must use grep, do it right!

      find . | grep -i '[.]jpe\?g$'

      find . | egrep -i '[.]jpe?g$'

      $you = new YOU;
      honk() if $you->love(perl)

Re: Find all JPEG files in a directory
by Transient (Hermit) on Aug 11, 2005 at 20:26 UTC
    with Perl you can use File::Find. But you should be able to do this from the command line (or script):
    find /path_to_start_from -name *.jpe?g 2>/dev/null > output.txt
    find /path_to_start_from -name "*.jp{,e}g" 2>/dev/null > output.txt
    (I didn't check the command itself, but that should be close)

    Updated shell command (to what should work on Linux - untested because I'm on AIX)

      I don't know about your find command but mine doesn't do that - I think you'll need to use grep:

      find ../images | egrep '\.jpe?g$'
      alternatively if just one directory level is required then you could ls - with bash you can do:
      shopt -s extglob; ls ../images/*.jp?(e)g; shopt -u extglob


        You're right on that one... this is supposed to be supported, but I can't get it to work on AIX:
        find . -name "*.jp{,e}g"


      The shell might have something to say about that...

      (Actually, Bourne-style shells will still pass it through in the likely event that nothing matches that glob, but it's not very safe.)

      edit: And the only way to get the match you want from find is to use something like:

      find dir -name \*.jpg -or -name \*.jpeg

Re: Find all JPEG files in a directory
by spiritway (Vicar) on Aug 12, 2005 at 05:03 UTC

    Since this is a Linux environment, you can use the 'ls' command:

    ls *.jp* > textfile

    Not Perl, but it works in Linux.

      #perl use File::Slurp; write_file ('filenameW', $ENV{OS} =~ /Windows/i ? `dir /b *.jpg` : `ls + *.jpg` );
      Update: changed single qutoes to backticks as originally intended.

      holli, /regexed monk/

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://483117]
Approved by planetscape
and the daffodils sway...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2017-03-30 21:05 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (364 votes). Check out past polls.