[英]Following symlinks with find to return the file the symlink is pointing to, not the symlink itself
I use usually use tmutil to control Timemachine instead of the GUI, which has some deficiencies to make it appealable to the masses. 我通常使用tmutil来控制Timemachine而不是GUI,GUI有一些不足之处使其无法吸引大众。 I tend to have a lot drives attached to my iMac, at this moment I have 11 with twenty total mounted partitions, but the drives and paritions change as I work on/with them. 我的iMac上往往连接了很多驱动器,此刻我有11个驱动器,总共安装了20个分区,但是当我使用它们时,驱动器和分区会发生变化。 Sometimes many at a time. 有时一次很多。
I wrote a one-liner with the goal to report on the exclusion status of all drives in the /Volumes/ directory and an alias to it for simple reuse. 我写了一个单行代码,目的是报告/ Volumes /目录中所有驱动器的排除状态,并为它提供一个别名,以方便重复使用。 alias tmutilvol="find /Volumes/ -maxdepth 1 -exec tmutil isexcluded {} \\;"
It works, well mostly. 它可以正常工作。 The typical output looks this is, 典型的输出看起来是
[Included] /Volumes
[Excluded] /Volumes/Backup-Lion
[Excluded] /Volumes/Backups
[Excluded] /Volumes/chris
[Excluded] /Volumes/El Capitan Temp Holding
[Excluded] /Volumes/Font
[Excluded] /Volumes/Hades
[Excluded] /Volumes/iMac Bad
[Excluded] /Volumes/iMac Clone
[Excluded] /Volumes/iMac Clone New
[Excluded] /Volumes/iMac ElCap
[Excluded] /Volumes/iMac HD
[Excluded] /Volumes/iMac HD New
[Excluded] /Volumes/iMac HS Clone
[Excluded] /Volumes/Macintosh HD Clone 1
[Excluded] /Volumes/maclaptop
[Excluded] /Volumes/Old Laptop
[Excluded] /Volumes/Old-Timemachine-Garnet
[Excluded] /Volumes/Old-Timemachine-Lion
[Excluded] /Volumes/TEMPORARY
[Excluded] /Volumes/Time Machine Mirror
The problem is /Volumes/iMac HD
. 问题是/Volumes/iMac HD
。 This is not the actual drive, it is a symlink to it. 这不是实际的驱动器,而是与其的符号链接。
drwxr-xr-x 33 chris staff 1190 Jan 17 12:31 Backup-Lion
drwxrwxr-x 20 chris staff 748 Nov 11 22:03 Backups
drwxrwxr-x 107 chris staff 3706 Nov 13 16:26 El Capitan Temp Holding
drwxrwxr-x 82 chris staff 2856 Jan 19 16:07 Font
drwxr-xr-x 260 chris staff 8908 Jan 22 11:51 Hades
drwxr-xr-x@ 41 chris staff 1462 Nov 11 22:03 Macintosh HD Clone 1
drwxr-xr-x 24 chris staff 884 Nov 11 22:03 Old Laptop
drwxr-xr-x 6 chris staff 272 Jan 17 12:31 Old-Timemachine-Garnet
drwxrwxr-x@ 20 chris staff 748 Jan 19 14:49 Old-Timemachine-Lion
drwxr-xr-x 20 chris staff 748 Jan 22 11:54 TEMPORARY
drwxr-xr-x 12 chris staff 476 Jan 23 01:30 Time Machine Mirror
drwx------@ 6 chris staff 272 Jan 16 16:18 chris
drwxrwxrwx 36 chris staff 1292 Jan 19 00:15 iMac Bad
drwxrwxrwx 29 chris staff 1054 Nov 13 16:26 iMac Clone
drwxr-xr-x 6 chris staff 272 Jan 17 16:05 iMac Clone New
drwxr-xr-x 30 chris staff 1088 Jan 16 17:31 iMac ElCap
lrwxr-xr-x 1 root wheel 1 Jan 16 20:34 iMac HD -> /
drwxr-xr-x 6 chris staff 272 Jan 17 16:05 iMac HD New
drwxrwxr-x 26 root wheel 952 Nov 13 16:26 iMac HS Clone
drwxrwxrwx 40 chris staff 1428 Jan 19 15:40 maclaptop
The result of tmutil isexcluded
is actually for the symlink and not "/" tmutil isexcluded
的结果实际上是针对符号链接,而不是“ /”
Kaze:~ chris$ tmutil isexcluded /
[Included] /
I want to know if there is a way to get find to follow and pass in the destination of the symlink. 我想知道是否有一种方法可以找到跟随并传递符号链接的目标的方法。 I read the find man page and about the -L option and thought that would do it and it does not. 我阅读了find手册页以及有关-L选项的内容,并认为这样做会实现,但不会。 Some digging on stackexchange https://unix.stackexchange.com/questions/31114/find-usage-with-l explained why it doesn't work. 一些对stackexchange的挖掘https://unix.stackexchange.com/questions/31114/find-usage-with-l解释了为什么它不起作用。 What I have not been able to find is any clue as to how I might do what want and keep a simple one-liner or if I am going to have to write a function for this and deal with symlinks in some convoluted way. 我一直无法找到关于如何执行所需操作并保持简单单线的任何线索,或者是否需要为此编写函数并以某种复杂的方式处理符号链接。
I am hoping that someone with a lot more knowledge of CLI and bash than I might know how to do what I want simply. 我希望有人对CLI和bash的了解比我可能会做的简单得多。
Have you tried? 你有没有尝试过?
readlink "/Volumes/iMac HD"
one liner for mac: 适用于mac的一种衬垫:
find /Volumes -maxdepth 1 -exec bash -c 'N="{}"; L=`readlink "{}"`; tmutil isexcluded "${L:-$N}"' \;
Explanation: 说明:
have to use two variables, $L
for readlink output, $N
for original name. 必须使用两个变量, $L
表示readlink输出, $N
表示原始名称。
${L:-$N}
basically just output $L
if its not empty, else output $N
${L:-$N}
如果不为空,则基本上只输出$L
,否则输出$N
Note that I have to prefix $L with " /Volumes/
" as MacOS's readlink does not output the absolute path. 请注意,我必须在$ L前面加上“ /Volumes/
”,因为MacOS的readlink不会输出绝对路径。 Linux " readlink -f
" solves this issue. Linux“ readlink -f
”解决了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.