PDA

View Full Version : How do I tell 'find' to skip all symlink folders?



Paddy Landau
August 7th, 2016, 02:02 PM
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.

.
├── 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:

find -P -xdev '!' -type l
How do I tell find to skip all symlink folders?

vasa1
August 7th, 2016, 02:36 PM
... I have tried the following options but to no avail:

find -P -xdev '!' -type l ...

Just to request a clarification.

Why use '!' instead of just !?

vasa1
August 7th, 2016, 02:48 PM
If I look in /usr/local, I see:
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
find -L . -type d I get
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
find . -type dI get
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.

steeldriver
August 7th, 2016, 04:56 PM
The default behaviour (without the -L command line option) should not descend into linked directories e.g. given



$ tree -l .
.
├── folderA
│ ├── folderAA
│ └── folderAB
├── folderB
│ ├── folderBA
│ └── folderBB
└── linkToOutside -> ../somewhere/else/outsideA/
└── somefile

7 directories, 1 file


then


$ find .
.
./folderB
./folderB/folderBB
./folderB/folderBA
./folderA
./folderA/folderAA
./folderA/folderAB
./linkToOutside


whereas



$ 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



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

Paddy Landau
August 8th, 2016, 03:56 PM
If I look in /usr/local, I see…

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:

$ 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.)

$ 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!)

sudodus
August 8th, 2016, 05:06 PM
I made a symbolic link from my home folder to a subdirectory in my 'data' partition


ln -s /media/multimed-2/test/test0/ test0

I tried the following command


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.


find [a-z]*/ -xdev -maxdepth 2 '!' -type l -print |grep test0

I suggest that you simplify your command :-)

steeldriver
August 8th, 2016, 05:46 PM
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