Why doesn't my symbolic link work?
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).
linux ubuntu bash symbolic-link
add a comment |
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).
linux ubuntu bash symbolic-link
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 todf -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 a comment |
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).
linux ubuntu bash symbolic-link
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).
linux ubuntu bash symbolic-link
linux ubuntu bash symbolic-link
asked Nov 28 '12 at 15:47
orokusakiorokusaki
59951025
59951025
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 todf -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 a comment |
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 todf -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
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
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
@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
@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 a comment |
5 Answers
5
active
oldest
votes
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:
Give the full path
ln -s ~/actual/file-1.txt ~/actual/file-2.txt ~/proper
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 curly braces are substituted and placed after each other, creating the command
ln -s ~/actual/file-1.txt ~/actual/file-2.txt ~/proper
which links both files to the target directory. Saves some major typing as you get further on in the shell.
2
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'trm -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 a 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.
add a 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
.
add a comment |
A related issue, and it may be obvious to many, but stumped me for a few minutes: if you're creating a symlink within a directory that is itself symlinked, or there's a symlink within the path of the current working directory, you might run in to problems with symlinks not working.
Use cd ..
and ls -l
repeatedly, to see if your parent directories are themselves symlinked.
If you need to create a symlink, cd
to the original Target directory, and create new symlinks there, so the relative paths are accurate.
Or to put it another way: the relative path is from the Origin to the Target. If the Origin is subsequently symlinked, that's OK. But you might run in to problems setting up a new Origin link_name within a directory that is itself somehow symlinked.
1
Instead of "cd ..
andls -l
repeatedly", you could usenamei -mox $PWD
. Another useful way to find out if there are symlinks along the path is to usepwd -P
, which gives you a path to the current directory which does not contain any symlinks (so ifpwd
andpwd -P
give different output, you know there is a symlink somewhere along the way).
– Ansa211
Nov 23 '17 at 11:25
add a comment |
When you try to create a link to a file that doesn't exist (or you've given the path incorrectly) ln -s doesn't throw an error. It creates a link but when you try 'cat' on that link it says no file found. Even when you can see it using ls. In such cases always double-check your file path.
ubuntu@ip-172-31-80-155:~/lab5$ ln -s etc/ufw/ufw.conf link1
ubuntu@ip-172-31-80-155:~/lab5$ ls -al
total 5360
drwxrwxr-x 2 ubuntu ubuntu 4096 Mar 9 18:56 .
drwxr-xr-x 7 ubuntu ubuntu 4096 Mar 9 18:42 ..
-rwxr--r-- 1 ubuntu ubuntu 5478400 Mar 9 18:32 backup.tar
lrwxrwxrwx 1 ubuntu ubuntu 16 Mar 9 18:56 link1 -> etc/ufw/ufw.conf
ubuntu@ip-172-31-80-155:~/lab5$ cat link1
cat: link1: No such file or directory
Because the file etc/ufw/ufw.conf doesn't exist. The correct path is /etc/ufw/ufw.conf
ubuntu@ip-172-31-80-155:~/lab5$ ln -s /etc/ufw/ufw.conf link2
ubuntu@ip-172-31-80-155:~/lab5$ ls -l
total 5352
-rwxr--r-- 1 ubuntu ubuntu 5478400 Mar 9 18:32 backup.tar
lrwxrwxrwx 1 ubuntu ubuntu 16 Mar 9 18:56 link1 -> etc/ufw/ufw.conf
lrwxrwxrwx 1 ubuntu ubuntu 17 Mar 9 19:00 link2 -> /etc/ufw/ufw.conf
ubuntu@ip-172-31-80-155:~/lab5$ cat link2
# /etc/ufw/ufw.conf
#
# Set to yes to start on boot. If setting this remotely, be sure to add a rule
# to allow your remote connection before starting ufw. Eg: 'ufw allow 22/tcp'
ENABLED=no
# Please use the 'ufw' command to set the loglevel. Eg: 'ufw logging medium'.
# See 'man ufw' for details.
LOGLEVEL=low
There is already a better answer, and the first part, thatln -s
"sometimes" doesn't check for the existence of the target is just wrong.
– RalfFriedl
Mar 9 at 19:24
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f511900%2fwhy-doesnt-my-symbolic-link-work%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
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:
Give the full path
ln -s ~/actual/file-1.txt ~/actual/file-2.txt ~/proper
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 curly braces are substituted and placed after each other, creating the command
ln -s ~/actual/file-1.txt ~/actual/file-2.txt ~/proper
which links both files to the target directory. Saves some major typing as you get further on in the shell.
2
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'trm -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 a comment |
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:
Give the full path
ln -s ~/actual/file-1.txt ~/actual/file-2.txt ~/proper
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 curly braces are substituted and placed after each other, creating the command
ln -s ~/actual/file-1.txt ~/actual/file-2.txt ~/proper
which links both files to the target directory. Saves some major typing as you get further on in the shell.
2
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'trm -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 a comment |
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:
Give the full path
ln -s ~/actual/file-1.txt ~/actual/file-2.txt ~/proper
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 curly braces are substituted and placed after each other, creating the command
ln -s ~/actual/file-1.txt ~/actual/file-2.txt ~/proper
which links both files to the target directory. Saves some major typing as you get further on in the shell.
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:
Give the full path
ln -s ~/actual/file-1.txt ~/actual/file-2.txt ~/proper
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 curly braces are substituted and placed after each other, creating the command
ln -s ~/actual/file-1.txt ~/actual/file-2.txt ~/proper
which links both files to the target directory. Saves some major typing as you get further on in the shell.
edited Aug 31 '17 at 18:39
Scott
16.1k113990
16.1k113990
answered Nov 28 '12 at 16:18
nerdwallernerdwaller
12.3k12939
12.3k12939
2
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'trm -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 a comment |
2
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'trm -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
2
2
Thanks - I was just about to pull my hair out before I read your answer.
– orokusaki
Nov 28 '12 at 17:01
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
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 a 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.
add a 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.
add a 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.
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.
edited Jun 11 '17 at 10:44
Peter Mortensen
8,376166185
8,376166185
answered Nov 28 '12 at 16:19
Don SimonDon Simon
43427
43427
add a comment |
add a 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
.
add a 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
.
add a 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
.
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
.
answered Nov 28 '12 at 16:29
ScottScott
16.1k113990
16.1k113990
add a comment |
add a comment |
A related issue, and it may be obvious to many, but stumped me for a few minutes: if you're creating a symlink within a directory that is itself symlinked, or there's a symlink within the path of the current working directory, you might run in to problems with symlinks not working.
Use cd ..
and ls -l
repeatedly, to see if your parent directories are themselves symlinked.
If you need to create a symlink, cd
to the original Target directory, and create new symlinks there, so the relative paths are accurate.
Or to put it another way: the relative path is from the Origin to the Target. If the Origin is subsequently symlinked, that's OK. But you might run in to problems setting up a new Origin link_name within a directory that is itself somehow symlinked.
1
Instead of "cd ..
andls -l
repeatedly", you could usenamei -mox $PWD
. Another useful way to find out if there are symlinks along the path is to usepwd -P
, which gives you a path to the current directory which does not contain any symlinks (so ifpwd
andpwd -P
give different output, you know there is a symlink somewhere along the way).
– Ansa211
Nov 23 '17 at 11:25
add a comment |
A related issue, and it may be obvious to many, but stumped me for a few minutes: if you're creating a symlink within a directory that is itself symlinked, or there's a symlink within the path of the current working directory, you might run in to problems with symlinks not working.
Use cd ..
and ls -l
repeatedly, to see if your parent directories are themselves symlinked.
If you need to create a symlink, cd
to the original Target directory, and create new symlinks there, so the relative paths are accurate.
Or to put it another way: the relative path is from the Origin to the Target. If the Origin is subsequently symlinked, that's OK. But you might run in to problems setting up a new Origin link_name within a directory that is itself somehow symlinked.
1
Instead of "cd ..
andls -l
repeatedly", you could usenamei -mox $PWD
. Another useful way to find out if there are symlinks along the path is to usepwd -P
, which gives you a path to the current directory which does not contain any symlinks (so ifpwd
andpwd -P
give different output, you know there is a symlink somewhere along the way).
– Ansa211
Nov 23 '17 at 11:25
add a comment |
A related issue, and it may be obvious to many, but stumped me for a few minutes: if you're creating a symlink within a directory that is itself symlinked, or there's a symlink within the path of the current working directory, you might run in to problems with symlinks not working.
Use cd ..
and ls -l
repeatedly, to see if your parent directories are themselves symlinked.
If you need to create a symlink, cd
to the original Target directory, and create new symlinks there, so the relative paths are accurate.
Or to put it another way: the relative path is from the Origin to the Target. If the Origin is subsequently symlinked, that's OK. But you might run in to problems setting up a new Origin link_name within a directory that is itself somehow symlinked.
A related issue, and it may be obvious to many, but stumped me for a few minutes: if you're creating a symlink within a directory that is itself symlinked, or there's a symlink within the path of the current working directory, you might run in to problems with symlinks not working.
Use cd ..
and ls -l
repeatedly, to see if your parent directories are themselves symlinked.
If you need to create a symlink, cd
to the original Target directory, and create new symlinks there, so the relative paths are accurate.
Or to put it another way: the relative path is from the Origin to the Target. If the Origin is subsequently symlinked, that's OK. But you might run in to problems setting up a new Origin link_name within a directory that is itself somehow symlinked.
answered Aug 20 '14 at 10:05
ChriskyChrisky
101
101
1
Instead of "cd ..
andls -l
repeatedly", you could usenamei -mox $PWD
. Another useful way to find out if there are symlinks along the path is to usepwd -P
, which gives you a path to the current directory which does not contain any symlinks (so ifpwd
andpwd -P
give different output, you know there is a symlink somewhere along the way).
– Ansa211
Nov 23 '17 at 11:25
add a comment |
1
Instead of "cd ..
andls -l
repeatedly", you could usenamei -mox $PWD
. Another useful way to find out if there are symlinks along the path is to usepwd -P
, which gives you a path to the current directory which does not contain any symlinks (so ifpwd
andpwd -P
give different output, you know there is a symlink somewhere along the way).
– Ansa211
Nov 23 '17 at 11:25
1
1
Instead of "
cd ..
and ls -l
repeatedly", you could use namei -mox $PWD
. Another useful way to find out if there are symlinks along the path is to use pwd -P
, which gives you a path to the current directory which does not contain any symlinks (so if pwd
and pwd -P
give different output, you know there is a symlink somewhere along the way).– Ansa211
Nov 23 '17 at 11:25
Instead of "
cd ..
and ls -l
repeatedly", you could use namei -mox $PWD
. Another useful way to find out if there are symlinks along the path is to use pwd -P
, which gives you a path to the current directory which does not contain any symlinks (so if pwd
and pwd -P
give different output, you know there is a symlink somewhere along the way).– Ansa211
Nov 23 '17 at 11:25
add a comment |
When you try to create a link to a file that doesn't exist (or you've given the path incorrectly) ln -s doesn't throw an error. It creates a link but when you try 'cat' on that link it says no file found. Even when you can see it using ls. In such cases always double-check your file path.
ubuntu@ip-172-31-80-155:~/lab5$ ln -s etc/ufw/ufw.conf link1
ubuntu@ip-172-31-80-155:~/lab5$ ls -al
total 5360
drwxrwxr-x 2 ubuntu ubuntu 4096 Mar 9 18:56 .
drwxr-xr-x 7 ubuntu ubuntu 4096 Mar 9 18:42 ..
-rwxr--r-- 1 ubuntu ubuntu 5478400 Mar 9 18:32 backup.tar
lrwxrwxrwx 1 ubuntu ubuntu 16 Mar 9 18:56 link1 -> etc/ufw/ufw.conf
ubuntu@ip-172-31-80-155:~/lab5$ cat link1
cat: link1: No such file or directory
Because the file etc/ufw/ufw.conf doesn't exist. The correct path is /etc/ufw/ufw.conf
ubuntu@ip-172-31-80-155:~/lab5$ ln -s /etc/ufw/ufw.conf link2
ubuntu@ip-172-31-80-155:~/lab5$ ls -l
total 5352
-rwxr--r-- 1 ubuntu ubuntu 5478400 Mar 9 18:32 backup.tar
lrwxrwxrwx 1 ubuntu ubuntu 16 Mar 9 18:56 link1 -> etc/ufw/ufw.conf
lrwxrwxrwx 1 ubuntu ubuntu 17 Mar 9 19:00 link2 -> /etc/ufw/ufw.conf
ubuntu@ip-172-31-80-155:~/lab5$ cat link2
# /etc/ufw/ufw.conf
#
# Set to yes to start on boot. If setting this remotely, be sure to add a rule
# to allow your remote connection before starting ufw. Eg: 'ufw allow 22/tcp'
ENABLED=no
# Please use the 'ufw' command to set the loglevel. Eg: 'ufw logging medium'.
# See 'man ufw' for details.
LOGLEVEL=low
There is already a better answer, and the first part, thatln -s
"sometimes" doesn't check for the existence of the target is just wrong.
– RalfFriedl
Mar 9 at 19:24
add a comment |
When you try to create a link to a file that doesn't exist (or you've given the path incorrectly) ln -s doesn't throw an error. It creates a link but when you try 'cat' on that link it says no file found. Even when you can see it using ls. In such cases always double-check your file path.
ubuntu@ip-172-31-80-155:~/lab5$ ln -s etc/ufw/ufw.conf link1
ubuntu@ip-172-31-80-155:~/lab5$ ls -al
total 5360
drwxrwxr-x 2 ubuntu ubuntu 4096 Mar 9 18:56 .
drwxr-xr-x 7 ubuntu ubuntu 4096 Mar 9 18:42 ..
-rwxr--r-- 1 ubuntu ubuntu 5478400 Mar 9 18:32 backup.tar
lrwxrwxrwx 1 ubuntu ubuntu 16 Mar 9 18:56 link1 -> etc/ufw/ufw.conf
ubuntu@ip-172-31-80-155:~/lab5$ cat link1
cat: link1: No such file or directory
Because the file etc/ufw/ufw.conf doesn't exist. The correct path is /etc/ufw/ufw.conf
ubuntu@ip-172-31-80-155:~/lab5$ ln -s /etc/ufw/ufw.conf link2
ubuntu@ip-172-31-80-155:~/lab5$ ls -l
total 5352
-rwxr--r-- 1 ubuntu ubuntu 5478400 Mar 9 18:32 backup.tar
lrwxrwxrwx 1 ubuntu ubuntu 16 Mar 9 18:56 link1 -> etc/ufw/ufw.conf
lrwxrwxrwx 1 ubuntu ubuntu 17 Mar 9 19:00 link2 -> /etc/ufw/ufw.conf
ubuntu@ip-172-31-80-155:~/lab5$ cat link2
# /etc/ufw/ufw.conf
#
# Set to yes to start on boot. If setting this remotely, be sure to add a rule
# to allow your remote connection before starting ufw. Eg: 'ufw allow 22/tcp'
ENABLED=no
# Please use the 'ufw' command to set the loglevel. Eg: 'ufw logging medium'.
# See 'man ufw' for details.
LOGLEVEL=low
There is already a better answer, and the first part, thatln -s
"sometimes" doesn't check for the existence of the target is just wrong.
– RalfFriedl
Mar 9 at 19:24
add a comment |
When you try to create a link to a file that doesn't exist (or you've given the path incorrectly) ln -s doesn't throw an error. It creates a link but when you try 'cat' on that link it says no file found. Even when you can see it using ls. In such cases always double-check your file path.
ubuntu@ip-172-31-80-155:~/lab5$ ln -s etc/ufw/ufw.conf link1
ubuntu@ip-172-31-80-155:~/lab5$ ls -al
total 5360
drwxrwxr-x 2 ubuntu ubuntu 4096 Mar 9 18:56 .
drwxr-xr-x 7 ubuntu ubuntu 4096 Mar 9 18:42 ..
-rwxr--r-- 1 ubuntu ubuntu 5478400 Mar 9 18:32 backup.tar
lrwxrwxrwx 1 ubuntu ubuntu 16 Mar 9 18:56 link1 -> etc/ufw/ufw.conf
ubuntu@ip-172-31-80-155:~/lab5$ cat link1
cat: link1: No such file or directory
Because the file etc/ufw/ufw.conf doesn't exist. The correct path is /etc/ufw/ufw.conf
ubuntu@ip-172-31-80-155:~/lab5$ ln -s /etc/ufw/ufw.conf link2
ubuntu@ip-172-31-80-155:~/lab5$ ls -l
total 5352
-rwxr--r-- 1 ubuntu ubuntu 5478400 Mar 9 18:32 backup.tar
lrwxrwxrwx 1 ubuntu ubuntu 16 Mar 9 18:56 link1 -> etc/ufw/ufw.conf
lrwxrwxrwx 1 ubuntu ubuntu 17 Mar 9 19:00 link2 -> /etc/ufw/ufw.conf
ubuntu@ip-172-31-80-155:~/lab5$ cat link2
# /etc/ufw/ufw.conf
#
# Set to yes to start on boot. If setting this remotely, be sure to add a rule
# to allow your remote connection before starting ufw. Eg: 'ufw allow 22/tcp'
ENABLED=no
# Please use the 'ufw' command to set the loglevel. Eg: 'ufw logging medium'.
# See 'man ufw' for details.
LOGLEVEL=low
When you try to create a link to a file that doesn't exist (or you've given the path incorrectly) ln -s doesn't throw an error. It creates a link but when you try 'cat' on that link it says no file found. Even when you can see it using ls. In such cases always double-check your file path.
ubuntu@ip-172-31-80-155:~/lab5$ ln -s etc/ufw/ufw.conf link1
ubuntu@ip-172-31-80-155:~/lab5$ ls -al
total 5360
drwxrwxr-x 2 ubuntu ubuntu 4096 Mar 9 18:56 .
drwxr-xr-x 7 ubuntu ubuntu 4096 Mar 9 18:42 ..
-rwxr--r-- 1 ubuntu ubuntu 5478400 Mar 9 18:32 backup.tar
lrwxrwxrwx 1 ubuntu ubuntu 16 Mar 9 18:56 link1 -> etc/ufw/ufw.conf
ubuntu@ip-172-31-80-155:~/lab5$ cat link1
cat: link1: No such file or directory
Because the file etc/ufw/ufw.conf doesn't exist. The correct path is /etc/ufw/ufw.conf
ubuntu@ip-172-31-80-155:~/lab5$ ln -s /etc/ufw/ufw.conf link2
ubuntu@ip-172-31-80-155:~/lab5$ ls -l
total 5352
-rwxr--r-- 1 ubuntu ubuntu 5478400 Mar 9 18:32 backup.tar
lrwxrwxrwx 1 ubuntu ubuntu 16 Mar 9 18:56 link1 -> etc/ufw/ufw.conf
lrwxrwxrwx 1 ubuntu ubuntu 17 Mar 9 19:00 link2 -> /etc/ufw/ufw.conf
ubuntu@ip-172-31-80-155:~/lab5$ cat link2
# /etc/ufw/ufw.conf
#
# Set to yes to start on boot. If setting this remotely, be sure to add a rule
# to allow your remote connection before starting ufw. Eg: 'ufw allow 22/tcp'
ENABLED=no
# Please use the 'ufw' command to set the loglevel. Eg: 'ufw logging medium'.
# See 'man ufw' for details.
LOGLEVEL=low
edited Mar 9 at 19:40
answered Mar 9 at 19:02
sindhura sindhura
12
12
There is already a better answer, and the first part, thatln -s
"sometimes" doesn't check for the existence of the target is just wrong.
– RalfFriedl
Mar 9 at 19:24
add a comment |
There is already a better answer, and the first part, thatln -s
"sometimes" doesn't check for the existence of the target is just wrong.
– RalfFriedl
Mar 9 at 19:24
There is already a better answer, and the first part, that
ln -s
"sometimes" doesn't check for the existence of the target is just wrong.– RalfFriedl
Mar 9 at 19:24
There is already a better answer, and the first part, that
ln -s
"sometimes" doesn't check for the existence of the target is just wrong.– RalfFriedl
Mar 9 at 19:24
add a comment |
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f511900%2fwhy-doesnt-my-symbolic-link-work%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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