Hot answers tagged

44

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 ...


21

You're looking for test: -h pathname True if pathname resolves to a file that exists and is a symbolic link. False if pathname cannot be resolved, or if pathname resolves to a file that exists but is not a symbolic link. If the final component of pathname is a symlink, that symlink is not followed. Most shells have it as a builtin, but test also ...


17

Shell aliases have the feature that you can do (some) name-completion on them (usually bound to tab). Alternatively, you can use the CDPATH feature, which "recently" (within the past 5-6 years) has been improved to support name-completion. If that works for you, it has the advantage that the what you type is the actual name of the directory rather than a ...


16

For the directories you frequent often, but don't change daily, another option is just to have several alias commands in your .bashrc file: alias cdo="cd /u01/app/oracle" alias cdw="cd /var/www/html" A friend has about 50 of those; I have a handful; quick and easy. Just cdo to change directory to /u01/app/oracle


12

In this case, that's a Debian "alternative", so to get more details, you could use: $ update-alternatives --display gnome-text-editor gnome-text-editor - auto mode link best version is /usr/bin/gedit link currently points to /usr/bin/gedit link gnome-text-editor is /usr/bin/gnome-text-editor slave gnome-text-editor.1.gz is /usr/share/man/man1/gnome-...


11

You can't do this with a single invocation of ln,but you could loop through all necessary destinations: $ for i in "$HOME/Documents/" "$HOME/Desktop/"; do ln -s "$HOME/file" "$i"; done


10

You could use tab completion. By default on many Linux distributions, bash is set up so that when you hit the [TAB] key, you're given a list of possible matches, or if there's just one match, it's all filled out. For cd, this is normally a list of subdirectories of the current working directory. You could overwrite that, but I suggest instead making an alias,...


10

readlink is the command you are looking for. $ readlink -e /usr/bin/gnome-text-editor /usr/bin/gedit Various flags (-f, -e, -m) are available according to how you'd like to behave in case of broken links - see man readlink for details.


9

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 ...


9

There are two things to remember: ln -s (without -r) stores the target name literally as you pass it to it if you pass a relative target, it resolves relatively to the link name, not your current working directory Example: I'm in /home/user/d0 and I want a link to /home/user/file so I do: ln -s ../file . ../file is a valid path from d0. Now if ...


8

If you did mkdir /tmp/foo ln -s ../../etc/passwd /tmp/foo Then access /tmp/foo/passwd would convert to /tmp/foo/../../etc/passwd - ie /etc/passwd If you did mkdir /var/tmp/foo ln -s ../../etc/passwd /var/tmp/foo Then access to /var/tmp/foo/passwd would convert to /var/tmp/foo/../../etc/passwd - ie /var/etc/passwd So you can see the "relative symlink ...


7

Some systems support changing the permission of a symbolic link, others do not. chmod -- change file modes or Access Control Lists (OSX and FreeBSD, using -h) -h If the file is a symbolic link, change the mode of the link itself rather than the file that the link points to. chmod - change file mode bits (Linux) chmod never changes ...


7

The basic idea is that you can't access files outside the document root. That's the point of the document root. There are several protections that prevent Apache from following symlinks outside the root. You need to enable FollowSymlinks in the directory containing the link, but if you can browse the files, it means this is already done. You also need to ...


6

Try using realpath: realpath --relative-to=/foo/bar/something /foo/hello/world For more examples, see: Convert absolute path into relative path given a current directory at SO


6

I recommend pushd and popd. I personally do find them handy when doing development work / reading source code when multiple directories are involved. They effectively implement a stack structure/LIFO, where you pushd a directory, and the next popd command retrieves it. So, when inside a dir, you would do: pushd . And when you need to retrieve it, you ...


6

Symlinks Autodereferenced references to filenames A symlink is literally a text file that's treated specially by the kernel and whose contents is a path to another file/directory. You can read the contents of a symlink file with readlink, and if you standardly open a symlink file, the system will open the file/directory referenced by contents of the ...


6

Set the mark-symlinked-directories readline option. The usual way to do this is to edit ~/.inputrc, put set mark-symlinked-directories on in there, then start a new bash shell (or press Ctrl+X Ctrl+R to reload your readline settings). Less commonly, you could also put it straight in your ~/.bashrc like this bind 'set mark-symlinked-directories on'


6

If you have gnu parallel you could try with parallel ln -s /path/file {} ::: /path/dest1 /path/dest2 /path/dest3 or, to symlink multiple targets to (the same) multiple destinations parallel ln -s {1} {2} ::: /path/file1 /path/file2 ::: /path/dest1 /path/dest2


6

It can remove files, but directories are not "files". ➜ lab touch file ➜ lab mkdir dir ➜ lab ln -sfT /home file ➜ lab ln -sfT /home dir ln: dir: cannot overwrite directory This is seen in the source: if (remove_existing_files || interactive || backup_type != no_backups) { dest_lstat_ok = (lstat (dest, &dest_stats) == 0); ...


6

In UNIX, directories are special (I feel myself channeling The Church Lady from SNL). Directories contain other files, so deleting them requires a different operation. Even when a directory is empty it still has two files in it (. and ..) so deleting a directory can't be done until it's really empty and the link counts on the relevant files have been ...


5

The command you ran created a symbolic link in the current directory. Judging by the prompt, the current directory is your home directory. Creating symbolic links to executable programs in your home directory is not particularly useful. When you type the name of a program, the shell looks for it in the directories listed in the PATH environment variable. To ...


5

$0 doesn't hold the name of the calling program but the name of the called program. The called program is ./my_command so $0 will be ./my_command too. The fact it is a symbolic link doesn't make a difference.


5

rsync -a --copy-unsafe-links src/ dest From the man page: --copy-unsafe-links This tells rsync to copy the referent of symbolic links that point outside the copied tree. Absolute symlinks are also treated like ordinary files, and so are any symlinks in the source path itself when --relative is used. This option has no ...


5

A file does not keep track of the symbolic links pointing to it. Instead, search for symbolic links under your tree and find out which file/dir they point to using readlink: find -type l -exec readlink -e -- "{}" \+ | sort | uniq Since find's default behavior is recursion, this will work for arbitrary depth.


5

Correct for a hard link but not for a symbolic link. The content of a symbolic link is just a string. This allows a symbolic link to point to a filesystem that isn't currently mounted, for instance. A symbolic link is just text; when the kernel encounters one, it essentially inserts the symbolic link into the path that it was looking up, breaking it up into ...


5

It's no less verbose than two separate ln -s invocations: echo $HOME/Documents/ $HOME/Desktop/ | xargs -n 1 ln -s $HOME/file but that only works for absolute paths (because symbolic links are interpreted relative to their parent directory, unless they're absolute). (The relative cost drops of course as the number of links goes up. Also, this snippet ...


5

If you're using bash, zsh or any POSIX shell, then cd -P pwd -P or to make it permanent. in bash: set -P or set -o physical in zsh: set -w or set -o chaselinks in other POSIX shells: alias cd='cd -P' in tcsh: set symlinks = chase But the effect you're seeing is created by the shell, not by the link itself. So the answer to your actual question is "no"...


4

I don't see any advantage to hard links. With hardlinks, you can move the original file (rename it) as needed without needing to recreate the link. That strikes me as a bug rather than a feature. If you want to disable a site (for example because you've just noticed that it has a major security hole), with symbolic links, you can just rename the sites-...


4

You could do this, to supply tar with a list of all files inside protTests except those which are symlinks: find protTests -maxdepth 1 -mindepth 1 -not -type l -print0 | tar --null --files-from - -cvf protTests.tar By the way, your existing command: tar -cvf protTests.tar protTests/* will not archive all files in protTests, it will only archive ...


4

As you showed, system/nrcalc is a symlink on the local system, but it's a directory on the remote server. If the directory on the remote system is not empty, rsync will refuse to delete it (to make way for the symlink) unless you specify --force or one of the --delete options. If you don't want to replace the directory on the far end, but you still need to ...



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