|
google site
|
|||||||
| Forums | Register | Blog | Man Pages | Forum Rules | Links | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
![]() |
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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 | ||
|
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
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 |