Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm trying to better understand symbolic links... and not having very much luck. This is my actual shell output with username/host changed:

username@host:~$ mkdir actual
username@host:~$ mkdir proper
username@host:~$ touch actual/file-1.txt
username@host:~$ echo "file 1" > actual/file-1.txt
username@host:~$ touch actual/file-2.txt
username@host:~$ echo "file 2" > actual/file-2.txt
username@host:~$ ln -s actual/file-1.txt actual/file-2.txt proper
username@host:~$ # Now, try to use the files through their links
username@host:~$ cat proper/file-1.txt
cat: proper/file-1.txt: No such file or directory
username@host:~$ cat proper/file-2.txt
cat: proper/file-2.txt: No such file or directory
username@host:~$ # Check that actual files do in fact exist
username@host:~$ cat actual/file-1.txt
file 1
username@host:~$ cat actual/file-2.txt
file 2
username@host:~$ # Remove the links and go home :(
username@host:~$ rm proper/file-1.txt
username@host:~$ rm proper/file-2.txt

I thought that a symbolic link was supposed to operate transparently, in the sense that you could operate on the file that it points to as if you were accessing the file directly (except of course in the case of rm where of course the link is simply removed).

share|improve this question
    
How is your disk formatted? What filesystem are you using? (FAT doesn't support symlinks but if you try to make on a FAT filesystem, it should give an error.) –  Nicole Hamilton Nov 28 '12 at 16:10
    
@NicoleHamilton - It's ext4 (according to df -T) - is the above result strange to you too then? –  orokusaki Nov 28 '12 at 16:18
    
@orokusaki Not strange. See my answer below. Just a heads up, no need to touch files to cat to them. They don't need to exist even. Just to save you some typing! –  nerdwaller Nov 28 '12 at 16:31
add comment

3 Answers

up vote 6 down vote accepted

Symlinks tend to like full paths or relative to the link, otherwise they can often be looking for file-1.txt locally (oddly enough).

Navigate to proper and execute ls -l and you can see that the symlink is looking for actual/file-1.txt, when it should be ../actual/file-1.txt.

So you have two options:

  1. Give the full path

    ln -s ~/actual/file-1.txt ~/actual/file-2.txt ~/proper

  2. Navigate to the folder you want the link to be in and link from there

    cd proper
    ln -s ../actual/file-1.txt ../actual/file-2.txt ./

Edit: A hint to save typing.

You could just do ln -s ~/actual/file-{1,2}.txt ~/proper

The items in the curley braces are executed once per run, left to right. So it would do the first, then the second. Saves some major typing as you get further on in the shell.

share|improve this answer
    
Thanks - I was just about to pull my hair out before I read your answer. –  orokusaki Nov 28 '12 at 17:01
    
It's confusing sometimes for sure! Linux tends to be very literal, so if you say do something - it will. Be careful with symlinking folders that way, I have overwritten one or two before (Just specify the name without a trailing slash). Also, don't rm -rf * to a symlinked folder - you could wipe the folder it links to. Just do rm symlinkname . Just a few things I have made mistakes on :D –  nerdwaller Nov 28 '12 at 17:04
add comment

The problem is the usage of relative paths. If you specify your link creation with the full explicit path, it works.

$ ln -s ~/actual/file1.txt ~/actual/file2.txt ~/proper/

$ cat proper/file1.txt

file 1

$

Your example creates links in proper that look for a subdirectory named actual under the current directory, rather than your intended parent-of-both.

share|improve this answer
add comment

Symbolic links can be tricky.  In essence, a symbolic link is a file that contains a filename/pathname for another file (and that is flagged for special treatment).  If the pathname in the link file begins with ‘/’, then it is treated as an absolute pathname, and things are fairly straightforward.  If it doesn’t begin with a slash, it is treated as a relative pathname — relative to the directory where the link is located.  (This is true whether or not the name contains slashes.)  So, you created proper/file–1.txt as a link to “actual/file–1.txt”, and when you tried to access it, the system tried to access proper/actual/file–1.txt.  You should have said

ln –s  ../actual/file–1.txt  ../actual/file–2.txt  proper

By the way, you didn’t need the touch commands.  echo "file 1" > actual/file–1.txt is sufficient to create actual/file–1.txt.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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