How to find files excluding symbolic links?
Asked Answered
I

6

102

I want to find files in Linux that follow a certain pattern but I am not interested in symbolic links.

There doesn't seem to be an option to the find command for that.

How shall I do ?

Instinctive answered 30/4, 2013 at 15:22 Comment(2)
Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See What topics can I ask about here in the Help Center. Perhaps Super User or Unix & Linux Stack Exchange would be a better place to ask.Heroic
The comment above is misleading. A lot of devs use the find command inside of bash and shell scripts -- Of which are, in fact, both programming and development. Also someone found the question useful -- As proven by the 94 (95 after mine) upvotes.Disjoined
D
130

Check the man page again ;) It's:

find /path/to/files -type f

type f searches for regular files only - excluding symbolic links.

Descendant answered 30/4, 2013 at 15:28 Comment(8)
ha ha, yes ok, I thought that regular file included symbolic links. Sorry about that. Well you will get some points for an easy question :)Instinctive
here it matched symlinks pointing to regular files, I had to use -type f -xtype fCondensed
@AquariusPower I cannot reproduce that. Which version of find are you using?Descendant
find (GNU findutils) 4.4.2, here test case: echo >>asdf.txt;ln -s asdf.txt asdf.txt.lnk;find -type f;echo;find -type f -xtype fCondensed
I get two times ./asdf which seems to prove that -type f delivers the same results..Descendant
This doesn't always work I have symblic links that are broken which show up in the find. I have another loop that checks for them with fhe file command as a work aroundHaematic
Is this expected: when searching by name using a regex likely to find both files and symlinks, if you specify -type f after the -name, then find will still return both regular files and symlinks; but if you specify -type f before the -name, then find returns files only? E.g. find /path/to -name "*.so*" -type f returns files and symlinks, and find /path/to -type f -name "*.so*" returns files only.Corollaceous
@Corollaceous Wasn't able to reproduce this. I suggest to open a question if the problem still exists.Descendant
M
38
! -type l

For example, if you want to search all regular files in /usr/bin, excluding symlink:

find /usr/bin/ \! -type l
Maggee answered 25/6, 2015 at 15:38 Comment(4)
No -type l will search ONLY FOR symbolic links. Checkout man pageMorganica
Negating test with "!" (pling) - which should be escaped if necessary - will return anything that ISN'T a symlink.Unstressed
This should be the correct answer because it worked for me opposed to -type f which allowed in broken symlinks fyiHaematic
While searching in a directory hierarchy it may be useful to exclude directories too with ! -type dChak
R
3

Do you want it to follow symlinks but not return them (if they match your pattern)?

find -H?

man find
     ...
     -H      Cause the file information and file type (see stat(2)) returned for each symbolic link specified on the command line to be those of
             the file referenced by the link, not the link itself.  If the referenced file does not exist, the file information and type will be
             for the link itself.  File information of all symbolic links not on the command line is that of the link itself.

     -L      Cause the file information and file type (see stat(2)) returned for each symbolic link to be those of the file referenced by the
             link, not the link itself.  If the referenced file does not exist, the file information and type will be for the link itself.

             This option is equivalent to the deprecated -follow primary.
Room answered 30/4, 2013 at 15:29 Comment(0)
A
3

I have readed the MAN and now it seems is -P also, using -type r would raise an error. also notice is the DEFAULT behavior now.

-P Never follow symbolic links. This is the default behaviour. When find examines or prints information a file, and the file is a symbolic link, the information used shall be taken from the properties of the symbolic link itself.

Assured answered 1/7, 2015 at 1:10 Comment(1)
Yes but the OP does not want any results coming from symlink - I presume to avoid duplicate processing, since the 'find' will probably return both the "real" file and that addressed by the symlink to the same "real" file.Unstressed
E
3

Like @AquariusPower say, the use of find -type f -xtype f solved my problem, and now I get only real files and not symbolic links anymore.

From: https://linux.die.net/man/1/find

I got:

-xtype c
The same as -type unless the file is a symbolic link. For symbolic links: if the -H or -P option was specified, true if the file is a link to a file of type c; if the -L option has been given, true if c is 'l'. In other words, for symbolic links, -xtype checks the type of the file that -type does not check.

Emrick answered 17/5, 2018 at 19:54 Comment(0)
H
0

This works for me:

find -H . -maxdepth 1 -type f

Actually, don't really need the -H

Hammerless answered 12/10, 2016 at 21:39 Comment(1)
-type f didn't work for me, I still was getting broken symlinks ! -type l mentioned above worked for meHaematic

© 2022 - 2024 — McMap. All rights reserved.