Author Topic: Linux: How to read a symlink?  (Read 5164 times)

AZRedhawk44

  • friends
  • Senior Member
  • ***
  • Posts: 13,982
Linux: How to read a symlink?
« on: April 02, 2009, 09:22:18 PM »
Got me a problem:

I've got a link file that I need to figure out where the real target is located.

How do I do that?  I've tried readlink, but can't seem to get that to work.

Imagine you have:
/home/azredhawk44/somefile.txt
and
/home/azredhawk44/somefile.lnk

Whenever you vi, cat, more, etc against somefile.lnk... you end up in somefile.txt.  Problem is, my situation is more difficult, because the link is running off somewhere else.

I've got a legacy informix app I'm rewriting to be straight MS SQL, but gotta support the existing crap until then.  Totally stumped on how to trace down the original source code and make environment for one client's custom module that was written before my arrival with no documentation/source.  Got a symlink taking me to a script that runs make, but it's erroring.  Good fun.  This particular environment is HP-UX.
"But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist."
--Lysander Spooner

I reject your authoritah!

Vodka7

  • friend
  • Senior Member
  • ***
  • Posts: 1,067
Re: Linux: How to read a symlink?
« Reply #1 on: April 02, 2009, 09:41:49 PM »
Well the output isn't as clean as readlink, but how about ls -l?  Or file -b?

GigaBuist

  • friends
  • Senior Member
  • ***
  • Posts: 4,345
    • http://www.justinbuist.org/blog/
Re: Linux: How to read a symlink?
« Reply #2 on: April 02, 2009, 10:26:26 PM »
ls -l will do it with the GNU filetools package and on my OS X laptop.

AZRedhawk44

  • friends
  • Senior Member
  • ***
  • Posts: 13,982
Re: Linux: How to read a symlink?
« Reply #3 on: April 02, 2009, 11:04:16 PM »
ls -l somefile.lnk

output only gives me:

-rw-r--r-- 2 azredhawk44 azredhawk44 18 2009-04-02 17:52 somefile.lnk

This doesn't tell me that it is targeted to somefile.txt in the same directory.

Moar plz.
"But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist."
--Lysander Spooner

I reject your authoritah!

AZRedhawk44

  • friends
  • Senior Member
  • ***
  • Posts: 13,982
Re: Linux: How to read a symlink?
« Reply #4 on: April 02, 2009, 11:07:39 PM »
file -b somefile.lnk

returns

ASCII text

which is the type of data in my test symlink (to somefile.txt).

I need something that reports /home/azredhawk44/somefile.lnk points to ./somefile.txt
"But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist."
--Lysander Spooner

I reject your authoritah!

Vodka7

  • friend
  • Senior Member
  • ***
  • Posts: 1,067
Re: Linux: How to read a symlink?
« Reply #5 on: April 03, 2009, 12:10:41 AM »
Hard to test for me, the only symlinks I have point to dirs, but so you can see what I get:

patrick@patrick-ubuntu:~/ushare$ readlink Anime
/media/Tertiary/Anime

patrick@patrick-ubuntu:~/ushare$ ls -l Anime
lrwxrwxrwx 1 patrick patrick 21 2009-02-04 20:25 Anime -> /media/Tertiary/Anime

patrick@patrick-ubuntu:~/ushare$ file -b Anime
symbolic link to `/media/Tertiary/Anime'

Vodka7

  • friend
  • Senior Member
  • ***
  • Posts: 1,067
Re: Linux: How to read a symlink?
« Reply #6 on: April 03, 2009, 12:16:29 AM »
Hang on a second, I was assuming you were talking about symlinks created in a Unix environment.  .lnk is a Windows shorcut.

http://www.linuxforums.org/forum/misc/126912-converting-windows-shortcuts-into-linux-links.html

The above link looks promising, but is there any way you can just get the lnks on a Windows box?  Would probably be easiest to get them all on a VM and right click in a windows guest to see the properties.

Nitrogen

  • friends
  • Senior Member
  • ***
  • Posts: 1,755
  • Who could it be?
    • @c0t0d0s2 / Twitter.
Re: Linux: How to read a symlink?
« Reply #7 on: April 03, 2009, 12:19:17 AM »
are you sure it's a symlink and not a hardlink?

ls -i firstfile
ls -i secondfile

If its a hardlink, you'll see something like this:

asthma:3:~$ touch foo
asthma:3:~$ ln foo bar
asthma:3:~$ ls -i foo bar
1994986 bar  1994986 foo
asthma:3:~$
Where the inode numbers reported are teh same.

Hardlinks work only on the same filesystem.
יזכר לא עד פעם
Remember. Never Again.
What does it mean to be an American?  Have you forgotten? | http://youtu.be/0w03tJ3IkrM

AZRedhawk44

  • friends
  • Senior Member
  • ***
  • Posts: 13,982
Re: Linux: How to read a symlink?
« Reply #8 on: April 03, 2009, 01:06:51 AM »
Sorry... I'm many moons removed from unix practice.

Yeah, could be a hardlink rather than symlink.

Looks that way:

ls -i somefile*

8896670 somefile.lnk  8896670 somefile.txt

And it's not linking a directory... it's linking to a script that runs a make command.  I need to browse the actual script's folder for missing dependencies but I can't find it... All I've got is a .link file.  And it is a 4-letter extension, "link."

I don't have access to it right now and won't until tomorrow again.  I've been toying with a txt file and linked file to it.

So... how do I read a hardlink's properties?

Can I search for a specific inode and get a list of all hardlinks to it?
"But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist."
--Lysander Spooner

I reject your authoritah!

AZRedhawk44

  • friends
  • Senior Member
  • ***
  • Posts: 13,982
Re: Linux: How to read a symlink?
« Reply #9 on: April 03, 2009, 01:23:19 AM »
find -inum 8896670

results:

./test.txt
./test.lnk

Or:

find -samefile test.lnk

results:

./test.txt
./test.lnk

Hooray.  Thanks, guys.

Hopefully "find" exists on HP-UX and isn't GNU-only.

ETA:  Yep, it exists.  -inum switch is common between the two, but -samefile doesn't seem to be.
« Last Edit: April 03, 2009, 01:33:30 AM by AZRedhawk44 »
"But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist."
--Lysander Spooner

I reject your authoritah!

CNYCacher

  • friend
  • Senior Member
  • ***
  • Posts: 4,438
Re: Linux: How to read a symlink?
« Reply #10 on: April 03, 2009, 08:40:13 AM »
I guess I need to ask:

Why not just use the "link" for whatever you are doing?


* "link" in quotes because if it really is a hardlink, then there's really no "real" and "linked" file, they are the same.
On two occasions, I have been asked [by members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question.
Charles Babbage

AZRedhawk44

  • friends
  • Senior Member
  • ***
  • Posts: 13,982
Re: Linux: How to read a symlink?
« Reply #11 on: April 03, 2009, 10:23:31 AM »
Because it isn't as simple as a linked text file in the same parent directory.

It's a compile script, linked from one directory like this:

/product/source/product_name.link

to another, somewhere else unknown, like this:

/product/someotherdir/somefile.sh

I've got missing dependency error messages and I need to know the location of "someotherdir."

Basically, I've inherited 15 year old code that I have to reverse engineer.  I've got the actual source on a piece of paper in front of me... but no lists of dependencies or compile instructions.

And I've made a change to the source since it wasn't working as the firm wanted, and now have to recompile.

I'd love to re-write it as a straight SQL to SQL query, but their target DB is still Informix.  So I can't.
"But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist."
--Lysander Spooner

I reject your authoritah!

CNYCacher

  • friend
  • Senior Member
  • ***
  • Posts: 4,438
Re: Linux: How to read a symlink?
« Reply #12 on: April 03, 2009, 10:57:59 AM »
OIC.  You needed the location of the other file, not the file (contents) itself.
On two occasions, I have been asked [by members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question.
Charles Babbage

Nick1911

  • Administrator
  • Senior Member
  • *****
  • Posts: 8,492
Re: Linux: How to read a symlink?
« Reply #13 on: April 03, 2009, 11:03:41 AM »

Basically, I've inherited 15 year old code that I have to reverse engineer.  I've got the actual source on a piece of paper in front of me... but no lists of dependencies or compile instructions.

And I've made a change to the source since it wasn't working as the firm wanted, and now have to recompile.

I'd love to re-write it as a straight SQL to SQL query, but their target DB is still Informix.  So I can't.

Eww, that really sucks.  :|

I have a similar situation.  My workplace has a totally undocumented .exe that does some data manipulation.  No source, no requirements, no help file...  Just an x86 exe.  I get: "Can you change this to make it do xxx?"

Nitrogen

  • friends
  • Senior Member
  • ***
  • Posts: 1,755
  • Who could it be?
    • @c0t0d0s2 / Twitter.
Re: Linux: How to read a symlink?
« Reply #14 on: April 03, 2009, 09:44:18 PM »
This is the kind of Male Bovine Excrement I deal with daily.  You wouldn't believe some of the things I've been asked.

My favorite so far:
"Can we put our swap on a ramdisk to increase performance??"

We really need a "headdesk" emoticon.
יזכר לא עד פעם
Remember. Never Again.
What does it mean to be an American?  Have you forgotten? | http://youtu.be/0w03tJ3IkrM

tyme

  • expat
  • friend
  • Senior Member
  • ***
  • Posts: 1,056
  • Did you know that dolphins are just gay sharks?
    • TFL Library
Re: Linux: How to read a symlink?
« Reply #15 on: April 04, 2009, 01:40:40 AM »
head desk: http://www.deviantart.com/#catpath=customization/icons/emoticons&order=9&q=head+desk
There are some facepalm emoticons over there, but they don't turn out well in normal-sized emoticons.

Quote from: Nick1911
My workplace has a totally undocumented .exe that does some data manipulation.  No source, no requirements, no help file...  Just an x86 exe.  I get: "Can you change this to make it do xxx?"
Only one thing to do... rewrite it in intercal so that once you're gone they're stuck maintaining it in that language :)
Support Range Voting.
End Software Patents

"Four people are dead.  There isn't time to talk to the police."  --Sherlock (BBC)