Tag Info

Hot answers tagged

59

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


17

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


17

autofs can do this for you. You can configure any number of mountpoints with various options, and the corresponding filesystems are mounted whenever the mountpoint is accessed. After a given amount of inactivity the filesystems are unmounted again. There are no doubt various ways of using autofs, but here's one way of doing what you're trying to do, based ...


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

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


13

That is a feature of the shell that remembers how you got to where you are. If you have realpath installed you can do: $ realpath /home/dazz/test/1 And lacking that if you have python: $ python -c "import os; print(os.path.realpath('.'))" /home/dazz/test/1 or readlink (from coreutils): $ readlink -f . /home/dazz/test/1 or /bin/pwd (not the shell ...


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


11

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() { ...


10

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


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


9

You can do this with systemd, so you don't have to install extra software and just have a small amount of extra configuration. Simply add noauto,x-systemd.automount to the options in fstab. noauto to not mount automatically on boot and x-systemd.automount to let systemd mount it on access. Source: ArchWiki - fstab


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

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


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.


8

Becoming familiar with your file system layout is all part of becoming a competent user - any time you spend with that aim in mind is not time wasted. However, with that said, you can indeed make it easier to move around the file system. Note that in Linux/UNIX, the file system is presented as a single tree, no matter how many devices make up your storage, ...


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

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

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


6

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


6

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


6

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


6

Contrast pwd and /bin/pwd. pwd, which is a built-in command in many shells, tells you where your shell thinks you are (and hence treats symlinks "soft links" as if they were real directories. /bin/pwd is an external program that tells you where you really are, if necessary by traversing the filesystem tree up to /. It takes no account of symlinks because ...


6

Other answers have covered /bin/pwd vs the shell's builtin pwd. If you want to follow symlinks in the Windows style you mentioned, use cd -P: it will change the PWD variable accordingly. If you want to use -P by default, you can add this line to your .bashrc or .zshrc: set -P Other shells may vary.


5

Edit smb.conf [global] unix extensions = no [share] follow symlinks = yes wide links = yes


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

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.



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