Tag Info

Hot answers tagged

78

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


70

You can't, because they are literally the same file, only reached by different paths. The first one has no special status.


39

In Unix all normal files are Hardlinks. Hardlinks in a Unix (and most (all?) ) filesystems are references to an to what's called an inode. The inode has a reference counter, when you have one "link" to the file (which is the normal modus operandi) the counter is 1. When you create a second, third, fourth, etc link, the counter is incremented (increased ) ...


29

An interesting question, indeed. At first glance I see the following advantages: First of all you state that interpreting "." as the current directory may be done by the Shell or by system calls. But having the dot-entry in the directory actually removes this necessity and forces consistency at even a lower level. But I don't think that this was the basic ...


28

One usage of hardlinks which is extremely useful is in incremental backups combined with rsync. It saves lot of space and makes the restoration procedure really easy. I use that approach for backup in my servers. Take some time to read this explanation.


22

The main advantage of hard links is that, compared to soft links, there is no size or speed penalty. Soft links are an extra layer of indirection on top of normal file access; the kernel has to dereference the link when you open the file, and this takes a small amount of time. The link also takes a small amount of space on the disk, to hold the text of the ...


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


14

If after reading that wikipedia page your question is "why would I ever use them" then you don't understand what hard links are. A link is a directory entry that points to blocks on disk. In other words every file on your system has at least one link. When you rm a file the actual system call is unlink(). It removes the directory entry. The blocks on disk ...


13

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


13

You just need ls (or find). When you create a directory, its link count starts at 2: One for the directory itself One for the . link inside itself The other thing that increases the directory's link count is its subdirectories: they all have a .. entry linking back to their parent, adding one to its link count. You can't hardlink directories in Linux, ...


13

If you copy a file it will duplicate the content. So if you modify the content of a single file, that has no effect on the other one. If you do a hardlink that will create a file pointing on the same content. So if you change the content of either of the files the change will be be seen on both.


12

There is no direct, clean (reliable) way to do that. But under appropriate circumstances this can be possible (or at least probable). The problem is that there are two hard links but just one file. Change, modification and (maybe) creation time are stored for files (inodes) only but not for directory entries (the hard links). Thus the information you want ...


11

A file is an inode with meta data among which a list of pointers to where to find the data. In order to be able to access a file, you have to link it to a directory (think of directories as phone directories, not folders), that is add one or more entries to one of more directories to associate a name with that file. All those links, those file names point ...


11

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

. is actually the current working directory in either case; it has nothing to do with the directory holding the script: [/tmp] $ echo "realpath ." > test.sh && chmod +x test.sh [/tmp] $ /tmp/test.sh /tmp [/tmp] $ cd /usr/bin [/usr/bin] $ /tmp/test.sh /usr/bin


10

The different semantics between hard and soft links make them suitable for different things. Hard links: indistinguishable from other directory entries, because every directory entry is hard link "original" can be moved or deleted without breaking other hard links to the same inode only possible within the same filesystem permissions must be the same as ...


10

Hard links are very useful for disk-based backup mechanisms, because you can have a full directory tree for each backup while sharing the space for files that haven't changed — and the filesystem keeps track of reference counting, so when the last reference to a given version goes away because the backup was expired/removed for space reasons, the space it ...


10

If you rely on the last modification time of the directories and you don't have knowledge of how and when those directories are changed, relying on mtime is going to lead you to be wrong some percentage of the time. The issue here is that the file is represented in the filesystem by an inode, not by a directory entry. The directory entry (filename) points ...


9

With the exception of mount points, each directory has one and only parent: ... One way to do pwd is to check the device:inode for '.' and '..'. If they are the same, you have reached the root of the file system. Otherwise, find the name of the current directory in the parent, push that on a stack, and start comparing '../.' with '../..', then '../../.' ...


9

rsync has a -H or --hard-links option for this, and has the usual rsync benefits of being able to be stopped and restarted, and to be re-run to efficiently deal with any files that were changed during/after the previous run. -H, --hard-links This tells rsync to look for hard-linked files in the source and link together the corresponding files on ...


9

A hard link is basicly a second filename for the same file. So if you hardlink a file, it will only be once on the filesystem, and therefore only take up space once. So you want to use this if you wish to save diskspace


9

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

(Hmm: the following is now a bit of an epic...) The design of the directory on unix filesystems (which, to be pedantic, are typically but not necessarily attached to unix OSs) represents a wonderful insight, which actually reduces the number of special cases required. A 'directory' is really just a file in the filesystem. All the actual content of files ...


8

By default, if you tell tar to archive a file with hard links, and more than one such link is included among the files to be archived, it archives the file only once, and records the second (and any additional names) as hard links. This means that when you extract that archive, the hard links will be restored. If you use the --hard-dereference option, then ...


8

I think this question is (quite reasonably) misguided as to what a hard link really is. I think however the most correct direct answer is 'They both are'. Unix file systems normally store actual file contents and data in i-nodes, these do not have a path whatsoever, paths then have a many to one relationship to these i-nodes. Take as an analogy a person who ...


7

Btrfs has native support for snapshots, so you wouldn't have to use hard links for deduplication. You could recreate your current setup by creating a btrfs filesystem and loading it with the earliest revision that you need, and taking a snapshot, and then revving the repository forward to each point in time that you need a snapshot of and taking a snapshot ...


7

It is usually true on unix systems that the number of links to a directory is the number of subdirectories plus 2. However there are cases where this is not true: Some unices allow hard links to directories. Then there will be more than 2 links that do not correspond to subdirectories. There are filesystems where directories do not have entries for . and ...


7

Posix requires that the operating system understand the concept of hard links but not that hard links can actually be used in any particular circumstance. You can find out how many hard links are permitted at a particular location (this can vary by filesystem type) by calling pathconf(filename, _PC_LINK_MAX). The minimum limit (_POSIX_LINK_MAX) is 8, but ...


7

Looking at the ext3 inode structure disk format in the linux kernel sources (*include/linux/ext3_fs.h*) that lists the links count as being a 16 bit number struct ext3_inode { ... snip ... __le16 i_links_count; /* Links count */ } I guess that means that an ext3 filesystem can have up to 65535 links. I haven't checked the values for other ...


7

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



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