Results 1 to 7 of 7

Thread: How do I tell 'find' to skip all symlink folders?

  1. #1
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    5,263
    Distro
    Ubuntu

    Question How do I tell 'find' to skip all symlink folders?

    This must have been answered somewhere, but Google as I might, I cannot find the answer.

    According to the find manual, you can use -P (which is default anyway) to prevent it from following symlinks.

    But, this applies only to flies. What I want is for find to not follow any folders that are symlinks.

    For example, suppose the folder structure is as follows.
    Code:
    .
    ├── folderA
    │   ├── folderAA
    │   └── folderAB
    ├── folderB
    │   ├── folderBA
    │   └── folderBB
    └── linkToOutside -> /somewhere/else/outsideA/
    I want to use find, but tell it to search only folderA and folderB, and exclude folder linkToOutside (because it is a symlink).

    I have tried the following options but to no avail:
    Code:
    find -P -xdev '!' -type l
    How do I tell find to skip all symlink folders?
    Always make regular backups of your data (and test them).
    Visit Full Circle Magazine for beginners and seasoned Linux enthusiasts.

  2. #2
    Join Date
    Dec 2007
    Beans
    12,521

    Re: How do I tell 'find' to skip all symlink folders?

    Quote Originally Posted by Paddy Landau View Post
    ... I have tried the following options but to no avail:
    Code:
    find -P -xdev '!' -type l
    ...
    Just to request a clarification.

    Why use '!' instead of just !?

  3. #3
    Join Date
    Dec 2007
    Beans
    12,521

    Re: How do I tell 'find' to skip all symlink folders?

    If I look in /usr/local, I see:
    Code:
    07:13 PM /usr/local $ ls -AlF
    total 32
    drwxr-xr-x 2 root root 4096 Feb 11 18:20 bin/
    drwxr-xr-x 2 root root 4096 Jul 23  2014 etc/
    drwxr-xr-x 2 root root 4096 Jul 23  2014 games/
    drwxr-xr-x 2 root root 4096 Jul 23  2014 include/
    drwxr-xr-x 4 root root 4096 Apr 20 14:52 lib/
    lrwxrwxrwx 1 root root    9 Nov 14  2014 man -> share/man/
    drwxr-xr-x 2 root root 4096 Jul 23  2014 sbin/
    drwxr-xr-x 9 root root 4096 Apr 20 15:17 share/
    drwxr-xr-x 2 root root 4096 Jul 23  2014 src/
    07:14 PM /usr/local $
    If I'm in /usr/local and run
    Code:
    find -L . -type d
    I get
    Code:
    07:14 PM /usr/local $ find -L . -type d
    .
    ./etc
    ./bin
    ./sbin
    ./lib
    ./lib/python2.7
    ./lib/python2.7/dist-packages
    ./lib/python2.7/site-packages
    ./lib/python3.5
    ./lib/python3.5/dist-packages
    ./games
    ./share
    ./share/xml
    ./share/xml/schema
    ./share/xml/misc
    ./share/xml/declaration
    ./share/xml/entities
    ./share/fonts
    ./share/applications
    ./share/ca-certificates
    ./share/sgml
    ./share/sgml/dtd
    ./share/sgml/misc
    ./share/sgml/declaration
    ./share/sgml/stylesheet
    ./share/sgml/entities
    ./share/man
    ./share/emacs
    ./share/emacs/site-lisp
    ./man
    ./src
    ./include
    07:14 PM /usr/local $
    But if I run
    Code:
    find . -type d
    I get
    Code:
    07:16 PM /usr/local $ find . -type d
    .
    ./etc
    ./bin
    ./sbin
    ./lib
    ./lib/python2.7
    ./lib/python2.7/dist-packages
    ./lib/python2.7/site-packages
    ./lib/python3.5
    ./lib/python3.5/dist-packages
    ./games
    ./share
    ./share/xml
    ./share/xml/schema
    ./share/xml/misc
    ./share/xml/declaration
    ./share/xml/entities
    ./share/fonts
    ./share/applications
    ./share/ca-certificates
    ./share/sgml
    ./share/sgml/dtd
    ./share/sgml/misc
    ./share/sgml/declaration
    ./share/sgml/stylesheet
    ./share/sgml/entities
    ./share/man
    ./share/emacs
    ./share/emacs/site-lisp
    ./src
    ./include
    07:17 PM /usr/local $
    In other words, the man folder isn't listed.

  4. #4
    Join Date
    Apr 2012
    Beans
    7,256

    Re: How do I tell 'find' to skip all symlink folders?

    The default behaviour (without the -L command line option) should not descend into linked directories e.g. given

    Code:
    $ tree -l .
    .
    ├── folderA
    │   ├── folderAA
    │   └── folderAB
    ├── folderB
    │   ├── folderBA
    │   └── folderBB
    └── linkToOutside -> ../somewhere/else/outsideA/
        └── somefile
    
    7 directories, 1 file
    then
    Code:
    $ find .
    .
    ./folderB
    ./folderB/folderBB
    ./folderB/folderBA
    ./folderA
    ./folderA/folderAA
    ./folderA/folderAB
    ./linkToOutside
    whereas

    Code:
    $ find -L . 
    .
    ./folderB
    ./folderB/folderBB
    ./folderB/folderBA
    ./folderA
    ./folderA/folderAA
    ./folderA/folderAB
    ./linkToOutside
    ./linkToOutside/somefile
    (note the additional result ./linkToOutside/somefile)

    If you don't want to even list links, you can do

    Code:
    $ find . ! -type l
    .
    ./folderB
    ./folderB/folderBB
    ./folderB/folderBA
    ./folderA
    ./folderA/folderAA
    ./folderA/folderAB

  5. #5
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    5,263
    Distro
    Ubuntu

    Re: How do I tell 'find' to skip all symlink folders?

    Quote Originally Posted by vasa1 View Post
    If I look in /usr/local, I see…
    Quote Originally Posted by steeldriver View Post
    The default behaviour (without the -L command line option) should not descend into linked directories…
    @vasa1 and @steeldriver, thank you for your responses.

    I am utterly befuddled.

    I get the same responses as you with your examples. But — which is the reason why I posted — it doesn't work on my home folder!

    My home folder has two symbolic links:
    Code:
    $ ls -ld li* Pl*
    lrwxrwxrwx 1 paddy paddy 20 Aug  8 15:47 linkToOutside -> /home/paddy/outsideA
    lrwxrwxrwx 1 paddy paddy 37 Jun  6  2014 PlayOnLinux's virtual drives -> /home/paddy/.PlayOnLinux//wineprefix/
    Yet find always descends into that folder. (Many output lines omitted for readability.)
    Code:
    $ find [a-z]*/ -xdev -maxdepth 2 '!' -type l -print
               :          :          :          :
    linkToOutside/
    linkToOutside/outsideAA
    linkToOutside/outsideAB
               :          :          :          :
    PlayOnLinux's virtual drives/
    PlayOnLinux's virtual drives/default
    PlayOnLinux's virtual drives/Quicken
    PlayOnLinux's virtual drives/Quicken/dosdevices
    PlayOnLinux's virtual drives/Quicken/drive_c
               :          :          :          :
    Why does this command descend into the symlinks? What am I missing? (I feel truly stupid!)
    Last edited by Paddy Landau; August 8th, 2016 at 03:57 PM. Reason: Remove extraneous wording.
    Always make regular backups of your data (and test them).
    Visit Full Circle Magazine for beginners and seasoned Linux enthusiasts.

  6. #6
    Join Date
    Nov 2011
    Location
    /dev/root
    Beans
    Hidden!

    Re: How do I tell 'find' to skip all symlink folders?

    I made a symbolic link from my home folder to a subdirectory in my 'data' partition

    Code:
    ln -s /media/multimed-2/test/test0/ test0
    I tried the following command

    Code:
    find ~ -ls | grep '/test0 '
    and it finds only the link itself, does not descend into it, while your command (translated to look at test0) lists the content.

    Code:
    find [a-z]*/ -xdev -maxdepth 2 '!' -type l -print |grep test0
    I suggest that you simplify your command

  7. #7
    Join Date
    Apr 2012
    Beans
    7,256

    Re: How do I tell 'find' to skip all symlink folders?

    Agree with the above - I think it's because you're telling it to find within folders matching [a-z]*/ (one of which is the linked directory)

    In other words, the point at which 'find' starts to do its thing, you are already the other side of the symlink
    Last edited by steeldriver; August 8th, 2016 at 06:38 PM. Reason: suggested workaround... doesn't

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •