如何在仍然遵循目录符号链接的同时使用长格式(-l)?

Rom*_*nSt 36 ls symlink

我注意到这ls -l不仅改变了输出的格式,而且还改变了目录符号链接的处理方式:

> ls /rmn
biweekly.sh  daily.sh  logs ...

> ls -l /rmn
lrwxrwxrwx 1 root root 18 Feb 11  2011 /rmn -> /root/maintenance/
Run Code Online (Sandbox Code Playgroud)

我想获得 中内容的详细列表/rmn,而不是有关/rmn符号链接的信息。

我能想到的一种解决方法是创建一个执行以下操作的 shell 函数:

cd /rmn
ls -l
cd -
Run Code Online (Sandbox Code Playgroud)

但这似乎太老套了,尤其是因为它会弄乱cd -. 有没有更好的办法?

(我使用的是 CentOS 2.6.9)

pjc*_*c50 41

看看你的 ls 是否有选项:

 -H, --dereference-command-line
     follow symbolic links listed on the command line 
 --dereference-command-line-symlink-to-dir
     follow each command line symbolic link that points to a directory
Run Code Online (Sandbox Code Playgroud)

如果这些没有帮助,您可以cd -通过执行以下操作使您的宏正常工作而不会搞砸:

(cd /rmn ; ls -l)
Run Code Online (Sandbox Code Playgroud)

它在子shell中运行。

  • 我真的希望 `--dereference-command-line-symlink-to-dir` 有一个简短的形式。 (5认同)

syn*_*ror 20

我不太明白为什么对这个简单问题的思考必须如此复杂——但也许我只是把问题弄错了。无论如何:当我需要类似的功能时,我使用了:

ls -lL
Run Code Online (Sandbox Code Playgroud)

这在我的情况下起到了作用。从ls手册页:

-L, --dereference
     when showing file information for a symbolic link, 
     show  information  for  the file the link references
     rather than for the link itself
Run Code Online (Sandbox Code Playgroud)

例如,我为我创建了一个/media/stick快捷方式来访问我的 U 盘上的数据。ls -l /media/stick将显示实际链接,同时ls -lL /media/stick将显示U 盘上的内容

有关的内部运作之间的差别一些深入的研究后-H-L,我终于跨越关于这个问题的优秀文章来到浅思考博客(甚至设有为你高兴一些巧妙的shell脚本技巧!)

这指出了两个选项之间的细微差别(比原始手册更好!)如下:

  • -H -- 仅取消引用在命令行中明确提到的那些链接
  • -L-- 取消引用链接,即使它们没有在命令行中提到

(当这两个用于简单的日常任务时,一般输出应该没有区别。)

此外:如果您更喜欢记住长形式 ( --option),那么它很可能--dereference--dereference-command-line-symlink-to-dir.


Sté*_*las 7

ls -l /rmn/
Run Code Online (Sandbox Code Playgroud)

会这样做或

ls -l /rmn/.
Run Code Online (Sandbox Code Playgroud)

但是,使用和不使用 的行为不应该有所不同-l。你有别名ls吗?

  • `ls` 标志很疯狂。`-l` 使 `-H` 默认为关闭,`-F` 也是如此。请参阅 [coreutils - 列出了哪些文件](http://www.gnu.org/software/coreutils/manual/html_node/Which-files-are-listed.html#Which-files-are-listed)。 (4认同)