我可以显示链接指向的目标文件ls -l
:
snowch$ ls -l /usr/local/bin/mvn
lrwxr-xr-x 1 snowch admin 29 12 Dec 08:58 /usr/local/bin/mvn -> ../Cellar/maven/3.2.3/bin/mvn
Run Code Online (Sandbox Code Playgroud)
有没有办法显示更少的输出而不必通过另一个命令(例如 awk)进行管道传输?例如:
snowch$ ls ?? /usr/local/bin/mvn
/usr/local/bin/mvn -> ../Cellar/maven/3.2.3/bin/mvn
Run Code Online (Sandbox Code Playgroud)
我在 OS X 10.9.5 上运行 3.2.53。几个命令的输出如下所示:
snowch$ ls -H /usr/local/bin/mvn
/usr/local/bin/mvn
snowch$ ls -L /usr/local/bin/mvn
/usr/local/bin/mvn
snowch$ file /usr/local/bin/mvn
/usr/local/bin/mvn: POSIX shell script text executable
snowch$ file -b /usr/local/bin/mvn
POSIX shell script text executable
Run Code Online (Sandbox Code Playgroud)
ls
不幸的是,没有选项可以检索文件属性并以任意方式显示它们。一些系统为此有单独的命令(例如 GNU 有一个stat
命令或 GNU 中的功能find
)。
在大多数现代系统上,对于大多数文件,这应该可以工作:
$ ln -s '/foo/bar -> baz' the-file
$ LC_ALL=C ls -ldn the-file | sed '
1s/^\([^[:blank:]]\{1,\}[[:blank:]]\{1,\}\)\{8\}//'
the-file -> /foo/bar -> baz
Run Code Online (Sandbox Code Playgroud)
这是通过删除输出的第一行的前 8 个空白分隔字段来实现的ls -l
。除了在那里没有显示 gid 或当有大量链接时前 2 个字段连接在一起的系统外,这应该有效。
使用 GNU stat
:
$ LC_ALL=C stat -c '%N' the-file
'the-file' -> '/foo/bar -> baz'
Run Code Online (Sandbox Code Playgroud)
使用 GNU find
:
$ find the-file -prune \( -type l -printf '%p -> %l\n' -o -printf '%p\n' \)
the-file -> /foo/bar -> baz
Run Code Online (Sandbox Code Playgroud)
使用 FreeBSD/OS/X 统计:
f=the-file
if [ -L "$f" ]; then
stat -f "%N -> %Y" -- "$f"
else
printf '%s\n' "$f"
fi
Run Code Online (Sandbox Code Playgroud)
与zsh
统计:
zmodload zsh/stat
f=the-file
zstat -LH s -- "$f"
printf '%s\n' ${s[link]:-$f}
Run Code Online (Sandbox Code Playgroud)
很多系统也有readlink
专门获取链接目标的命令:
f=the-file
if [ -L "$f" ]; then
printf '%s -> ' "$f"
readlink -- "$f"
else
printf '%s\n' "$f"
fi
Run Code Online (Sandbox Code Playgroud)
使用file
命令。
[sreeraj@server ~]$ ls -l mytest
lrwxrwxrwx 1 sreeraj sreeraj 15 Dec 12 09:31 mytest -> /usr/sbin/httpd
[sreeraj@server ~]$ file mytest
mytest: symbolic link to `/usr/sbin/httpd'
Run Code Online (Sandbox Code Playgroud)
或者
[sreeraj@server ~]$ file -b mytest
symbolic link to `/usr/sbin/httpd'
[sreeraj@server ~]$
Run Code Online (Sandbox Code Playgroud)
另外,请经过的男子页阅读ls
并检查选项-L
,并-H
看看是否这就够了您的要求。
归档时间: |
|
查看次数: |
11275 次 |
最近记录: |