How to find all symbolic links pointing to any file/directory inside a given directory
Clash Royale CLAN TAG#URR8PPP
up vote
10
down vote
favorite
On this question or on this one (for example) you will get solutions on how to look for symlinks pointing to a given directory (let's call it /dir1
), while I am interested to symbolic links possibly pointing to any file/folder inside /dir1
.
I want to delete such directory but I am not sure that I am safe to do so, as on an other directory (let's call it /dir2
), I may have symlinks pointing to inner parts of /dir1
.
Further, I may have created these symlinks using absolute or relative paths.
My only help is that I know the symlinks I want to check are on a mounted filesystem, on /dir2
.
symlink
add a comment |
up vote
10
down vote
favorite
On this question or on this one (for example) you will get solutions on how to look for symlinks pointing to a given directory (let's call it /dir1
), while I am interested to symbolic links possibly pointing to any file/folder inside /dir1
.
I want to delete such directory but I am not sure that I am safe to do so, as on an other directory (let's call it /dir2
), I may have symlinks pointing to inner parts of /dir1
.
Further, I may have created these symlinks using absolute or relative paths.
My only help is that I know the symlinks I want to check are on a mounted filesystem, on /dir2
.
symlink
add a comment |
up vote
10
down vote
favorite
up vote
10
down vote
favorite
On this question or on this one (for example) you will get solutions on how to look for symlinks pointing to a given directory (let's call it /dir1
), while I am interested to symbolic links possibly pointing to any file/folder inside /dir1
.
I want to delete such directory but I am not sure that I am safe to do so, as on an other directory (let's call it /dir2
), I may have symlinks pointing to inner parts of /dir1
.
Further, I may have created these symlinks using absolute or relative paths.
My only help is that I know the symlinks I want to check are on a mounted filesystem, on /dir2
.
symlink
On this question or on this one (for example) you will get solutions on how to look for symlinks pointing to a given directory (let's call it /dir1
), while I am interested to symbolic links possibly pointing to any file/folder inside /dir1
.
I want to delete such directory but I am not sure that I am safe to do so, as on an other directory (let's call it /dir2
), I may have symlinks pointing to inner parts of /dir1
.
Further, I may have created these symlinks using absolute or relative paths.
My only help is that I know the symlinks I want to check are on a mounted filesystem, on /dir2
.
symlink
symlink
edited May 23 '17 at 11:33
Community♦
1
1
asked Aug 6 '16 at 9:36
Antonello
2801313
2801313
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
11
down vote
accepted
You can find all the symbolic links using:
find / -type l
you might want to run this as root in order to get to every place on the disc.
You can expand these using readlink -f
to get the full path of the link and you should be able to grep
the output against the target directory that you are considering for deletion:
find / -type l -exec readlink -f + | grep -F /dir2
Using find / -type l -printf '%ln'
doesn't work as you get relative links like ../tmp/xyz
which might be pointing to your target dir, but are not matched because they are not fully expanded.
1
In case of subtree it can be useful to follow symlinks:find -L /subtree -xtype l -exec readlink -f +
– ruvim
Nov 30 '17 at 11:52
add a comment |
up vote
0
down vote
In my case, the accepted answer wasn't useful (because it didn't output the link source). Here is what worked for me.
I worked around it using two -exec
clauses:
find /home/ -type l -exec readlink -nf ';' -exec echo " -> " ';' | grep "/dir2"
New contributor
add a comment |
up vote
0
down vote
With zsh
:
printf '%sn' /dir2/**/*(D@e'([[ $REPLY:P = /dir1(/*|) ]])')
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
11
down vote
accepted
You can find all the symbolic links using:
find / -type l
you might want to run this as root in order to get to every place on the disc.
You can expand these using readlink -f
to get the full path of the link and you should be able to grep
the output against the target directory that you are considering for deletion:
find / -type l -exec readlink -f + | grep -F /dir2
Using find / -type l -printf '%ln'
doesn't work as you get relative links like ../tmp/xyz
which might be pointing to your target dir, but are not matched because they are not fully expanded.
1
In case of subtree it can be useful to follow symlinks:find -L /subtree -xtype l -exec readlink -f +
– ruvim
Nov 30 '17 at 11:52
add a comment |
up vote
11
down vote
accepted
You can find all the symbolic links using:
find / -type l
you might want to run this as root in order to get to every place on the disc.
You can expand these using readlink -f
to get the full path of the link and you should be able to grep
the output against the target directory that you are considering for deletion:
find / -type l -exec readlink -f + | grep -F /dir2
Using find / -type l -printf '%ln'
doesn't work as you get relative links like ../tmp/xyz
which might be pointing to your target dir, but are not matched because they are not fully expanded.
1
In case of subtree it can be useful to follow symlinks:find -L /subtree -xtype l -exec readlink -f +
– ruvim
Nov 30 '17 at 11:52
add a comment |
up vote
11
down vote
accepted
up vote
11
down vote
accepted
You can find all the symbolic links using:
find / -type l
you might want to run this as root in order to get to every place on the disc.
You can expand these using readlink -f
to get the full path of the link and you should be able to grep
the output against the target directory that you are considering for deletion:
find / -type l -exec readlink -f + | grep -F /dir2
Using find / -type l -printf '%ln'
doesn't work as you get relative links like ../tmp/xyz
which might be pointing to your target dir, but are not matched because they are not fully expanded.
You can find all the symbolic links using:
find / -type l
you might want to run this as root in order to get to every place on the disc.
You can expand these using readlink -f
to get the full path of the link and you should be able to grep
the output against the target directory that you are considering for deletion:
find / -type l -exec readlink -f + | grep -F /dir2
Using find / -type l -printf '%ln'
doesn't work as you get relative links like ../tmp/xyz
which might be pointing to your target dir, but are not matched because they are not fully expanded.
edited Aug 6 '16 at 10:00
answered Aug 6 '16 at 9:53
Anthon
59.8k17102163
59.8k17102163
1
In case of subtree it can be useful to follow symlinks:find -L /subtree -xtype l -exec readlink -f +
– ruvim
Nov 30 '17 at 11:52
add a comment |
1
In case of subtree it can be useful to follow symlinks:find -L /subtree -xtype l -exec readlink -f +
– ruvim
Nov 30 '17 at 11:52
1
1
In case of subtree it can be useful to follow symlinks:
find -L /subtree -xtype l -exec readlink -f +
– ruvim
Nov 30 '17 at 11:52
In case of subtree it can be useful to follow symlinks:
find -L /subtree -xtype l -exec readlink -f +
– ruvim
Nov 30 '17 at 11:52
add a comment |
up vote
0
down vote
In my case, the accepted answer wasn't useful (because it didn't output the link source). Here is what worked for me.
I worked around it using two -exec
clauses:
find /home/ -type l -exec readlink -nf ';' -exec echo " -> " ';' | grep "/dir2"
New contributor
add a comment |
up vote
0
down vote
In my case, the accepted answer wasn't useful (because it didn't output the link source). Here is what worked for me.
I worked around it using two -exec
clauses:
find /home/ -type l -exec readlink -nf ';' -exec echo " -> " ';' | grep "/dir2"
New contributor
add a comment |
up vote
0
down vote
up vote
0
down vote
In my case, the accepted answer wasn't useful (because it didn't output the link source). Here is what worked for me.
I worked around it using two -exec
clauses:
find /home/ -type l -exec readlink -nf ';' -exec echo " -> " ';' | grep "/dir2"
New contributor
In my case, the accepted answer wasn't useful (because it didn't output the link source). Here is what worked for me.
I worked around it using two -exec
clauses:
find /home/ -type l -exec readlink -nf ';' -exec echo " -> " ';' | grep "/dir2"
New contributor
edited Nov 20 at 15:30
ctrl-alt-delor
10.2k41955
10.2k41955
New contributor
answered Nov 20 at 14:41
Dorian Marchal
1
1
New contributor
New contributor
add a comment |
add a comment |
up vote
0
down vote
With zsh
:
printf '%sn' /dir2/**/*(D@e'([[ $REPLY:P = /dir1(/*|) ]])')
add a comment |
up vote
0
down vote
With zsh
:
printf '%sn' /dir2/**/*(D@e'([[ $REPLY:P = /dir1(/*|) ]])')
add a comment |
up vote
0
down vote
up vote
0
down vote
With zsh
:
printf '%sn' /dir2/**/*(D@e'([[ $REPLY:P = /dir1(/*|) ]])')
With zsh
:
printf '%sn' /dir2/**/*(D@e'([[ $REPLY:P = /dir1(/*|) ]])')
answered Nov 20 at 15:32
Stéphane Chazelas
294k54555898
294k54555898
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f301717%2fhow-to-find-all-symbolic-links-pointing-to-any-file-directory-inside-a-given-dir%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown