Tag Info

Hot answers tagged

73

This is just a bad idea, as there is no way to tell the difference between a hard link and original name. Allowing hard links to directories would break the directed acyclic graph structure of the filesystem, possibly creating directory loops and dangling directory subtrees, which would make fsck and any other file tree walkers error prone. First, to ...


73

I'd strongly suggest not to use find -L for the task (see below for explanation). Here are some other ways to do this: If you want to use a "pure find" method, it should rather look like this: find . -type l -xtype l (xtype is a test performed on a dereferenced link) This may not be available in all versions of find, though. But there are other options ...


55

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


36

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


29

You can use the readlink utility, with the -f option: -f, --canonicalize canonicalize by following every symlink in every component of the given name recursively; all but the last component must exist Some distributions, for example those that use GNU coreutils and FreeBSD, also come with a realpath(1) utility that basically ...


27

There is no command to retarget a symbolic link, all you can do is remove it and create another one. Assuming you have GNU utilities (e.g. under non-embedded Linux or Cygwin), you can use the -lname primary of find to match symbolic links by their target, and readlink to read the contents of the link. Untested: find /mnt/home/someone/something -lname ...


27

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


25

Patrice identified the source of the problem in his answer, but if you want to know how to get from there to why you get that, here's the long story. The current working directory of a process is nothing you'd think too complicated. It is an attribute of the process which is a handle to a file of type directory where relative paths (in system calls made by ...


24

Symbolic links do take room, of course, but just the room it takes to store the name and target plus a few bytes for other metadata. The space taken by a symbolic link does not depend on the space taken by the target (after all, the target is not even required to exist). Plain du reports the space taken by a directory tree on the disk. du -L reports the ...


23

Try this line: readlink -f `which command` If command is in your $PATH variable , otherwise you need to specify the path you know.


20

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


19

With mount --bind, a directory tree exists in two (or more) places in the directory hierarchy. This can cause a number of problems. Backups and other file copies will pick all copies. It becomes difficult to specify that you want to copy a filesystem: you'll end up copying the bind-mounted files twice. Searches with find, grep -r, locate, etc., will traverse ...


18

According to the POSIX manpage for pwd, the -P option may be of use: -P The absolute pathname written shall not contain filenames that, in the context of the pathname, refer to files of type symbolic link. Thus $ pwd -P should be what you need.


18

This is the result of a hard-coded limit in the Linux kernel source; to prevent denial-of-service, the limit on the number of nested symlinks is 40 (found in the follow_link() function inside fs/namei.c, called by nested_symlink() in the kernel source). You would probably get a similar behaviour (and possibly another limit than 40) with other kernels ...


18

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


17

With any POSIX implementation of cd, you can use the -P option to do this. From bash's help cd: -P use the physical directory structure without following symbolic links You can see it in action here: $ mkdir foo $ ln -s foo bar $ cd -P bar $ pwd /tmp/tmp.WkupF2Ucuh/foo If you want this to be the default behaviour, you can either create an ...


16

Symlinks are essentially just pointers to another file, you can't point to something outside the chroot because it is looking for a file with that name (/var/www, which doesn't exist inside the chroot). Hardlinks on the other hand are pointers to the inode. As such, if you want to do that, you need to use a hard link by omitting -s. However, you cannot hard ...


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

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


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

You can use this to delete all symbolic links: find -type l -delete with modern find versions. On older find versions it may have to be: find -type l -exec rm {} \; # or find -type l -exec unlink {} \; To limit to a certain link target, assuming none of the paths contain any newline character: find -type l | while IFS= read -r lnkname; do if [ ...


14

With a modern find that supports -lname: find /usr/local/bin -lname '/usr/local/texlive/*' -delete should do it.


14

add switch -h touch -h -t 201301291810 myfile.txt Mandatory arguments to long options are mandatory for short options too. -a change only the access time -c, --no-create do not create any files -d, --date=STRING parse STRING and use it instead of current time -f (ignored) -h, --no-dereference ...


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

Every program can see the full command line that was used to run it (except for wildcards and variables, which the shell expands). In a C program, the command line is stored in argv, which is short for argument vector. The progam's name is the first element of argv, i.e. argv[0]. Clearly in the case of halt and reboot, the program is changing its behavior ...


12

This can indeed be done atomically with rename(2), by first creating the new symlink under a temporary name and then cleanly overwriting the old symlink in one go. As the man page states: If newpath refers to a symbolic link the link will be overwritten. In the shell, you would do this with mv -T as follows: $ mkdir a b $ ln -s a z $ ln -s b z.new $ ...


12

The point of both types of links is to provide a way to make a file appear in two locations at the same time. This has a lot of uses. 9 times out of 10 you want to use symbolic links. Symbolic links, or "symlinks" work a little like Windows shortcuts. The contents of a symlink are a pointer to the real location of the file/directory. If you delete the ...


12

The shell stores the current working directory in $PWD. That's what's used for the shell builtins cd and pwd, and it treats symlinks as normal directories, as you've seen. Sometimes this is helpful, sometimes not. You can find the real directory using pwd (type help pwd for more details): $ pwd /tmp/A/Blink $ pwd -L /tmp/A/Blink $ pwd -P /tmp/B Likewise, ...


11

You could use mount to remount the directories you need in your jail: # mount --bind /bin /chroot/bin # mount --bind /lib /chroot/lib # chroot /chroot For use in /etc/fstab: /bin /chroot/bin none bind /lib /chroot/lib none bind Cheers!



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