在我的 ~ 目录中,我有一些指向“/Volumes/Macintosh HD 2/...”的符号链接,我想以递归方式找到它们。查看“find”的手册页,其中说“-lname”参数将搜索符号链接内容。它似乎有效,但不是递归的:
$ pwd
/Users/myusername
$ sudo find . -lname '/Volumes*'
$ cd Documents/
$ sudo find . -lname '/Volumes*'
./Documents on Win7
./work.rtf
发生了什么事?我怎样才能让它以递归方式工作?--“find”程序应该始终以递归方式工作。
我检查了权限,它们看起来没问题,但是正如你所看到的,我使用了“sudo”以确保...没有骰子。
$ ls -ld Documents/
drwx------+ 14 myusername staff 476 Jan 12 16:32 Documents/
答案1
这看起来像是谓词测试方式的一个错误-lname
,而且它比不递归更奇怪。从我的测试来看,它似乎只对与当前工作目录处于相同目录深度的文件起作用(即,如果您在 ~/Documents 中并运行find .. -lname '/Volumes*'
,它会在 ~/Documents 和 ~/Desktop 等中找到链接,但不会找到 ~ 中的任何链接)。但有一个解决方法(至少根据我的测试):如果起始目录是绝对路径,它似乎可以工作,因此使用find "$PWD" -lname '/Volumes*'
而不是find . -lname '/Volumes*'
似乎可以避免该问题。以下是一个测试序列来说明这一点:
Mac:~ test$ mkdir -p sub1/subsub1 # Create some folders and links to work with
Mac:~ test$ mkdir -p sub2/subsub2
Mac:~ test$ ln -s sometarget toplink
Mac:~ test$ ln -s sometarget sub1/sub1link
Mac:~ test$ ln -s sometarget sub1/subsub1/subsub1link
Mac:~ test$ ln -s sometarget sub2/sub2link
Mac:~ test$ ln -s sometarget sub2/subsub2/subsub2link
Mac:~ test$ find . -lname "*" # This only finds links at the top of the home directory
./toplink
Mac:~ test$ cd sub1
Mac:sub1 test$ find . -lname "*"
./sub1link
Mac:sub1 test$ find .. -lname "*" # This only finds links one level down
../Pictures/iChat Icons
../sub1/sub1link
../sub2/sub2link
Mac:sub1 test$ cd subsub1
Mac:subsub1 test$ find ../.. -lname "*" # This only finds links two levels down
../../sub1/subsub1/subsub1link
../../sub2/subsub2/subsub2link
Mac:subsub1 test$ find ../.. -lname "*" -o -name "*sub2*" # The -name predicate works fine
../../sub1/subsub1/subsub1link
../../sub2
../../sub2/sub2link
../../sub2/subsub2
../../sub2/subsub2/subsub2link
Mac:subsub1 test$ find /Users/test -lname "*" # It works as expected with an absolute starting path
/Users/test/Library/Application Support/SyncServices/Local/clientdata/com.apple.AddressBook
/Users/test/Library/Application Support/SyncServices/Local/clientdata/com.apple.Dashboard
/Users/test/Library/Application Support/SyncServices/Local/clientdata/com.apple.Dock
/Users/test/Library/Application Support/SyncServices/Local/clientdata/com.apple.iCal
/Users/test/Library/Application Support/SyncServices/Local/clientdata/com.apple.Keychain
/Users/test/Library/Application Support/SyncServices/Local/clientdata/com.apple.Mail
/Users/test/Library/Application Support/SyncServices/Local/clientdata/com.apple.PreferenceSync
/Users/test/Library/Application Support/SyncServices/Local/clientdata/com.apple.Safari
/Users/test/Pictures/iChat Icons
/Users/test/sub1/sub1link
/Users/test/sub1/subsub1/subsub1link
/Users/test/sub2/sub2link
/Users/test/sub2/subsub2/subsub2link
/Users/test/toplink
我会将其提交给 Apple 的 bugreporter。