[lug] "find" question

David Morris lists at morris-clan.net
Tue Aug 17 00:07:11 MDT 2004


On Mon, Aug 16, 2004 at 11:23:13PM -0600, Daniel Webb wrote:
> I want to search a directory tree, dereferencing symbolic
> directory links, but not dereferencing symbolic file
> links.  What is the easiest way to accomplish this?
> 
> The -follow option to find dereferences all symbolic
> links.  It seems like what I want is a new option to find:
> -follow-dir
> 
> I want to be able to do this:
> 
> find . -follow-dir \( -type f -a \! -type l \)

I could be wrong, but it sounds like what you are trying to
do is find files in a symbolically linked directory tree
which are *not* symbolic links themselves.

If that is correct, this is fairly easy to do combined with
grep and awk (all one line, but too long for email):

    find . -follow -type -f \
        -exec ls -l {} \; | grep -v ^l | awk '{print $9}'

Or, if you wanted to find *only* symbolic links to files,
just drop '-v' from the grep statement and leave everything
else the same.

Note that while you can put the pipe to grep and awk within
the exec statement (which ends with '\;'), though with a bit
of difficulty, you don't really want to do that as it'd be
much slower.  `ls`, though, must be inside the -exec
statement to avoid a command-line that is too long when
searching large directory structures.

--David




More information about the LUG mailing list