LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-18-2012, 12:37 PM   #1
nwazsohail
LQ Newbie
 
Registered: Apr 2012
Posts: 7

Rep: Reputation: Disabled
listing within a folder and display success message upon matching pattern


i want to write a script which check within a folder and check for specific pattern upon a file is matched it should give me a success message.

for example a folder test contains file1 file2 file3 and onward. scripts should check for all files in test and if file1 is present it should give me the success message.

thanks and regards.
 
Old 04-18-2012, 01:31 PM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
So what have you done already and where do you have problems?
 
Old 04-18-2012, 02:44 PM   #3
nwazsohail
LQ Newbie
 
Registered: Apr 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
i do not know how to have loop on directory?
 
Old 04-18-2012, 02:52 PM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
You don't need a loop for doing this. Have a look at the find command, that is what you are looking for. For example:
Code:
find $DIRECTORY -name "$PATTERN"
will find the files that match $PATTERN in the directory $DIRECTORY.
For more information look at
Code:
man find
 
Old 04-18-2012, 03:10 PM   #5
nwazsohail
LQ Newbie
 
Registered: Apr 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
thanks for the great hint. but find produced the one part that it sorted out looking into the directory and match the pattern.

i want that if i got file1 in the result it should give some sort of message.


i found that exec switch would be helpful in this case but don't know how i can implement some condition within a find command.

thanks in advance.
 
Old 04-18-2012, 03:27 PM   #6
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
You don't do that in the find command. Find is printing one line per matching file to stdout. Just count the lines. If the number of lines is 0 then no match was found, if it is greater than 0 there were matches.
To store the number of lines just pipe the output of find through wc -l:
Code:
LINES=$(find $DIRECTORY -name "$PATTERN" | wc -l)
Then use if or case to print out whatever you want based on the contents of $LINE.
 
  


Reply

Tags
scripting



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] awk with pipe delimited file (specific column matching and multiple pattern matching) lolmon Programming 4 08-31-2011 12:17 PM
[SOLVED] Pattern Matching kdate Linux - Newbie 4 05-23-2011 05:27 PM
pattern matching vinaytp Linux - Newbie 2 10-10-2009 06:06 AM
Pattern Matching Aveltium Linux - Newbie 5 04-12-2009 11:14 PM
pattern matching nadeemr Linux - Newbie 8 06-13-2007 11:05 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:39 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