LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 07-05-2012, 12:38 PM   #1
Freddythunder
LQ Newbie
 
Registered: Apr 2012
Posts: 11

Rep: Reputation: Disabled
Condition in cp/ls | grep (regex, now I have two problems)


Hello,

I'm not sure where to post this question on the forum, but would like to see if someone could help me. I am still totally noob so I guess it's fitting here.

I have a situation where I have a folder with a bunch of image files. They are all pngs, but I need one copy with the extension and one copy without. The copy with is for HTML and the one without is for SEO friendly URL to tell Flash what to pull in and flash uses the file without. It's not ideal, but it is how it is and firefox loads the extension-less file, but the loading "wheel" at the top of the browser just spins forever when all is loaded.

I feel obligated to tell you, I have already created a script in PHP that does the task I want, but I would much rather find a command that will run in BASH so I can set up a shell and run it with cron and also run it through SSH. I know I can do both with the PHP script, but this problem is eating at me...

Anyway, I need a command (or series of commands) that will find all the extension-less png files that do NOT have a matching file.png in the same directory while ignoring .php, .zip, . & ..

I've found that I can list all extension-less files with
Code:
ls |grep -v '\.'
And I can list all .png files with
Code:
ls |grep ^.*\.png$
But how o how can I search with a condition? I tried using () in the command but BASH does not care for it.

Thanks!
 
Old 07-05-2012, 02:34 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Assuming all the files in the directory with no suffix are as you describe:

Code:
for nosuf in $(ls |grep -v '\.')
do if [ ! -f ${nosuf}.png ]
   then echo PNG file for $nosuf does not exist
   fi
done
You might even add a test to verify the file without a suffix IS a png file by running the "file" command on it.

Code:
for nosuf in $(ls |grep -v '\.')
do if file -b $nosuf |grep PNG
   then if [ ! -f ${nosuf}.png ]
        then echo PNG file for $nosuf does not exist
        fi
   fi
done

Last edited by MensaWater; 07-05-2012 at 02:35 PM.
 
Old 07-05-2012, 02:39 PM   #3
Freddythunder
LQ Newbie
 
Registered: Apr 2012
Posts: 11

Original Poster
Rep: Reputation: Disabled
cool. Looks like that will work. Thanks!
 
Old 07-05-2012, 06:02 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Lateral thinking here; does it really matter if there is already a png file; why not not simplify the code and just create a .png for every extension-less file?
 
Old 07-05-2012, 06:12 PM   #5
Freddythunder
LQ Newbie
 
Registered: Apr 2012
Posts: 11

Original Poster
Rep: Reputation: Disabled
That does solve a problem for a lookup, but I also want to list it out so I can read what images do not have that matching counterpart. I was hoping to get a regex I could use in ls |grep or somthing like that in one shot, but it looks like I'll be looping through. But thanks!
 
Old 07-05-2012, 06:26 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Chill mate; far too many people try to do too much in one line of code; a short script is no big deal, honest
Not only that, but complex single lines of code are a b*gger to understand/debug; believe me I've seen lots
 
Old 07-06-2012, 08:39 AM   #7
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Quote:
Originally Posted by Freddythunder View Post
That does solve a problem for a lookup, but I also want to list it out so I can read what images do not have that matching counterpart. I was hoping to get a regex I could use in ls |grep or somthing like that in one shot, but it looks like I'll be looping through. But thanks!
ls |sort

Should put all your files in sort order by name so you'd see something like:

filea
filea.png
fileb
fileb.png
filec
filed
filed.png
filef
fileg
fileh
fileh.png

From such a list you can see that filec, filed, filef and fileg do not have corresponding .png files whereas filea, fileb, filed and fileh do.
 
  


Reply



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
grep regex with wildcard yknot7 Linux - Newbie 6 11-10-2011 01:11 AM
[SOLVED] Combining regex in grep devUnix Linux - General 2 09-06-2011 11:11 AM
trying grep inside script, w/ OR condition in quotes '$A\|$B' wipeout Programming 7 05-20-2011 12:29 AM
grep regex . matches new lines?! lambchops468 Linux - Newbie 3 03-24-2008 09:19 PM
regex in ls vs. grep jhwilliams Linux - Software 2 08-10-2007 10:14 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:13 PM.

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