95

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 ?

Barth
  • 15,135
  • 20
  • 70
  • 105
  • 1
    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](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww Aug 10 '18 at 12:21
  • 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. – Zak Jul 27 '23 at 18:06

6 Answers6

121

Check the man page again ;) It's:

find /path/to/files -type f

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

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • 1
    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 :) – Barth Apr 30 '13 at 15:32
  • 3
    here it matched symlinks pointing to regular files, I had to use `-type f -xtype f` – Aquarius Power Feb 16 '16 at 01:54
  • @AquariusPower I cannot reproduce that. Which version of `find` are you using? – hek2mgl Feb 16 '16 at 05:36
  • 4
    `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 f` – Aquarius Power Feb 16 '16 at 19:39
  • I get two times `./asdf` which seems to prove that `-type f` delivers the same results.. – hek2mgl Feb 16 '16 at 20:39
  • 1
    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 around – Mike Q Jan 04 '20 at 05:34
  • 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. – StoneThrow Jan 17 '22 at 20:15
  • @StoneThrow Wasn't able to reproduce this. I suggest to open a question if the problem still exists. – hek2mgl Jan 18 '22 at 21:54
38
! -type l

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

find /usr/bin/ \! -type l
Anthony Geoghegan
  • 11,533
  • 5
  • 49
  • 56
  • No -type l will search ONLY FOR symbolic links. Checkout man page – laserbeamer Dec 04 '15 at 00:01
  • 7
    Negating test with "!" (pling) - which should be escaped if necessary - will return anything that ISN'T a symlink. – MikeW Dec 15 '15 at 10:36
  • 2
    This should be the correct answer because it worked for me opposed to -type f which allowed in broken symlinks fyi – Mike Q Jan 04 '20 at 05:36
  • While searching in a directory hierarchy it may be useful to exclude directories too with `! -type d` – lfurini Mar 11 '21 at 14:04
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.
ron rothman
  • 17,348
  • 7
  • 41
  • 43
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.

Nande
  • 409
  • 1
  • 6
  • 11
  • 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. – MikeW Dec 15 '15 at 10:39
2

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.

lfurini
  • 3,729
  • 4
  • 30
  • 48
Wellington1993
  • 340
  • 1
  • 3
  • 17
0

This works for me:

find -H . -maxdepth 1 -type f

Actually, don't really need the -H

peterh
  • 11,875
  • 18
  • 85
  • 108
Joyce M
  • 1
  • 1
  • -type f didn't work for me, I still was getting broken symlinks ! -type l mentioned above worked for me – Mike Q Jan 04 '20 at 05:41