104

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 ?

2
  • 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 in the Help Center. Perhaps Super User or Unix & Linux Stack Exchange would be a better place to ask.
    – jww
    Commented Aug 10, 2018 at 12:21
  • 2
    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
    Commented Jul 27, 2023 at 18:06

6 Answers 6

132

Check the man page again ;) It's:

find /path/to/files -type f

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

8
  • 2
    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
    Commented Apr 30, 2013 at 15:32
  • 3
    here it matched symlinks pointing to regular files, I had to use -type f -xtype f Commented Feb 16, 2016 at 1:54
  • @AquariusPower I cannot reproduce that. Which version of find are you using?
    – hek2mgl
    Commented Feb 16, 2016 at 5: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 Commented Feb 16, 2016 at 19: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
    Commented Jan 4, 2020 at 5:34
40
! -type l

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

find /usr/bin/ \! -type l
4
  • No -type l will search ONLY FOR symbolic links. Checkout man page Commented Dec 4, 2015 at 0:01
  • 7
    Negating test with "!" (pling) - which should be escaped if necessary - will return anything that ISN'T a symlink.
    – MikeW
    Commented Dec 15, 2015 at 10:36
  • 3
    This should be the correct answer because it worked for me opposed to -type f which allowed in broken symlinks fyi
    – Mike Q
    Commented Jan 4, 2020 at 5:36
  • While searching in a directory hierarchy it may be useful to exclude directories too with ! -type d
    – lfurini
    Commented Mar 11, 2021 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.
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.

1
  • 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
    Commented Dec 15, 2015 at 10:39
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.

0

This works for me:

find -H . -maxdepth 1 -type f

Actually, don't really need the -H

1
  • -type f didn't work for me, I still was getting broken symlinks ! -type l mentioned above worked for me
    – Mike Q
    Commented Jan 4, 2020 at 5:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.