假设我有一个目录有一个名为file1的文件和5个符号链接1 2 3 4 5,每个文件指向file1,如下所示:
@H_404_2@-rw-r--r-- file1 lrwxrwxrwx 1 -> file1 lrwxrwxrwx 2 -> file1 lrwxrwxrwx 3 -> file1 lrwxrwxrwx 4 -> file1 lrwxrwxrwx 5 -> file1 drwxr-xr-x ../ drwxr-xr-x ./如果我使用-type f过滤器在此目录下运行,则会按预期的方式输出:
@H_404_2@% find . -type f ./file1但是当我使用-f运算符运行一个perl脚本时,它会提供以下输出:
@H_404_2@% ls | perl -e 'while(<>) { chomp; print "$_\n" if -f $_ }' 1 2 3 4 5 file1当我也添加了-l的测试,它的工作原理:
@H_404_2@% ls | perl -e 'while(<>) { chomp; print "$_\n" if -f $_ and not -l $_}' file1解决方法
符号链接并找到
默认情况下,GNU找不到引用或遵循符号链接,但find
documentation描述了更改此策略的交换机。
The options controlling the behavIoUr of find with respect to links are as follows :-
-P
find
does not dereference symbolic links at all. This is the default behavIoUr. This option must be specified before any of the file names on the command line.
-H
find
does not dereference symbolic links (except in the case of file names on the command line,which are dereferenced). If a symbolic link cannot be dereferenced,the information for the symbolic link itself is used. This option must be specified before any of the file names on the command line.
-L
find
dereferences symbolic links where possible,and where this is not possible it uses the properties of the symbolic link itself. This option must be specified before any of the file names on the command line. Use of this option also implies the same behavIoUr as the-noleaf
option. If you later use the-H
or-P
options,this does not turn off-noleaf
.
-follow
This option forms part of the “expression” and must be specified after the file names,but it is otherwise equivalent to-L
. The-follow
option affects only those tests which appear after it on the command line. This option is deprecated. Where possible,you should use-L
instead.
将查找命令转换为Perl
标准配置附带一个兼容于旧版Unix系统的find2perl
实用程序。
在代码find2perl生成后,默认想要的子传递给find
from the File::Find module就是
但随着 – 随后,我们得到
@H_404_2@sub wanted { my ($dev,$gid) = stat($_)) && -f _ && print("$name\n"); }请注意,唯一的区别是是否需要通话stat
或lstat
,后者被记录为
lstat EXPR
lstat
Does the same thing as the 07003 function (including setting the special
_
filehandle) but stats a symbolic link instead of the file the symbolic link points to. If symbolic links are unimplemented on your system,a normal 07003 is done. For much more detailed information,please see the documentation for 07003.If EXPR is omitted,stats
$_
.
当find2perl的示例输出显示时,您可以使用filetest运算符表达您的意图,但要准确地说明您选择stat对lstat的符号链接的语义。
有趣的_令牌
上述快速解决方案的末尾是lstat
documentation提到的特殊文件句柄。它持有stat或lstat的最新结果的副本,以避免不必重复进行昂贵的系统调用。 -00,如-f,-r,-e和-l也填写这个缓冲区:
If any of the file tests (or either the
stat
orlstat
operator) is given the special filehandle consisting of a solitary underline,then the stat structure of the prevIoUs file test (orstat
operator) is used,saving a system call. (This doesn’t work with-t
,and you need to remember thatlstat
and-l
leave values in the stat structure for the symbolic link,not the real file.) (Also,if the stat buffer was filled by anlstat
call,-T
and-B
will reset it with the results ofstat _
). Example:06005