[wpseo_breadcrumb]

Determining if a file is a hard link or symbolic link?

Solutons:


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 a “file”).

Thankfully, the stat command, where available, can tell you how many names an inode has.

So you’re looking for something like this (here assuming the GNU or busybox implementation of stat):

if [ "$(stat -c %h -- "$file")" -gt 1 ]; then
    echo "File has more than one name."
fi

The -c '%h' bit tells stat to just output the number of hardlinks to the inode, i.e., the number of names the file has. -gt 1 then checks if that is more than 1.

Note that symlinks, just like any other files, can also be linked to several directories so you can have several hardlinks to one symlink.

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 19:56 s1 -> f1
10802346 lrwxrwxrwx 1 stephane stephane 2 Nov 12 19:56 s2 -> f2
10802347 lrwxrwxrwx 1 stephane stephane 8 Nov 12 19:56 s3 -> ./././f3
10802384 lrwxrwxrwx 2 stephane stephane 2 Nov 12 19:56 s4 -> s3
10802384 lrwxrwxrwx 2 stephane stephane 2 Nov 12 19:56 s5 -> s3

The f1, f2 and f3 directory entries are the same file (same inode: 10802124, you’ll notice the number of links is 3). They are hard links to the same regular file.

s4 and s5 are also the same file (10802384). They are of type symlink, not regular. They point to a path, here s3. Because s4 and s5 are entries of the same directory, that relative path s3 point to the same file (the one with inod 10802347) for both.

If you do a ls -Ll, that is asking to get file information after resolving symlinks:

$ ls -lLi
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
10802124 -rw-r--r-- 3 stephane stephane 0 Nov 12 19:55 s1
10802124 -rw-r--r-- 3 stephane stephane 0 Nov 12 19:55 s2
10802124 -rw-r--r-- 3 stephane stephane 0 Nov 12 19:55 s3
10802124 -rw-r--r-- 3 stephane stephane 0 Nov 12 19:55 s4
10802124 -rw-r--r-- 3 stephane stephane 0 Nov 12 19:55 s5

You’ll find they all resolve to the same file (10802124).

You can check if a file is a symlink with [ -L file ]. Similarly, you can test if a file is a regular file with [ -f file ], but in that case, the check is done after resolving symlinks.

hardlinks are not a type of file, they are just different names for a file (of any type).

Using the -h and -L operators of the test command:

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

Related Solutions

What is D-Bus practically useful for?

dbus does exactly what you said: it allows two-way communication between applications. For your specific example you mentioned terminator. From terminator's man page, we see: --new-tab If this is specified and Terminator is already running, DBus will be used to...

How to check ‘mdadm’ RAIDs while running?

The point of RAID with redundancy is that it will keep going as long as it can, but obviously it will detect errors that put it into a degraded mode, such as a failing disk. You can show the current status of an array with mdadm --detail (abbreviated as mdadm...

What is a “toast notification”?

A Toast is a non modal, unobtrusive window element used to display brief, auto-expiring windows of information to a user. Android OS makes relatively heavy use of them. Here's an example of a Google Chrome toast notification on Mac OS X: A list of descriptions...

Which elliptic curve should I use?

You are misreading Bernstein and Lange's advice (admittedly, their presentation is a bit misleading, with the scary red "False" tags). What they mean is not that some curves are inherently unsafe, but that safe implementation of some curves is easier than for...

How can I find files that are bigger/smaller than x bytes?

Use: find . -type f -size +4096c to find files bigger than 4096 bytes. And : find . -type f -size -4096c to find files smaller than 4096 bytes. Notice the + and - difference after the size switch. The -size switch explained: -size n[cwbkMG] File uses n units of...

Relative imports in Python 3

Explanation From PEP 328 Relative imports use a module's __name__ attribute to determine that module's position in the package hierarchy. If the module's name does not contain any package information (e.g. it is set to '__main__') then relative imports are...

How to add a class to a given element?

If you're only targeting modern browsers: Use element.classList.add to add a class: element.classList.add("my-class"); And element.classList.remove to remove a class: element.classList.remove("my-class"); If you need to support Internet Explorer 9 or lower: Add...

less searches are always case-insensitive

I'm not sure how to enable this from the command line but when you're inside of less you can toggle the behavior you want by giving the -i command to less. toggling -i                searching for /blah and /BLAH               searching for /Blah       ...

Is using nested try-catch blocks an anti-pattern?

This is sometimes unavoidable, especially if your recovery code might throw an exception. Not pretty, but sometimes there are no alternatives. I don't think its an antipattern, just widely misused. Most nested try catch's are indeed avoidable and ugly as hell,...

Create a branch in Git from another branch

If you like the method in the link you've posted, have a look at Git Flow. It's a set of scripts he created for that workflow. But to answer your question: git checkout -b myFeature dev Creates the MyFeature branch off dev. Do your work and then git commit -am...

How can I set customise settings for htop?

htop has a setup screen, accessed via F2, that allows you to customize the top part of the display, including adding or removing a "Load average" field and setting it's style (text, bar, etc.). These seem to be auto saved in $HOME/.config/htop/htoprc, which...

Is there any way to manually bring up the keyboard?

As I see an alternative keyboard may solve your issue, and this seems to be an acceptable solution, and you even mention something you cannot find -- hereby I proudly present: Hacker's Keyboard Checking its Guide, there's in fact a section suggesting such a...

How to get rid of “No match found” when running “rm *”

This behaviour is controlled by several of Zsh's globbing options. By default, if a command line contains a globbing expression which doesn't match anything, Zsh will print the error message you're seeing, and not run the command at all. You can disable this in...

How to append date to backup filename

This isn't working because the command date returns a string with spaces in it. $ date Wed Oct 16 19:20:51 EDT 2013 If you truly want filenames like that you'll need to wrap that string in quotes. $ touch "foo.backup.$(date)" $ ll foo* -rw-rw-r-- 1 saml saml 0...

What does __all__ mean in Python?

Linked to, but not explicitly mentioned here, is exactly when __all__ is used. It is a list of strings defining what symbols in a module will be exported when from <module> import * is used on the module. For example, the following code in a foo.py...