[K12OSN] OT: bash scripting - check for permissions, check for existence of file in directory tree

Nils Breunese nils at breun.nl
Wed Sep 24 12:10:02 UTC 2008


Rob Owens wrote:

> Nils Breunese wrote:
>> Rob Owens wrote:
>>
>>> Nils Breunese wrote:
>>>> Rob Owens wrote:
>>>>
>>>>> 2) I need to check for the existence of a file somewhere in a  
>>>>> directory
>>>>> tree (with lots of subdirectories).
>>>>
>>>> ----
>>>> find <directory> -name <filename>
>>>> ----
>>>>
>>> I thought of that, but suspected there might be a better way.   
>>> Guess I
>>> was wrong.
>>
>> Better in what way?
>>
> I'm not actually looking to find a file and output it, I need to  
> perform
> some action if a file exists.  Since I don't know where the file is, I
> guess I've got to find it first...

Then use the -exec option to find:

	find <directory> -name <filename> -exec <do something> \;

Use {} as a placeholder for the filename if you want to use it in your  
action. Say you want to output the contents of the file to the screen:

	find <directory> -name <filename> -exec cat {} \;

Nils Breunese.




More information about the K12OSN mailing list