Tag Info

Hot answers tagged

58

There are many reasons for broken symbolic links: A link was created to a target which no longer exists. Resolution: remove the broken symlink. A link was created for a target which has been moved. Or it's a relative link that's been moved relative to its target. (Not to imply that relative symlinks are a bad idea — quite the opposite: absolute symlinks ...


19

Jim's answer explains how to test for a symlink: by using test's -L test. But testing for a "hard link" is, well, strictly speaking not what you want. Hard links work because of how Unix handles files: each file is represented by a single inode. Then a single inode has zero or more names or directory entries or, technically, hard links (what you're calling ...


16

Do not blindly remove all dangling symbolic links. They may exist just to carry some information, and may be safer than normal files since a symlink creation is atomic. For instance, Firefox creates a lockfile "lock" that is a symlink whose value has a form like "IP_address:+PID".


15

With POSIX shell, you can use -P option of cd builtin: cd -P <link> With bash, from man bash: The -P option says to use the physical directory structure instead of following symbolic links (see also the -P option to the set builtin command)


14

Most shells have a CDPATH variable that cd can lookup for directories to change to in the same way that executables are searched in $PATH. So if you add your symlinks in a ~/projects directory and do CDPATH=~/projects, you'll be able to do cd foo to go in ~/projects/foo With zsh, if $var contains a path you can do cd ~var to cd to that path. The useful ...


13

Use rsync's option -K (--keep-dirlinks). From the manpage: -K, --keep-dirlinks This option causes the receiving side to treat a symlink to a directory as though it were a real directory, but only if it matches a real directory from the sender. Without this option, the receiver’s symlink would be ...


13

In most shells including bash, pwd is a shell builtin: $ type -a pwd pwd is a shell builtin pwd is /bin/pwd If you use /bin/pwd, you must use the -L option to get the same result as builtin pwd: $ ln -s . test $ cd test && pwd /home/cuonglm/test $ /bin/pwd /home/cuonglm $ /bin/pwd -L /home/cuonglm/test By default, /bin/pwd ignores symlinks and ...


13

Anytime you have these types of questions it's best to conceive of a little test to see what's actually happening. For this you can use strace. unlink $ touch file1 $ strace -s 2000 -o unlink.log unlink file1 rm $ touch file1 $ strace -s 2000 -o rm.log rm file1 When you take a look at the 2 resulting log files you can "see" what each call is actually ...


12

On linking You generally do not link /usr/local/* with /bin, but this is more of a historical practice, in general, there are a few "technical" reason why you cannot do what you're suggesting. Making links to executables in /bin can cause problems: Probably the biggest caveat would be if you're system is having packages managed by some sort of package ...


12

An example: $ touch f1 $ ln f1 f2 $ ln f1 f3 $ ln -s f1 s1 $ ln -s f2 s2 $ ln -s ./././f3 s3 $ ln -s s3 s4 $ ln s4 s5 $ ls -li total 0 10802124 -rw-r--r-- 3 stephane stephane 0 Nov 12 19:55 f1 10802124 -rw-r--r-- 3 stephane stephane 0 Nov 12 19:55 f2 10802124 -rw-r--r-- 3 stephane stephane 0 Nov 12 19:55 f3 10802345 lrwxrwxrwx 1 stephane stephane 2 Nov 12 ...


10

With zsh, it's just: mypath=$0:A Now for other shells, though realpath() and readlink() are standard functions (the latter being a system call), realpath and readlink are not standard command, though some systems have one or the other or both with various behaviour and feature set. As often, for portability, you may want to resort to perl: abs_path() { ...


9

You probably want to use variables instead of symbolic links, e.g. export project=/home/me/project then cd $project or vim $project/file UPDATE As pointed out by peterph, you can also combine these (including predefined variables), e.g. export project=$HOME/project


8

For the stated question you can use find: find . -mindepth 1 ! -type l will list all files and directories in the current directory or any subdirectories that are not symlinks. mindepth 1 is just to skip the . current-directory entry. The meat of it is the combination of -type l, which means "is a symbolic link", and !, which means negate the following ...


8

A symlink actually stores the path you give literally, as a string¹. That means your link ~/mylink contains "." (one character). When you access the link, that path is interpreted relative to where the link is, rather than where you were when you made the link. Instead, you can store the actual path you want in the link: ln -s "$(pwd)" ~/mylink using ...


8

Using the -h and -L operators. -h file true if file is a symbolic link -L file true if file is a symbolic link http://www.mkssoftware.com/docs/man1/test.1.asp According to this SO thread, they have the same behavior, but -L is preferred.


7

Splitting files in /etc across partitions is a bad idea for this reason. What is happening is that the groupadd utility is creating a temporary file, and then replacing the real /etc/groups file (or rather, what the symlink points to) with the temporary one via a simple rename operation. The catch is that rename() only works on the same filesystem, ...


7

which 2 commands? /usr/bin/java is a soft (symbolic) link to /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/bin/java There is no difference as they are the same file. If you type something like ls -l /usr/bin/java You might get a result such as: lrwxrwxrwx. 1 root root 22 Aug 5 17:01 /usr/bin/java -> /etc/alternatives/java Which would mean ...


7

Because bash (and possibly other shells) track the path you descended, including symlinks, in order to make your trail back up look like the one down. Bash knows how you got to the working directory because cd must be a shell built-in. When you run ls .. the shell can't substitute the "symbolic path" because grep .. is also valid and translating .. would be ...


6

function lnmv set dest_dir $argv[1] set files $argv[2..-1] for f in $files set dest $dest_dir/$f mv -- $f $dest and ln -s -- $dest $f end end


6

From version 2.12 onwards, the -r option for GNU grep doesn’t dereference symbolic links unless you specify them by hand: -r, --recursive Read all files under each directory, recursively, following symbolic links only if they are on the command line. This is equivalent to the -d recurse option. -R, --dereference-recursive ...


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


5

Edit in response to updated question Since you only care about links, directories and regular files, and don't need to deal with the other filetypes that ls can identify (FIFOs, sockets etc), you could do something like stat. For the examples below, I have created the following test environment: $ ls -l total 4.0K -rw-r--r-- 1 terdon terdon 0 Jun 30 ...


5

Shells keep track of symbolic links in the path to the current directory (this is known as logical current directory tracking). If you want to expand all symbolic links, pass the option -P to the cd builtin (for physical current directory tracking): cd -P logic If you're in a directory which you've accessed via a symbolic link and want to switch the ...


5

GNUly: find . -lname '/foo*' -printf '%p\0%l\0' | awk -vRS='\0' ' { getline target sub("^/foo", "/bar", target) printf("%s\0%s\0", target, $0) }' | xargs -r0n2 ln -sfT Or with recent GNU sed: find . -lname '/foo*' -printf '%l\0%p\0' | sed -z 's|^/foo|/bar|;n' | xargs -r0n2 ln -sfT Beware that you will potentially be ...


5

Both the fnord and the Gatling webserver use the Unix filesystem as their configuration database (as opposed to, say, Microsoft IIS, which uses the Windows registry, or Apache, which uses a complex-to-parse configuration file). For example, virtual hosts are just directories, and creating a new virtual host is as simple as mkdir www.example.com:80 ...


5

With a single file, rm and unlink do the same task, remove the file. As POSIX defined, rm and unlink both call to unlink() system call. In GNU rm, it calls to unlinkat() system call, which is equivalent to the unlink() or rmdir() function except in the case where path specifies a relative path. Note On some systems, unlink can also remove directory. At ...


5

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.


5

You could write a script that runs trough directory B that creates a link for every file in directory A. But, if you want to merge the two folders, I recommend using an overlay filesystem such as aufs. I use it myself for such a task. Use the following mount command (You may have to install the tools to manage aufs): mount -t aufs -o ...


4

Answering to the questions asked: Is this correct? No, it is a poor practice. Are there some hidden side-effects to my suggestion? Yes there are several side effects. Your suggestion might work or not depending on the application, and might regress or be broken in the long term. There are sensible reasons not to create such a symbolic link: ...


4

I don't think there's such an utility. With GNU readlink, you could do something like: is_in() ( needle=$(readlink -ve -- "$1" && echo .) || return haystack=$(readlink -ve -- "$2" && echo .) || return needle=${needle%??} haystack=${haystack%??} haystack=${haystack%/} needle=${needle%/} case $needle in ("$haystack" | ...



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