简体   繁体   English

将符号链接和他的目标与bash test -ef进行比较

[英]Compare a symlink and his target with bash test -ef

TL;DR: why [ symlink_to_file_a -ef file_a ] return true ? TL; DR:为什么[ symlink_to_file_a -ef file_a ]返回true?


I need to test if a file b (mostly symlink) is the same than a file a for trying to know if a and b are hard linked together, and hard link them if itsn't the case. 我需要测试文件b (主要是符号链接)是否与文件a相同,以试图了解ab是否硬链接在一起,如果不是,则硬链接它们。
I use conditional expression -ef and bash manual says : 我使用条件表达式-efbash手册说:

file1 -ef file2 file1 -ef file2
True if file1 and file2 refer to the same device and inode numbers. 如果file1和file2引用相同的设备和inode编号,则为true。

I compare just symlink/regular file and inodes of a and b are different but the result is True . 我只比较了symlink / regular文件, ab的 inode不同,但结果为True

An answer of a similar question says : 一个类似问题的答案是:

If yes, then will the inode number be same for target and links? 如果是,那么目标和链接的索引节点号是否相同?

No. Usually, the symlink is a file with its own inode, (with file-type, own data blocks, etc.) 不会。通常,符号链接是一个具有自己的inode的文件(具有文件类型,自己的数据块等)。

I'm not sure of what I understood but I may found some explanations about it in ext4 spec : 我不确定我的理解,但是我可以在ext4规范中找到一些解释:

The target of a symbolic link will be stored in this field if the target string is less than 60 bytes long. 如果目标字符串的长度小于60个字节,则符号链接的目标将存储在此字段中。 Otherwise, either extents or block maps will be used to allocate data blocks to store the link target. 否则,将使用扩展数据块或块映射来分配数据块以存储链接目标。

I tried with target shorter/longer than 60B but there is no difference. 我尝试使用比60B短/长的目标,但没有区别。

$ cat test.sh
#!/usr/bin/env bash
foo="foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
mkdir /tmp/test
cd /tmp/test
touch a
ln -s /tmp/test/a b
ls -li
if [ a -ef b ]
then
  echo b : same device or inode of a
 else
  echo b : different device or inode of a
fi
mkdir /tmp/test/${foo}
cd /tmp/test/${foo}
touch c
ln -s /tmp/test/${foo}/c d
ls -li
if [ c -ef d ]
then
  echo d : same device or inode of c
else
  echo d : different device or inode of c
fi
$ ./test.sh
156490 -rw-rw-r-- 1 msi msi  0 nov.  25 23:55 a                                                                                                                                                                                                
156491 lrwxrwxrwx 1 msi msi 11 nov.  25 23:55 b -> /tmp/test/a                                                                                                                                                                                 
b : same device or inode of a                                                                                                                                                                                                                  
total 4                                                                                                                                                                                                                                        
156494 -rw-rw-r-- 1 msi msi   0 nov.  25 23:55 c
156495 lrwxrwxrwx 1 msi msi 155 nov.  25 23:55 d -> /tmp/test/foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo/c
d : same device or inode of c

Inodes are different but the test succeed and I didn't see what is wrong. 索引节点不同,但测试成功,但我没有发现什么地方出了问题。

Seems like documentation is misleading in some places. 似乎文档在某些地方具有误导性。 From http://www.tldp.org/LDP/abs/html/fto.html : http://www.tldp.org/LDP/abs/html/fto.html

f1 -ef f2 f1 -ef f2

files f1 and f2 are hard links to the same file 文件f1f2是指向同一文件的硬链接

As you already found out yourself, that expression returns true not only for hard links but also for soft links to the same file. 正如您已经发现的那样,该表达式不仅对于硬链接而且对于指向同一文件的链接都返回true。

Use stat instead: 使用stat代替:

if [ "$(stat -c "%d:%i" FILE1)" == "$(stat -c "%d:%i" FILE2)" ]

Source: https://superuser.com/questions/196572/check-if-two-paths-are-pointing-to-the-same-file 资料来源: https : //superuser.com/questions/196572/check-if-two-paths-are-pointing-to-the-same-file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM