列出当前目录中的符号链接?

cwd*_*cwd 9 ls find symlink

这个问题讨论在当前目录中查找目录。解决方法基本上是:

ls -d */
Run Code Online (Sandbox Code Playgroud)

这很好,但我如何轻松列出符号链接?我是否必须使用类似的东西

find . -xtype l -d 1
(intended to find symlinks max depth 1 - doesn't work)
Run Code Online (Sandbox Code Playgroud)

或者有更简单的方法吗?ls 可以用于此吗?

Gil*_*il' 10

在 zsh 中(N在括号内添加以包含名称以 a 开头的符号链接.):

echo *(@)
Run Code Online (Sandbox Code Playgroud)

大多数find实现:

find -maxdepth 1 -type l
Run Code Online (Sandbox Code Playgroud)

符合 POSIX:

find . -type d \! -name . -prune -o -type l -print
Run Code Online (Sandbox Code Playgroud)

或者使用 shell 循环:

for x in * .*; do
  if [ -h "$x" ]; then echo "$x"; done
done
Run Code Online (Sandbox Code Playgroud)


fro*_*r78 7

这不是在 Mac 上,但是

find . -maxdepth 1 -type l
Run Code Online (Sandbox Code Playgroud)

为我工作。


Arc*_*ege 3

你应该使用-type而不是-xtype

\n\n
   -xtype c\n          The same as -type unless the file is a symbolic link.  For  sym\xe2\x80\x90\n          bolic  links:  if the -H or -P option was specified, true if the\n          file is a link to a file of type c; if the -L  option  has  been\n          given,  true  if  c is `l'.  In other words, for symbolic links,\n          -xtype checks the type of the file that -type does not check.\n
Run Code Online (Sandbox Code Playgroud)\n\n

默认值为-P,因此 -xtype 选项将尝试确定结果文件,而不是符号链接本身。事实上,我得到了一些积极的结果,这看起来像是一个错误。-P -xtype l当且仅当结果本身就是符号链接时,应该返回 true(在符号链接上)。

\n\n

还可以使用:ls -FA | sed -ne 's/@//p'它将仅显示符号链接。

\n

  • `sed -ne 's/@//p'` (甚至 `sed -ne 's/@$//p'`)不是一个安全的测试,因为第一个版本在 `@` 时会给出误报出现在 *ls* 输出中的任何位置,当文件名实际上以“@”结尾时,第二个将返回假阳性 (2认同)