Hot answers tagged

64

Here's what's happening. If you make a symlink with a relative path, the symlink will be relative. Symlinks just store the paths that you give them. They never resolve paths to full paths. Running $ pwd /usr/bin $ ln -s ls /usr/bin/ls2 creates a symlink named ls2 in /usr/bin to ls(viz. /usr/bin/ls) relative to the directory that the symlink is in ...


43

It's not a bug. The use case is for when you want to link a file to the same basename but in a different directory: cd /tmp ln -s /etc/passwd ls -l passwd lrwxrwxrwx 1 xxx xxx 11 Jul 29 09:10 passwd -> /etc/passwd It's true that when you do this with a filename that is in the same directory it creates a link to itself which does not do a whole lot of ...


38

All the ln means "link", not just the "l". Just the same as ls meaning "list", cp means "copy" and mv means "move". They are part of the "two letter commands", for example: ar — ARchive as — ASsembler bc — Basic Calculator cc — C Compiler cp — CoPy files and directories dc — Desk Calculator dd — Data Description: convert and copy a file df — Disk Free: ...


31

Many programs make use of this technique where there is a single executable that changes its behavior based on how it was executed. There's typically a structure inside the program called a case/switch statement that determines the name the executable was called with and then will call the appropriate functionality for that executable name. That name is ...


30

As Dubu points out in a comment, the issue lies in your relative paths. I had a similar problem symlinking my nginx configuration from /usr/local/etc/nginx to /etc/nginx. If you create your symlink like this: cd /usr/local/etc ln -s nginx/ /etc/nginx You will in fact make the link /etc/nginx -> /etc/nginx, because the source path is relative to the link's ...


23

First of all, to find what a command's options do, you can use man command. So, if you run man ln, you will see: -f, --force remove existing destination files -s, --symbolic make symbolic links instead of hard links Now, the -s, as you said, is to make the link symbolic as opposed to hard. The -f, however, is not to remove the ...


21

You can't without writing a bit of code. Those symlink shortcuts work because vim is written that way. It looks at how (with what name) it was started and acts as if it had been called with the appropriate command line options. This behavior is hardcoded in the executable, it is not a trick done by the symbolic link. So if you want to do that yourself, the ...


14

Because in the second ln it doesn't fail it creates a symlink_dir/dir_2 -> dir_2 symbolic link Do a: ls -l symlink_dir/dir_2 And you'll see a (probably broken) symlink there. That's how ln is meant to work if the target is a directory (or a symlink to a directory). A third ln could fail because there's already a dir_2 inside symlink_dir (aka ...


12

The easiest way to link to the current directory as an absolute path, without typing the whole path string would be ln -s "$(pwd)/foo" ~/bin/foo_link The target argument for the ln -s command works relative to the symbolic link's location, not your current directory. It helps to imagine that the created symlink simply holds the text you provide for the ...


9

You forgot the initial slash before bin/python. This means /usr/bin/prj-python now points to /usr/bin/bin/python. What would you like it to point to exactly?


8

On the surface, what you've suggested you've tried works for me. Example $ mkdir -p test/src test/firefox $ tree --noreport -fp . `-- [drwxrwxr-x] ./test |-- [drwxrwxr-x] ./test/firefox `-- [drwxrwxr-x] ./test/src Make the symbolic link: $ ln -s test/src test/firefox $ tree --noreport -fp . `-- [drwxrwxr-x] ./test |-- [drwxrwxr-x] ...


8

ln -f "$(readlink <symlink>)" <symlink>


8

The easiest way to find out of course, is to try it and see. When no 2nd argument is given, ln will create a link in the current directory with the same name as the original: $ ln -s /etc $ ls -l lrwxrwxrwx 1 terdon terdon 4 Jul 29 16:09 etc -> /etc This is also explained in man ln: In the 2nd form, create a link to TARGET in the current ...


7

Things to check: Perform an ls -l on /usr/bin/prj-python if its likelrwxrwxrwx (...) /usr/bin/prj-python -> bin/python the file will actually be searched at /usr/bin/bin/python (that's what xralf tried to say). Fix:rm /usr/bin/prj-pythonln -s /full/path/to/your/python /usr/bin/python-prj If your bin/python is a shell script (aka. wrapper-script) check ...


7

When you write ln -s VALUE link_name it creates a symbolic link with value VALUE. This is what you got. If you want to create a relative link, it is best to cd to the directory where you want to put the link: $ cd ~/bin $ ln -s ../programming/tmux/tmux . Shell completion will help you.


7

On a Linux system, when changing the ownership of a symbolic link using chown, by default it changes the target of the symbolic link (ie, whatever the symbolic link is pointing to). If you'd like to change ownership of the link itself, you need to use the -h option to chown: -h, --no-dereference affect each symbolic link instead of any referenced ...


6

From the ln man page: When creating hard links, each TARGET must exist. No mention of symlinks there; in fact, this statement seems to imply that this is not the case for symlinks. As I said in my comment on your question, when creating a symlink to a non-existent source, a broken link is created: $ ln -sfv blah blabla 'blabla' -> 'blah' $ file ...


6

The results of both has to be the same, in that a hard link is created to the original file. The difference is in the intended usage and therefore the options available to each command. For example, cp can use recursion whereas ln cannot: cp -lr <src> <target> will create hard links in <target> to all files in <src>. (it creates ...


5

You can do this in a roundabout way via your shell configuration. Something like alias big_vim='gvim -p -geom 126x512' would work in bash/zsh. It allows you to customise things without messing with sudo/root.


5

Read your man page: Question 1 = 1st Form, this is because in linux all items are considered files, even directories. As an example, use your text editor to "open" /etc/, ie: nano -w /etc/ nano will politely tell you /etc/ is a directory Since it's technically legal to create never ending symlinks. In the old days, before the bounds check was written, I ...


4

You can use the -f, --force option of ln to have it remove the existing symlink before creating the new one. If the destination is a directory, you need to add the -n, --no-dereference option to tell ln to treat the symlink as a normal file. ln -sfn target existing_link However, this operation is not atomic, as ln will unlink() the old symlink before ...


4

You can't: A symlink is simply an extra inode (a structure that points to the file) and this inode consists of, amongst other things, a deviceId and an inode pointer. The deviceId effectively points to a device special file within the /dev directory and the inode pointer points to a block on that device. Your network location of 10.0.1.103 does not and ...


4

mv moves a file, and ln -s creates a symbolic link, so the basic task is accomplished by a script that executes these two commands: #!/bin/sh mv -- "$1" "$2" ln -s -- "$2" "$1" There are a few caveats. If the second argument is a directory, then mv would move the file into that directory, but ln -s would create a link to the directory rather than to the ...


3

It depends on where your temporary directory is. That is, have you created your own temporary directory, or are you using the system's (/tmp)? In your scenario, you are expecting the files/folder to remain after the temporary directory has been cleaned up. If it's in the system's /tmp directory then it may well be cleaned up by the system (it's distro ...


3

The link() system call on the NFS client should map directly to the NFS LINK operation, which the server should implement using its link() system call. So as long as link() is atomic on the server, it will also be atomic on the clients.


3

This will create symlinks /somedir/dir1/* pointing to /media/sd*/dir1/*. mkdir /somedir/dir1 ln -sf /media/sd*/dir1 /somedir/dir1 If a file or directory exists in more than one /media/sd*/dir1/ then the link will point to the last one, for example if you have: /media/sda1/dir1/Movies /media/sda1/dir1/Pictures /media/sdb1/dir1/Data you will get: ...


3

There's no flag to do this with ln. Creating directories is not its job. mkdir -p foo/bar/qux will create foo, foo/bar and foo/bar/qux as needed. So call mkdir -p on all but the last path component first. It sounds like you're reinventing the wheel Stow, a simple package manager that merges directory hierarchies by creating directories to the required ...


3

Windows has a special syntax \\MACHINE\DIRECTORY…\FILE meaning the file located at \DIRECTORY…\FILE on the machine called \\MACHINE over the SMB protocol. This is built into the operating system and specialized to one network protocol. Linux has a flexible filesystem based on the notion of mounting. Filesystems are attached to an existing directory, and the ...


3

What is the advantage to keep symbolic links relative to directory? Because this allows one to move the direcoty itself without breaking the symbolic links? Exactly. In addition, is it possible to create a symbolic link with .. (parent directory) in the path without being in the directory? ln -sv '/etc/init.d/rsyslog' '/etc/rc3.d/../init.d/rsyslog' ...


3

I created a script that will do this. The script converts all hard-links it finds in a source directory (first argument) that are the same as in the working directory (optional second argument) into symbolic links: https://gist.github.com/rubo77/7a9a83695a28412abbcd It has an option -n for a dry-run, that doesn't do anything but shows what would be done. ...



Only top voted, non community-wiki answers of a minimum length are eligible