157

Directory hardlinks break the filesystem in multiple ways They allow you to create loops A hard link to a directory can link to a parent of itself, which creates a file system loop. For example, these commands could create a loop with the back link l: mkdir -p /tmp/a/b cd /tmp/a/b ln -d /tmp/a l A filesystem with a directory loop has infinite depth: cd ...


113

No, a hard link is completely different. A soft link is closer to a Windows shortcut (though there are important differences, symbolic links are more similar to windows shortcuts than hard links are). A hard link is a different thing and one you will almost never need. Briefly, a soft link is created with this command: ln -s foo bar If you then run ls -l,...


77

"You generally should not use hard links anyway" is over-broad. You need to understand the difference between hard links and symlinks, and use each as appropriate. Each comes with its own set of advantages and disadvantages: Symlinks can: Point to directories Point to non-existent objects Point to files and directories outside the same filesystem Hard ...


43

FYI, you can achieve the same thing as hard links for directories by using mount: mount -t bind /var/www /home/user/workspace/www This is very dangerous because most tools and programs will not be aware of the binding. I once did something like in the above example and then proceeded to rm -rf /home/user. Luckily, there was nothing relevant in /var/www.


19

The reason hard-linking directories is not allowed is a little technical. Essentially, they break the file-system structure. You should generally not use hard links anyway. Symbolic links allow most of the same functionality without causing problems (e.g ln -s target link).


18

There's a good explanation of what soft and hard links are, but one thing needs to be clarified. Windows shortcuts are equivalent or similar to neither soft links nor hard links. On the file system level they are just files. It's the shell that understands their structure and interprets them as links. Windows shortcuts can also point to objects in shell ...


9

I use a hard link where I need a single file in two or more places, I predict that one day I'll want to delete one of the locations, and I may forget that I have a link to the file. This prevents me from ending up with a symbolic link to a file that no longer exists. Clarification: A file name is, in fact, a hard link to the file. Thus, every file has at ...


9

$ find -type f -links +1 That will show all regular files that have more than one link (name) to them. It will not tell you which names are linked to the same file, for that you could use -samefile or -inum, e.g. find -samefile "$somefile" In the technical sense, all files (file names) are (hard) links, it's just that files with more than one link pointing ...


8

I don't really understand what you mean. I think you have misunderstood what hard links are. First of all, all files are hardlinks. Every single one. A file is just a link pointing to an inode. A symlink, on the other hand, is a link pointing to a hardlink (to a path). How could the two be combined? More to the point, the functionality is very different. ...


8

Do this: LS_COLORS="mh=44;37" ls -l And you may edit your ~/.profile to change LS_COLORS accordingly. Background This feature was enabled as default in 2008 has been disabled by default in 2009. Somehow the freeze for Ubuntu 10.04 was exactly in between those moments. Using the Git repository of coreutils I see that the commit to revert automatic ...


7

Hard links allow ... a single executable to have more than one name. Example: ls -l /bin | grep -v ' 1 ' | sort will list the ones in /bin for you. Result ... -rwxr-xr-x 2 root root 63 2010-01-19 21:49 gunzip -rwxr-xr-x 2 root root 63 2010-01-19 21:49 uncompress -rwxr-xr-x 3 root root 26300 2011-12-12 22:40 bunzip2 -rwxr-xr-x 3 root root ...


6

No. In Linux things work differently. Each file is represented by an object called 'inode'. Every inode has a number (ID) associated with it. As we know humans are not good at remembering numbers but names. (That's how phonebooks evolved) Therefore, filename came into the picture to give each inode a human readable name. Basically, a hardlink binds a ...


5

Of course, if you create a hard link to a file, then both the original file and the hard link point to the same inode. In fact, both are equivalent - for the system there is no such thing as "the original" and "the link". They are simply two access points to the same inode. Consequently, they take up no more space together than if you had only one access ...


5

You cannot create hardlinks across mount boundaries. You'll get something like: ln: failed to create hard link ‘X’ => ‘Y’: Invalid cross-device link


5

Search for hard links @ilkkachu's and @barrycarter's answers are good. This answer is an alternative, that describes some results with more details. If the linked {match is/matches are} in the same directory tree, you will find them directly. Otherwise you can search in the whole file system from the mount point, but only within the same file system using -...


4

Every directory has a link to itself and its parent (that's why . of an empty directory will have a link count of 2). But because every directory links to its parent, any directory that has a subdirectory will have a link from that child. Thus the link count of a directory is 2 + the number of directories immediately contained by it.


4

Short answer: The number of links to a directory is at least two: The link to the parent directory The link to itself See also here: The number of links is the number of hard links to the file. For a directory, the number of hard links is the number of (immediate) subdirectories plus the parent directory and itself.


4

Here is my solution with find: find . -links +1 -type f -name '*' -printf '%i %p\n' | sort . : search in current directory, you can change it to anything else, e.g: /, ~/ravexina, etc. -links +1 : only files with more than of 1 link ( >= 2 ). -type f : only files (not directories, sym links, pipe files, etc). -name '*': all files with anything in their ...


4

Apparently this option is intentionally cut years ago. From the topic Creating hard links from bugzilla.gnome.org: Almost none of our users knows the hardlink concept, so why should we confuse him and even risk data loss? You can use nautilus-open-terminal and create hardlinks manually. The good news is you have two options to create this feature: ...


4

In theory, hard links should be indistinguishable from regular files (that's sort of the point). If "x" is a hardlink to "y", then "y" is also a hardlink to "x". That being said, the second column of ls -l tells you how many links there are to a given file. If this number is bigger than 1, the file is or has a hardlink somewhere. This may not work for ...


3

I think you are confused by the word “link” in “hardlink”/“softlink”. Despite the apparent naming symmetry, those are completely different things. Soft links: If you come from some microsoft background, maybe it would be easier to say that : a softlink is pretty close to what a shortcut is. It is an (almost) regular file that has a pathname in it. Only ...


3

No, it doesn't. When you update a file, Dropbox breaks all links. So in the example @Falk gives above, if he edited that file from a remote computer, when it synced back the link would be broken, and Dropbox would contain a new file called testfile (Falks conflicted copy). The only link which works is a soft link where the original file is the one in the ...


3

TL;DR: Making a hard link does not create a relationship between different files that causes new changes in one to be reflected in the other. Instead, it creates another name for the same file. Your link command failed because you told it to create a link called file2.txt, but file2.txt already existed. Like people and things in world, a file can have more ...


3

You could do something like this: find . -type t -ls | grep -v " 1 username" This will list files in the current directory and perform a ls on it. As @barrycarter said, hard links are indistinguishable from real files, but in this listing they will show up as having more than one link. Using grep -v you weed out the files that have only one link. (The ...


2

On Windows you can create hardlinks too if you have NTFS filesystem. fsutil hardlink create target_file source_file The files has to be on the same logical drive.


2

Based on gertvdijk's answer, I came up with the following snippet, which suits my needs: if [[ -e "/etc/debian_version" ]] && type dircolors > /dev/null 2>&1; then command dircolors|command grep -q 'hl=' && export LS_COLORS="ln=01;36:hl=00;36" command dircolors|command grep -q 'mh=' && export LS_COLORS="ln=01;36:mh=...


2

The idea you describe exists already, in the form of the 'Alias' in Apple's HFS+ filesystem. If you create an alias for a file, then you can use that alias to refer to the file in future. The alias uses a mixture of inode-equivalent (HFS+ doesn't have inodes as such), file name, and... other stuff to re-find the file, using an algorithm which is ...


2

I see the following disadvantages: with hard links there is no "original" path of the file anymore, i.e. you can't distinguish a file from its links. I often use links as shortcuts to files which are down in a well-sorted, nested directory structure, to simplify navigation, but I still want to be able to look up where exactly the file is stored (since its ...


2

Turns out this answer was mistaken, see Andrew's post for the correct answer. Yes I does. I've tested it with a standard free dropbox account with the dropbox folder in a btrfs volume. The test was: ln /media/username/volume/Documents/testfile /media/username/volume/Dropbox/testfile ls -li /media/username/volume/Documents/testfile ls -li /media/username/...


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