如何在 macOS 上安装和使用 GNU "ls"?
How to install and use GNU "ls" on macOS?
我正在关注 this 文章来更新我的 bash。我真的很想更新我的 bash 版本,以便我可以使用 ls
命令的 --group-directories-first
选项。
到目前为止,我已经完成了链接文章中的以下内容:
我成功执行了brew install bash
命令,下载了新的bash。根据文章,我可以验证我机器上的 /usr/local/bin/bash --version
是否显示以下内容:
GNU bash, version 5.0.11(1)-release (x86_64-apple-darwin18.6.0)
Copyright (C) 2019 Free Software Foundation, Inc. License GPLv3+: GNU
GPL version 3 or later http://gnu.org/licenses/gpl.html
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
- 我完成了
sudo vim /etc/shells
命令,编辑文件以包含新下载的 bash。这是它的样子:
- 我执行了
chsh -s /usr/local/bin/bash
以将新的 bash 设置为默认值。
但是,即使在我关闭终端并重新启动它之后,我也不确定我是否在使用新终端。这是因为:
当我执行 bash --version
时,我得到以下信息:
GNU bash, version 5.0.11(1)-release (x86_64-apple-darwin18.6.0)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
但是当我 运行 echo $BASH_VERSION
时,我得到了相反的结果 (3.2.57(1)-release
)。这不是老版本吗?
- 此外,新的 "group directories first" 命令仍然不起作用。当我 运行
ls --group-directories-first
: 时出现以下错误
ls: illegal option -- - usage: ls
[-ABCFGHLOPRSTUWabcdefghiklmnopqrstuwx1] [file ...]
如有任何帮助或指点,我们将不胜感激。
谢谢!
这与bash
无关。命令 ls
不是 bash
.
的内置命令
macOS 基于 Unix 操作系统。它的一些基本命令是 BSD 风格的。您想要的 --group-directories-first
选项仅在 GNU ls
中可用。你所需要的只是一个 GNU 风格的 ls
.
解决方法:安装coreutils
,其中包含GNU ls
。
brew install coreutils
将以下代码添加到 ~/.bash_profile
中,使 ls
命令和其他 GNU 风格的命令优先于 macOS 中内置的 BSD 风格命令。
# Make all GNU flavor commands available, may override same-name BSD flavor commands
# For x86 Mac
export PATH="/usr/local/opt/coreutils/libexec/gnubin:${PATH}"
export MANPATH="/usr/local/opt/coreutils/libexec/gnuman:${MANPATH}"
# For M1 Mac
export PATH="/opt/homebrew/opt/coreutils/libexec/gnubin:${PATH}"
export MANPATH="/opt/homebrew/opt/coreutils/libexec/gnuman:${MANPATH}"
如果您只想要 GNU ls
而不是其他 GNU 风格的命令。不要添加以上内容,而是在~/.bash_profile
.
中创建一个别名
# For x86 Mac
alias ls="/usr/local/opt/coreutils/libexec/gnubin/ls"
# For M1 Mac
alias ls="/opt/homebrew/opt/coreutils/libexec/gnubin/ls"
这里是coreutils
带来的所有命令。
❯ ls /usr/local/opt/coreutils/libexec/gnubin
'[' cat cksum dd echo fmt install ls mv od printf rmdir sha384sum split tac tr unexpand wc
b2sum chcon comm df env fold join md5sum nice paste ptx runcon sha512sum stat tail true uniq who
base32 chgrp cp dir expand groups kill mkdir nl pathchk pwd seq shred stdbuf tee truncate unlink whoami
base64 chmod csplit dircolors expr head link mkfifo nohup pinky readlink sha1sum shuf stty test tsort uptime yes
basename chown cut dirname factor hostid ln mknod nproc pr realpath sha224sum sleep sum timeout tty users
basenc chroot date du false id logname mktemp numfmt printenv rm sha256sum sort sync touch uname vdir
我正在关注 this 文章来更新我的 bash。我真的很想更新我的 bash 版本,以便我可以使用 ls
命令的 --group-directories-first
选项。
到目前为止,我已经完成了链接文章中的以下内容:
我成功执行了
brew install bash
命令,下载了新的bash。根据文章,我可以验证我机器上的/usr/local/bin/bash --version
是否显示以下内容:GNU bash, version 5.0.11(1)-release (x86_64-apple-darwin18.6.0) Copyright (C) 2019 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html
This is free software; you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.
- 我完成了
sudo vim /etc/shells
命令,编辑文件以包含新下载的 bash。这是它的样子:
- 我完成了
- 我执行了
chsh -s /usr/local/bin/bash
以将新的 bash 设置为默认值。
但是,即使在我关闭终端并重新启动它之后,我也不确定我是否在使用新终端。这是因为:
当我执行
bash --version
时,我得到以下信息:GNU bash, version 5.0.11(1)-release (x86_64-apple-darwin18.6.0) Copyright (C) 2019 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html
This is free software; you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.
但是当我 运行 echo $BASH_VERSION
时,我得到了相反的结果 (3.2.57(1)-release
)。这不是老版本吗?
- 此外,新的 "group directories first" 命令仍然不起作用。当我 运行
ls --group-directories-first
: 时出现以下错误
ls: illegal option -- - usage: ls [-ABCFGHLOPRSTUWabcdefghiklmnopqrstuwx1] [file ...]
如有任何帮助或指点,我们将不胜感激。
谢谢!
这与bash
无关。命令 ls
不是 bash
.
macOS 基于 Unix 操作系统。它的一些基本命令是 BSD 风格的。您想要的 --group-directories-first
选项仅在 GNU ls
中可用。你所需要的只是一个 GNU 风格的 ls
.
解决方法:安装coreutils
,其中包含GNU ls
。
brew install coreutils
将以下代码添加到 ~/.bash_profile
中,使 ls
命令和其他 GNU 风格的命令优先于 macOS 中内置的 BSD 风格命令。
# Make all GNU flavor commands available, may override same-name BSD flavor commands
# For x86 Mac
export PATH="/usr/local/opt/coreutils/libexec/gnubin:${PATH}"
export MANPATH="/usr/local/opt/coreutils/libexec/gnuman:${MANPATH}"
# For M1 Mac
export PATH="/opt/homebrew/opt/coreutils/libexec/gnubin:${PATH}"
export MANPATH="/opt/homebrew/opt/coreutils/libexec/gnuman:${MANPATH}"
如果您只想要 GNU ls
而不是其他 GNU 风格的命令。不要添加以上内容,而是在~/.bash_profile
.
# For x86 Mac
alias ls="/usr/local/opt/coreutils/libexec/gnubin/ls"
# For M1 Mac
alias ls="/opt/homebrew/opt/coreutils/libexec/gnubin/ls"
这里是coreutils
带来的所有命令。
❯ ls /usr/local/opt/coreutils/libexec/gnubin
'[' cat cksum dd echo fmt install ls mv od printf rmdir sha384sum split tac tr unexpand wc
b2sum chcon comm df env fold join md5sum nice paste ptx runcon sha512sum stat tail true uniq who
base32 chgrp cp dir expand groups kill mkdir nl pathchk pwd seq shred stdbuf tee truncate unlink whoami
base64 chmod csplit dircolors expr head link mkfifo nohup pinky readlink sha1sum shuf stty test tsort uptime yes
basename chown cut dirname factor hostid ln mknod nproc pr realpath sha224sum sleep sum timeout tty users
basenc chroot date du false id logname mktemp numfmt printenv rm sha256sum sort sync touch uname vdir