英文:
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 '^l' | awk '{print $9 "\t" $11}'
which is inefficient compared to:
find -type l -printf "%f\t%l\n"
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 '{}' \;
If you are not interested in the path of the symbolic link, use
find -type l -execdir ls -l --color=auto '{}' \;
答案2
得分: 1
如果你使用 GNU Findutils,则 -printf
选项的 %Y
格式可能会有用。它打印一个单字符,表示符号链接引用的文件类型(f
表示普通文件,d
表示目录,N
表示不存在,...)。
find . -type l -printf ''%Y %p\n'
将打印当前目录下所有符号链接的类型和路径。请注意,如果任何路径包含换行字符,输出将难以解析。要生成可以对所有可能路径进行明确解析的输出,请使用 NUL 字符终止每个链接的输出:
find . -type l -printf ''%Y %pfind . -type l -printf ''%Y %p\0''
''
请注意,这不会告诉你符号链接的目标是否是另一个符号链接。所有符号链接在确定目标的类型(如果有)之前都会被解引用。
英文:
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 '%Y %p\n'
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 '%Y %pfind . -type l -printf '%Y %p\0'
'
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论