Armed Polite Society

Main Forums => The Roundtable => Topic started by: AZRedhawk44 on April 02, 2009, 09:22:18 PM

Title: Linux: How to read a symlink?
Post by: AZRedhawk44 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.
Title: Re: Linux: How to read a symlink?
Post by: Vodka7 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?
Title: Re: Linux: How to read a symlink?
Post by: GigaBuist on April 02, 2009, 10:26:26 PM
ls -l will do it with the GNU filetools package and on my OS X laptop.
Title: Re: Linux: How to read a symlink?
Post by: AZRedhawk44 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.
Title: Re: Linux: How to read a symlink?
Post by: AZRedhawk44 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
Title: Re: Linux: How to read a symlink?
Post by: Vodka7 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'
Title: Re: Linux: How to read a symlink?
Post by: Vodka7 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.
Title: Re: Linux: How to read a symlink?
Post by: Nitrogen 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.
Title: Re: Linux: How to read a symlink?
Post by: AZRedhawk44 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?
Title: Re: Linux: How to read a symlink?
Post by: AZRedhawk44 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.
Title: Re: Linux: How to read a symlink?
Post by: CNYCacher 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.
Title: Re: Linux: How to read a symlink?
Post by: AZRedhawk44 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.
Title: Re: Linux: How to read a symlink?
Post by: CNYCacher on April 03, 2009, 10:57:59 AM
OIC.  You needed the location of the other file, not the file (contents) itself.
Title: Re: Linux: How to read a symlink?
Post by: Nick1911 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?"
Title: Re: Linux: How to read a symlink?
Post by: Nitrogen 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.
Title: Re: Linux: How to read a symlink?
Post by: tyme 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 :)