LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-03-2019, 11:46 AM   #1
lucmove
Senior Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Debian
Posts: 1,433

Rep: Reputation: 110Reputation: 110
How do I find symlink targets?


I have a large directory with many files and directories. A few of those MAY be targets to symlinks. I don't really know if they exist much less which ones they are. How do I find those?
 
Old 02-03-2019, 11:48 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,864

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
ls -l will color its output and you will see. You can use find to identify links. And there are a lot of other ways using test, perl, python, shell, whatever.
 
Old 02-03-2019, 12:06 PM   #3
lucmove
Senior Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Debian
Posts: 1,433

Original Poster
Rep: Reputation: 110Reputation: 110
Quote:
Originally Posted by pan64 View Post
ls -l will color its output and you will see. You can use find to identify links. And there are a lot of other ways using test, perl, python, shell, whatever.
You certainly don't understand the question.
 
Old 02-03-2019, 12:16 PM   #4
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,728

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
There was a recent thread on this here...suggest the OP search for it. It's been in the last couple of weeks and contained considerable discussion.

If I understood that thread, it's not possible to know that a given file is a target of a softlink. It may be possible to detect targets of hard links.
 
Old 02-03-2019, 01:12 PM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,312
Blog Entries: 3

Rep: Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722
Do you want only the targets?

Code:
find ~ -type l -exec readlink --canonicalize {} \;
Or also the symlink?
 
1 members found this post helpful.
Old 02-03-2019, 01:44 PM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,864

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
Quote:
Originally Posted by lucmove View Post
You certainly don't understand the question.
you certainly can't explain what do you really need. https://www.linuxquestions.org/quest...re-4175646996/

did I miss something?
 
Old 02-03-2019, 08:04 PM   #7
lucmove
Senior Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Debian
Posts: 1,433

Original Poster
Rep: Reputation: 110Reputation: 110
Quote:
Originally Posted by Turbocapitalist View Post
Do you want only the targets?
Code:
find ~ -type l -exec readlink --canonicalize {} \;
Or also the symlink?
Now that is a useful answer! I knew that there had to be some unix tool that would make that task easier. I was just hoping that someone would point it out. Thank you!

I also found a program called symlinks on the Debian repository that will do exactly that. It takes a while to scan all potential directories, but once I have that report, grep will tell me exactly what I need.

Solved. Thank you again, Turbocapitalist. Nice nickname, btw.
 
Old 02-04-2019, 09:49 AM   #8
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
If you are interested in knowing which symlinks are pointing to your directory target files as well you can use the following:
Code:
find "directory/to/search/for/symlinks" -type l -exec ls -l "{}" \; | grep "directory/containing/targets"
This is probably what pan64 meant in post #2

Last edited by l0f4r0; 02-04-2019 at 09:51 AM.
 
Old 02-04-2019, 09:56 AM   #9
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,312
Blog Entries: 3

Rep: Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722
Quote:
Originally Posted by l0f4r0 View Post
If you are interested in knowing which symlinks are pointing to your directory target files as well...
Or

Code:
find ~ -type l -exec sh -c "echo -n \"{}\t\"; readlink --canonicalize {};"  \;
 
Old 02-04-2019, 10:21 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,864

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
from grammatical point of view the question "how do I find those" may mean both "how do I find those symlinks" or "how do I find those files".
from the other hand we need to scan the whole directory tree and collect all the symlinks and finally print links, targets or both. That is completely irrelevant. I mean technically. What Turbocapitalist posted is most probably the slowest solution, implementing the same algorithm in perl/python - or bash will do the job too. Including the "postprocessing" mentioned in post #7.

And the problem is still not completely solved, because there can be links pointing to nowhere and there can be link chains pointing finally to a file (or dir or ?), there can be link loops ...
 
1 members found this post helpful.
Old 02-04-2019, 10:24 AM   #11
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,312
Blog Entries: 3

Rep: Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722
As for the links pointing to nowhere, they can be culled or identified. See --canonicalize-existing or --canonicalize-missing for readlink. In the case of a loop, it gets treated as a link with a missing target.

Edit: agreed about perl being faster to run.

Last edited by Turbocapitalist; 02-04-2019 at 10:26 AM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find symlink target name in script germanyzulu Linux - Software 4 08-14-2015 10:23 AM
[SOLVED] symlink to symlink confusion, Reprovo Linux - Newbie 2 11-18-2014 02:17 PM
How to find symlink? kaz2100 Linux - Software 10 09-25-2014 12:41 PM
Question How to get stat() of symlink and not of target of symlink ? ronbarak Programming 3 11-08-2010 12:14 PM
can not find root file. create symlink anon222 Fedora - Installation 2 11-04-2009 07:39 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration