Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 02-22-2012, 01:36 AM   #1
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
Perl read dir script - regex gif|jpg

I worte a script that moves all the gif from my public_html directory to the images directory.

it works.


$dir_name = "/home/casper/public_html"; 

opendir (DIR, $dir_name) || die "Error in opening dir $dirname\n"; 
while ( ($filename = readdir (DIR))) { 
        if ($filename =~ /\w+\.gif/){
                system("cp $filename /home/casper/public_html/images/"); 
        sleep 1;  
however when i combines the jpg with the gif i got errors:


$dir_name = "/home/casper/public_html"; 

opendir (DIR, $dir_name) || die "Error in opening dir $dirname\n"; 
while ( ($filename = readdir (DIR))) { 
        if ($filename =~ /\w+\.gif|jpg/){
                system("cp $filename /home/casper/public_html/images/"); 
        sleep 1;  
I just put an or bracket in the regex - it should work - but it does not.

Is there some other way to ake the regex?
Old 02-22-2012, 10:45 AM   #2
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Your regex looks fine. Do the filenames contain whitespace? If so, you will need to enclose them in quotes in your system() commandline.
BTW, when you say 'I got errors', it would be more helpful to show specifically what errors you got.
--- rod.

Last edited by theNbomr; 02-22-2012 at 10:46 AM.
Old 02-22-2012, 10:47 AM   #3
Senior Member
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Also --

Originally Posted by casperdaghost
however when i combines the jpg with the gif i got errors
What errors?

You should have the use warnings pragma in your Perl script.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Postscript to .gif or .jpg conversion program? stf92 Slackware 7 08-23-2011 09:42 PM
[SOLVED] GIF and JPG file formats. stf92 Linux - Newbie 2 08-19-2011 01:58 AM
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 01:10 AM
[SOLVED] Read file names in a Dir and its Sub Dir in Perl sagarkha Programming 2 04-28-2010 04:58 PM
jpg to gif conversion damonj Linux - General 2 11-03-2008 06:13 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:04 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration