Linux 如何查找不包括符号链接的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16303449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find files excluding symbolic links?
提问by Barth
I want to find files in Linux that follow a certain pattern but I am not interested in symbolic links.
我想在 Linux 中找到遵循某种模式的文件,但我对符号链接不感兴趣。
There doesn't seem to be an option to the find
command for that.
该find
命令似乎没有选项。
How shall I do ?
我该怎么办?
采纳答案by hek2mgl
回答by ron rothman
Do you want it to follow symlinks but not return them (if they match your pattern)?
您是否希望它遵循符号链接但不返回它们(如果它们与您的模式匹配)?
find -H
?
find -H
?
man find
...
-H Cause the file information and file type (see stat(2)) returned for each symbolic link specified on the command line to be those of
the file referenced by the link, not the link itself. If the referenced file does not exist, the file information and type will be
for the link itself. File information of all symbolic links not on the command line is that of the link itself.
-L Cause the file information and file type (see stat(2)) returned for each symbolic link to be those of the file referenced by the
link, not the link itself. If the referenced file does not exist, the file information and type will be for the link itself.
This option is equivalent to the deprecated -follow primary.
回答by Vittorio Milazzo
! -type l
For example, if you want to search all regular files in /usr/bin, excluding symlink:
例如,如果要搜索 /usr/bin 中的所有常规文件,不包括符号链接:
find /usr/bin/ \! -type l
回答by Nande
I have readed the MAN and now it seems is -P also, using -type r would raise an error. also notice is the DEFAULT behavior now.
我已经阅读了 MAN,现在似乎也是 -P,使用 -type r 会引发错误。还要注意的是现在的默认行为。
-P Never follow symbolic links. This is the defaultbehaviour. When find examines or prints information a file, and the file is a symbolic link, the information used shall be taken from the properties of the symbolic link itself.
-P 从不遵循符号链接。这是默认行为。当 find 检查或打印文件信息,并且该文件是一个符号链接时,所使用的信息应取自符号链接本身的属性。
回答by Joyce M
This works for me:
这对我有用:
find -H . -maxdepth 1 -type f
Actually, don't really need the -H
实际上,真的不需要 -H
回答by Wellington1993
Like @AquariusPower say, the use of find -type f -xtype f
solved my problem, and now I get only real files and not symbolic links anymore.
就像@AquariusPower 说的那样,使用 find -type f -xtype f
解决了我的问题,现在我只得到真实的文件,不再得到符号链接。
From: https://linux.die.net/man/1/find
来自:https: //linux.die.net/man/1/find
I got:
我有:
-xtype c The same as -type unless the file is a symbolic link. For symbolic links: if the -H or -P option was specified, true if the file is a link to a file of type c; if the -L option has been given, true if c is 'l'. In other words, for symbolic links, -xtype checks the type of the file that -type does not check.
-xtype c 与 -type 相同,除非文件是符号链接。对于符号链接:如果指定了 -H 或 -P 选项,则如果文件是指向 c 类型文件的链接,则为 true;如果已给出 -L 选项,则如果 c 为 'l',则为 true。换句话说,对于符号链接, -xtype 会检查 -type 不检查的文件类型。
Thanks.
谢谢。