Re: Find all JPEG files in a directory
by davidrw (Prior) on Aug 11, 2005 at 20:30 UTC
|
# 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
| [reply] [Watch: Dir/Any] [d/l] |
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.
| [reply] [Watch: Dir/Any] |
Re: Find all JPEG files in a directory
by salva (Canon) 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$
+/'
| [reply] [Watch: Dir/Any] [d/l] [select] |
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. | [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Find all JPEG files in a directory
by sh1tn (Priest) on Aug 11, 2005 at 22:21 UTC
|
use File::Find;
...
sub _find_jpg {
my @files;
find( sub{ push @files, $_ if /\.jpg$/i }, @_ );
\@files;
}
| [reply] [Watch: Dir/Any] [d/l] |
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 | [reply] [Watch: Dir/Any] [d/l] |
|
"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)
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Find all JPEG files in a directory
by Transient (Hermit) on Aug 11, 2005 at 20:26 UTC
|
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)
Update:
Updated shell command (to what should work on Linux - untested because I'm on AIX) | [reply] [Watch: Dir/Any] [d/l] [select] |
|
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
/J\ | [reply] [Watch: Dir/Any] [d/l] [select] |
|
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"
| [reply] [Watch: Dir/Any] [d/l] |
|
|
|
*.jpe?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
| [reply] [Watch: Dir/Any] [d/l] [select] |
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.
| [reply] [Watch: Dir/Any] [d/l] |
|
#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.
| [reply] [Watch: Dir/Any] [d/l] |