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 09-19-2005, 02:39 PM   #1
germanyzulu
LQ Newbie
 
Registered: Jul 2005
Distribution: Ubuntu "Dapper" 6.06 LTS
Posts: 18

Rep: Reputation: 1
How to find symlink target name in script


Hey,

I have a symlink
~/bin/foo/image.link
that points to
~/photos/image.jpg

I am looking for a command kind of like this:

Quote:
(From the 'ls' manpage)
-L, --dereference
when showing file information for a symbolic link, show informa-
tion for the file the link references rather than for the link
itself
Code:
$ ls -l ~/bin/foo/image.link
lrwx------  1 me me 39 Sep 19 11:47 ~/bin/foo/image.link -> ~/photos/image.jpg
$ ls -l --dereference ~/bin/foo/image.link
-rwx------  1 me me 150108 May 29  2003 ~/bin/foo/image.link
But I want the name of the target file not just info about it. The 'file' command would work fine except that I don't know how to pull out just the filename for my script, grep with a regex just lists the whole line.

Is there a command you know of/option to 'ls' ?? Or is 'sed'/'awk' something I should look into??

Thanks,
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 09-19-2005, 03:15 PM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
file somefile | awk '{print $5}'

or

ls -l somefile | awk '{print $11}'
 
Old 09-19-2005, 08:41 PM   #3
germanyzulu
LQ Newbie
 
Registered: Jul 2005
Distribution: Ubuntu "Dapper" 6.06 LTS
Posts: 18

Original Poster
Rep: Reputation: 1
Perfect!

That's exactly what I needed.

I suppose I could have read the 'awk' man page but at the time it seemed so daunting, at least now I understand the basics of it.

Thank you!

P.S. There you go Jeremy (and macemoneta) yet another satified Linux user, at least untill I come up with another Question!
 
Old 08-09-2006, 03:14 PM   #4
germanyzulu
LQ Newbie
 
Registered: Jul 2005
Distribution: Ubuntu "Dapper" 6.06 LTS
Posts: 18

Original Poster
Rep: Reputation: 1
Well after a long time of not thinking about this I was poking around and found the readlink command:

Code:
$readlink --help
Usage: readlink [OPTION]... FILE
Display value of a symbolic link on standard output.
[...]
The awk method does work and for more that just the question I had. But for finding the target of a symlink I think this works better. (Much simpler, easyer to read, remember what it does in a script, etc.)

Also it is contained in the coreutils package (Ubuntu) so most if not all distros will have it.
 
2 members found this post helpful.
Old 08-14-2015, 10:23 AM   #5
pcabrera
LQ Newbie
 
Registered: Aug 2015
Posts: 2

Rep: Reputation: Disabled
One more reason to use readlink is that it can follow recursively

I know is an extremely old thread, but hey, some of these tips are still valid after years!!!

I just wanted to point out one advantage of using readlink instead of the awk solution is that readlink can follow links recursively as opposed to the awk solution.
for example, I found this thread trying to find a way to get the real java command in my system (in a script), so I ended up doing this:

Code:
readlink -f $(which java)
Which in fact has two symlinks:

Code:
which java -> /usr/bin/java
/usr/bin/java --symlink--> /etc/alternatives/java
/etc/alternatives/java --symlink--> /usr/lib/jvm/jdk1.8.0_11/bin/java

Last edited by pcabrera; 08-14-2015 at 10:24 AM.
 
  


Reply

Tags
awk, symlink, symlinks



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
Shell script to find a particular string Prasun1 Linux - General 5 08-30-2005 09:23 AM
find shell script help liren Linux - Newbie 3 05-02-2005 03:05 PM
why isn't this php symlink script working verbatim Programming 8 04-27-2005 09:13 AM
Specifying target directory for command in bash shell script? spectrescape Programming 1 07-22-2004 05:37 PM
how to find the pid of a perl script from shell script toovato Linux - General 1 12-19-2003 06:25 PM

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

All times are GMT -5. The time now is 04:18 PM.

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