17

So let's say I have a symbolic link of a file in my home directory to another file on a different partition. How would I find the target location of the linked file? By this, I mean, let's say I have file2 in /home/user/; but it's a symbolic link to another file1. How would I find file1 without manually having to go through each partition/directory to find the file?

  • 1
    Please expand on "original file". What if file1 is a symlink to file0 ? Do you want to find the last target of your symlink (file0) or just the next one (file1) ? – don_crissti Nov 12 '14 at 20:30
21

Try this :

readlink -f /path/file

( last target of your symlink if there's more than one level )

If you just want the next level of symbolic link, use :

readlink /path/file
  • im getting no output – k-Rocker Nov 12 '14 at 20:09
  • You should have one ! Error or a path – Gilles Quenot Nov 12 '14 at 20:11
  • no nothing: user@shell a5$ readlink -f ./student1.java and i get user@shell a5$ – k-Rocker Nov 12 '14 at 20:13
  • nvm, my symbolic link wasnt setup right, not sure why. but i made a new one and it works, thanks. – k-Rocker Nov 12 '14 at 20:22
  • 2
    FWIW readlink /path/file will return the next target. – don_crissti Nov 12 '14 at 20:43
2

1.

ls -l bin

produce

lrwxrwxrwx 1 az az 14 Ноя 12 22:13 bin -> ../Gdrive/bin/

2.

file bin

produce

bin: symbolic link to `../Gdrive/bin/' 

3.

stat bin

produce

File: «bin» -> «../Gdrive/bin/»
  • ok well im trying to make a script, so how would i use these? im new to linux. – k-Rocker Nov 12 '14 at 20:21
  • 1
    @k-Rocker If you explain what you intend to do it would be better. – Costas Nov 12 '14 at 20:29
0

Expanding on Costas

Suppose you have

ln -s test.txt sym_link_1.txt
ls -l sym_link_1.txt
lrwxrwxrwx. 1 a1 g1 8 Jan  7 16:59 sym_link_1.txt -> test.txt

In a script you can do (use backticks)

ln -s `readlink sym_link_1.txt` sym_link_2.txt

Then you have

ls -l sym_link_*.txt
lrwxrwxrwx. 1 a1 g1 8 Jan  7 16:59 sym_link_1.txt -> test.txt
lrwxrwxrwx. 1 a1 g1 8 Jan  7 17:01 sym_link_2.txt -> test.txt
0

Simplest way: cd to where the symbolic link is located and do ls -l to list the details of the files.

The part to the right of -> after the symbolic link is the destination to which it is pointing.

Ex:

lrwxrwxrwx 1 userName groupName 22 Jan 17 13:29 Link to temp.txt -> /home/user/temp.txt

Here we have "Link to temp.txt" that points to (->) "/home/user/temp.txt".

But, like @Gilles Quenot points out, you can also just do readlink -f /path/to/symbolic_link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.