-1

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

Edward
  • 19
  • 3
  • 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 '19 at 06:39
  • I think that the question is duplicated, there is an already answered question here: https://unix.stackexchange.com/questions/167610/determining-if-a-file-is-a-hard-link-or-symbolic-link – Dasel Apr 26 '19 at 06:40
  • `[ -e file ] && [ \! -h file ]` –  Apr 26 '19 at 10:01

1 Answers1

0

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"
yes
bash-4.2$ [ -h test ] && echo "yes" || echo "no"
no
Kamaraj
  • 4,295
  • 1
  • 12
  • 18
  • `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 '19 at 07: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 '19 at 08: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 '19 at 09: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 '19 at 09:36
  • @Kusalananda `[ -h /@/not-there ] && echo "yes" || echo "no"` => `no`. So I guess that `/@/not-there` is *only* a hard link? –  Apr 26 '19 at 09:55
  • @mosvy I was carefully using the word "reasonable". I did not use the word "correct". – Kusalananda Apr 26 '19 at 10:00
  • @Kusalananda I concur, that the OP is looking for `a way to detect whether a file was a hard link`. This answer provides a way to detect SYMlinks and nothing else. The example at the bottom literally say `yes` in case of a symlink and `no` otherwise. Otherwise is not equivalent to "this is a hard link". The question for something about hardlinks gets an answer which is strictly about symlinks. The answer is strictly about symlinks while the OP asks for hardlinks. There is a difference there. That is why I think an explanation is needed. The answer is not off topic, but bad without explanation. – Bananguin Apr 30 '19 at 12:08