如何从 oh-my-zsh 和 Mac 上的 'ls' 命令输出中删除文件类型分类器?

How to remove file type classifier from the 'ls' command output on oh-my-zsh and Mac?

我怀疑这与我在 Mac 上的 oh-my-zsh 设置有关,但我无法弄清楚。当我在命令行上执行 ls *.sh 时,我得到类似:

foo.sh* bar.sh*

我了解默认情况下,-F 应用于 ls

-F Display a slash ('/') immediately after each pathname that is a directory, an asterisk ('*') after each that is executable, an at sign ('@') after each symbolic link, an equals sign ('=') after each socket, a percent sign ('%') after each white-out, and a vertical bar ('|') after each that is a FIFO.

我真的很想列出不带分类符的文件名。这是可取的,因为没有它,我的 for:

for f in `ls *.sh` 
do
wc -l $f
done

可执行 shell 脚本中断(因为尾随 *):

wc: foo.sh*: open: No such file or directory 
wc: bar.sh*: open: No such file or directory

当然,我可以轻松解决这个问题。我只是想知道我是否可以在 ls 命令的输出中处理它。此外,当我的 shell 是 bash.

时,这也能正常工作

无需使用 ls 即可生成匹配文件列表。您可以直接使用模式:

for f in *.sh
do
    wc -l $f
done

这也有利于处理包含空格的文件名。

一般来说:ls 的输出是供人类使用的,永远不应该被解析。通常文件列表可以直接用 globbing 生成,ls 可能提供的任何信息都可以通过其他方式检索(例如 stat)。


也就是说:

-F 默认情况下未启用,因此有一个选项。我怀疑 ls 是别名。您可以查看

whence -v ls

虽然 oh-my-zsh 默认创建 ls 的别名,但 -F 不是设置的一部分。

如果您只想使用未修改的 ls(可能有不同的选项),您可以在前面加上 command 预命令修饰符:

command ls

这将运行外部命令ls(通常/bin/ls)而不是别名或函数。

如果您想永久删除 -F,您可以在配置最后的某处覆盖它。或者你追踪它的设置位置并在那里进行更改。此命令可能有助于找到设置别名的位置:

grep -r $(whence ls) ~/.zshrc ~/oh-my-zsh.sh ~/.oh-my-zsh

根据您的设置,可能还有其他配置文件。