LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   listing within a folder and display success message upon matching pattern (http://www.linuxquestions.org/questions/linux-general-1/listing-within-a-folder-and-display-success-message-upon-matching-pattern-940497/)

nwazsohail 04-18-2012 12:37 PM

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.

TobiSGD 04-18-2012 01:31 PM

So what have you done already and where do you have problems?

nwazsohail 04-18-2012 02:44 PM

i do not know how to have loop on directory?

TobiSGD 04-18-2012 02:52 PM

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

nwazsohail 04-18-2012 03:10 PM

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.

TobiSGD 04-18-2012 03:27 PM

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.


All times are GMT -5. The time now is 11:44 PM.