This question already has an answer here:

I need a command for an if condition to know if 'file_name' is an hard link (not symlink). Thanks

marked as duplicate by JdeBP, Jeff Schaller Apr 26 at 16:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    All files are hard links to themselves. Do you want to identify files with multiple links, or do you have an idea of an "original" file and other "links"? – Michael Homer Apr 26 at 6:39
  • I think that the question is duplicated, there is an already answered question here: unix.stackexchange.com/questions/167610/… – Dasel Apr 26 at 6:40
  • [ -e file ] && [ \! -h file ] – mosvy Apr 26 at 10:01

man test

use -h to check, whether the file is symbolic link and the File exists.

   -h FILE
          FILE exists and is a symbolic link (same as -L)


bash-4.2$ ls -lrt
total 0
-rw-r--r--. 1 MYID MYID 0 Apr 26 14:37 test
lrwxrwxrwx. 1 MYID MYID 4 Apr 26 14:37 t -> test
bash-4.2$ [ -h t ] && echo "yes" || echo "no"
bash-4.2$ [ -h test ] && echo "yes" || echo "no"
  • I need [...] to know if 'file_name' is [a] hard link (not symlink). and you provide an answer to detect symlinks. I think this needs some explaining .... – Bananguin Apr 26 at 7:28
  • @Bananguin This seems reasonable to me. Every file is a hard link, but only some are also of the specific "symbolic link" filetype. Could you elaborate your criticism? – Kusalananda Apr 26 at 8:11
  • @Kusalananda The OP is looking for a test for hard links and this answer, which is barely more than RTFM, highlights a test for sym links. This is especially not a test for hard links. The OP even explicitly states (not symlink) and receives an answer pertaining to precisely symlinks. You need to throw in additional technical knowledge to explain your "seems reasonable" judgement. Notice: this demo says "no" when it detects a hard link. I am not saying the technical background is wrong. I am saying this is a bad answer. – Bananguin Apr 26 at 9:23
  • @Bananguin Both Kamaraj and I interpreted the question as asking for a way to detect whether a file was a hard link and not a symbolic link (see title). With that interpretation, the answer seems reasonable. If the question was to "detect whether a file was a hard link or not (not whether it was a symbolic link or not)", I can definitely see that your argument is totally valid. The question would then also need additional clarification as to what they meant by that. – Kusalananda Apr 26 at 9:36
  • @Kusalananda [ -h /@/not-there ] && echo "yes" || echo "no" => no. So I guess that /@/not-there is only a hard link? – mosvy Apr 26 at 9:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.