查询符号链接的状态

huangapple go评论58阅读模式
英文:

Querying status of symbolic links

问题

查询符号链接指向何处(目录、普通文件、不存在的文件等)的命令是:

```bash
ls -aRl | grep '^l' | awk '{print $9 "\t" $11}'

与下面的命令相比,这个命令效率较低:

find -type l -printf "%f\t%l\n"

但第一个命令根据链接类型给我提供了颜色。第二个命令则没有。

是否有一个用于给路径着色或查询符号链接状态的更好方法?

我查看了man dircolors,但似乎它只提供了ls的定义。


<details>
<summary>英文:</summary>

Say I want to query what symlinks pointing to where (directory, regular file, non-existent file, etc):
```bash
ls -aRl | grep &#39;^l&#39; | awk &#39;{print $9 &quot;\t&quot; $11}&#39;

which is inefficient compared to:

find -type l -printf &quot;%f\t%l\n&quot;

But first command gives me colors depending on the link type. The second doesn't.

Is there a program for colorizing paths or a better way to query the status of symbolic links?

I've taken a look on man dircolors but it seems to only provide definitions for ls.

答案1

得分: 1

如果 ls 正在执行您想要的操作,只需使用 ls 来显示它:

find -type l -exec ls -l --color=auto '{}' \;

如果您对符号链接的路径不感兴趣,可以使用:

find -type l -execdir ls -l --color=auto '{}' \;
英文:

If ls is doing what you want, just use ls for displaying it:

find -type l -exec ls -l --color=auto &#39;{}&#39; \;

If you are not interested in the path of the symbolic link, use

find -type l -execdir ls -l --color=auto &#39;{}&#39; \;

答案2

得分: 1

如果你使用 GNU Findutils,则 -printf 选项的 %Y 格式可能会有用。它打印一个单字符,表示符号链接引用的文件类型(f 表示普通文件,d 表示目录,N 表示不存在,...)。

find . -type l -printf '&#39;%Y %p\n&#39; 

将打印当前目录下所有符号链接的类型和路径。请注意,如果任何路径包含换行字符,输出将难以解析。要生成可以对所有可能路径进行明确解析的输出,请使用 NUL 字符终止每个链接的输出:

find . -type l -printf '&#39;%Y %p
find . -type l -printf '&#39;%Y %p\0&#39;' 
&#39;'

请注意,这不会告诉你符号链接的目标是否是另一个符号链接。所有符号链接在确定目标的类型(如果有)之前都会被解引用。

英文:

If you are using GNU Findutils, the %Y format for the -printf option of find may be of use. It prints a single character that gives the type of the file referenced by a symlink (f for regular file, d for directory, N for non-existent, ...).

find . -type l -printf &#39;%Y %p\n&#39;

will print the types and paths of all symlinks under the current directory. Note that the output will be difficult to parse if any of the paths contain newline characters. To produce output that can be parsed unambiguously for all possible paths, use the NUL character to terminate the output for each link:

find . -type l -printf &#39;%Y %p
find . -type l -printf &#39;%Y %p\0&#39;
&#39;

Be aware that this will not tell you if the target of a symlink is another symlink. All symlinks are dereferenced before determining the type (if any) of the target.

huangapple
  • 本文由 发表于 2023年4月4日 04:41:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75923615.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定