LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-15-2005, 08:19 AM   #1
Melsync
Member
 
Registered: Sep 2005
Posts: 75

Rep: Reputation: 15
if test for a file name pattern


I'm sorry if this is too basic.
I need to pin some files down in a directory to make a bash script with them.
I want to select the file containing '*-reduced.html' string in its name among these ones:
200512-reduced.html
200512-reduced.jpg
200512-reduced.zip
200512-snapshot.html
200512-snapshot.jpg
200512-snapshot.zip
and many others like 200511-another.txt, etc.

$ snp=*-reduced.html
$ echo $snp
200512-reduced.html
$ if [[ -e $snp ]]; then echo "html exits"; else echo "no html"; fi
no html


Why? The variable is populated with the name of a file!

I have no idea of perl to deal with regular expressions, so I'd prefer your help in bash shell only so I can progress from there
Thanks.
 
Old 12-15-2005, 08:34 AM   #2
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
Code:
#!/bin/sh

START_LOCATION=/home
FILE_PATTERN='*-reduced.html'

# Search a directory tree
for i in `find $START_LOCATION -name $FILE_PATTERN `; do

  echo $i

done

# Search a single directory

for i in `ls $START_LOCATION/$FILE_PATTERN`; do
   echo $i
done
 
Old 12-15-2005, 08:36 AM   #3
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
Reading through you post again, I'm not sure what you are after. Do you just want a yes/no if the file of that pattern exists in a directory?
 
Old 12-15-2005, 08:57 AM   #4
Melsync
Member
 
Registered: Sep 2005
Posts: 75

Original Poster
Rep: Reputation: 15
Thanks.
No, I'm not after a boolean; I need to do all sorts of things (basename, rename, zip, rm, etc.) to that precise file while keeping all the other files in the directory unchanged.
I need to be able to do for instance
zip $(basename $FILE_PATTERN .html).zip $FILE_PATTERN
in a loop and then move on to the next file manipulation loop like
FILE_ANOTHER_PATTERN='*-snapshot.html'
for FILE_ANOTHER_PATTERN in *
do
 
Old 12-15-2005, 08:59 AM   #5
Melsync
Member
 
Registered: Sep 2005
Posts: 75

Original Poster
Rep: Reputation: 15
sorry, I hit the 'button' post too soon:

for FILE_ANOTHER_PATTERN in *
do
rename $(basename $FILE_ANOTHER_PATTERN -snapshot.html) -reduced.html
done
 
Old 12-15-2005, 09:10 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,288

Rep: Reputation: 174Reputation: 174
simpler:
Code:
if [ -e *.html ];then
  blah blah
fi
 
Old 12-15-2005, 09:15 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,288

Rep: Reputation: 174Reputation: 174
Exclamation

doh!
the above doesn't work in bash
crappy bash.




otherwise,
is it too many [[]]
try
[ -e "$snp" ]
 
Old 12-15-2005, 09:15 AM   #8
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
Code:
#!/bin/sh

START_LOCATION=/home
FILE_PATTERNS="*-reduced.html \
               *-whatever.html \
               *-linux.html"

# Search a directory tree
for PAT in $FILE_PATTERNS; do
   echo "Processing pattern $PAT"
   for i in `find $START_LOCATION -name $PAT `; do
      echo Processing file $i
      # gzip $i
   done
done
 
Old 12-15-2005, 09:49 AM   #9
Melsync
Member
 
Registered: Sep 2005
Posts: 75

Original Poster
Rep: Reputation: 15
Quote:
is it too many [[]]
try
[ -e "$snp" ]
Yes, it worked!
 
Old 12-15-2005, 09:51 AM   #10
Melsync
Member
 
Registered: Sep 2005
Posts: 75

Original Poster
Rep: Reputation: 15
Quote:
FILE_PATTERNS="*-reduced.html \
*-whatever.html \
*-linux.html"
did the trick as well.

Thanks x10 guys!

P.S.
I changed #!/bin/sh to #!/bin/bash in the script and it worked as well.
Why so much irritation about bash? I don't know anything about other shells!
 
Old 12-15-2005, 09:53 AM   #11
Melsync
Member
 
Registered: Sep 2005
Posts: 75

Original Poster
Rep: Reputation: 15
By 'irritation' I don't mean in this post, but in other posts I read.
 
Old 12-15-2005, 10:48 AM   #12
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
bash is the best of the bunch and far better than the putrid shell that MS tries to pass off.
 
Old 12-15-2005, 05:42 PM   #13
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,287

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
bash is the default shell in Linux, capabilities based on commercial ksh which is common on commercial Unices (sic).
It only appears to be irritating (here) because it's a programming lang, but newer people tend to hope that doing what seems simple to a human is so to a computer. It ain't so.
IOW, all langs take some time to learn....
 
  


Reply


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
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 06:17 AM
pattern file with no return character ksun Linux - Newbie 1 12-28-2004 06:40 PM
rename file with a pattern sujith_deva Programming 7 12-16-2004 07:39 PM
Grep pattern first line of a file ericcarlson Linux - Newbie 11 07-20-2004 10:51 AM
how to cp filestructure for specific file pattern mizuki Linux - Newbie 5 05-24-2003 02:58 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration