Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
google site



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Display Modes
  #1  
Old 03-11-2009
Registered User
 

Join Date: Mar 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Cool help with find function

Hi,

Im using a find function to find a specific file and print it.
eg.
find /path -name filename -type f -print -exec more '{}' \;

The problem I have is that I need to know if the file has not been fount (ie it does not exist).
I had a look at the find function and could not find anything relevant. I also tried adding another -exec statement that would increment a counter, giving me a number of instances but -exec doesnt seem to accept most shell commands.
Huh, this should be so simple and its been driving me nuts

Also, while at it, is there any way to only process one result... eg. only print the first instance of the file found?

Any ideas?

Thanks!!!
Sponsored Links
  #2  
Old 03-11-2009
Registered User
 

Join Date: Aug 2008
Location: Montreal, Qc, CA
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
You can try something like this :


Code:
find /path -name filename -type f > /tmp/list
if [ $? -ne 0 ]; then
   echo "File not found"
else
   echo "Number of occurence of filename : \c"
   cat /tmp/list | wc -l
   echo "First occurence is : \c"
   head -n1 /tmp/lst
fi

  #3  
Old 03-11-2009
Registered User
 

Join Date: Mar 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
I should mention that I do not want to use any external files... It all needs to be executed within a single script...

Thanks for the idea though!

Its a very simple function the Im writing that finds and prints a specific file...but the location of the file can vary which is why I'm using find.
Also, I want to be able to print an error if the file does not exist.
  #4  
Old 03-11-2009
Registered User
 

Join Date: Aug 2008
Location: Montreal, Qc, CA
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
If you dont want an external file you can do this like that :


Code:

find /path -name filename -type f >/dev/null
if [ $? -ne 0 ]; then
   echo "File not found"
else
   i=1
   for file in `find /path -name filename -type f`
   do
      if [ $i -eq 1 ]; then
         echo "First occurence is : $file"
      fi
      i=$(($i + 1))
   done
   echo "Number of occurence of filename : $(($i - 1))
fi

  #5  
Old 03-12-2009
Registered User
 

Join Date: Mar 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Excellent...

Thanks, that will work.
I tried using $? previously but it tends to return 0 even if the file does not exist.
the "for file in" idea works though.

Thanks very much!!!
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
script to find function names samantha grace Shell Programming and Scripting 2 12-22-2008 06:31 AM
Function to find day of any given date. RRVARMA Shell Programming and Scripting 5 05-12-2008 03:19 AM
How can I execute own ksh function in find -exec piooooter Shell Programming and Scripting 11 04-03-2007 06:44 AM
FIND function - system wide DGoubine UNIX for Dummies Questions & Answers 5 02-03-2005 03:58 AM
find function pkumar_syd UNIX for Dummies Questions & Answers 2 06-23-2002 11:17 PM



All times are GMT -4. The time now is 02:34 AM.