本文介绍了macOS 终端:`ls` 首先使用大写名称对结果进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何修复 macOS 终端中 ls 的默认排序行为?我希望结果不区分大小写并按字母顺序排列.

How can I fix this default sorting behaviour for ls in the macOS Terminal? I want the results to be case-insensitive and alphabetised.

这是一个示例,用于说明在包含以下文件/目录的目录中运行 ls 时当前和所需的行为:Apple_、apple、Basket_、basket

Here is an example to illustrate the current and desired behaviours when running ls in a directory with the files/directories: Apple_, apple, Basket_, basket

当前行为:

苹果_

篮子_

苹果

篮子

期望的行为:

苹果_

苹果

篮子_

篮子

有人知道如何实现吗?

推荐答案

这不再有效

所以@Yoric非常接近,但是他的函数内容有点不对:确实会按字母顺序对结果进行排序,但是使用`-la'标志时,会搞乱点"的顺序;文件/目录(以点开头的那些).

This no longer works

So @Yoric was very close, but the content of his function was a bit wrong: it would indeed sort the results alphabetically, but when using the `-la' flag, it would mess up the order of the "dot" files/directories (those beginning with a dot).

我通过调整以下 Stack Overflow 帖子中的解决方案找到了正确的解决方案:制作ls"命令排序a"在B"之前(vs a->b->A->B)

  1. 在您的主目录 (~/) 中打开/创建您的 shell 配置文件.(每次启动 shell 时都会自动运行此文件).这将是:
  1. Open/create your shell profile file in your home directory (~/). (This file is automatically run every time you launch the shell). This would be:

  • .bash_profile 如果不使用配置框架
  • .zshrc 如果使用 zsh
    • .bash_profile if not using a configuration framework
    • .zshrc if using zsh
      1. 添加以下代码


      ls_sort() {
        LC_COLLATE=en_US.utf8 ls "$@"
      }
      
      alias ls='ls_sort'
      


      1. 重启你的终端


      感谢@Yoric 帮助我找到这个解决方案!


      Thank you @Yoric for helping me to get to this solution!

      这篇关于macOS 终端:`ls` 首先使用大写名称对结果进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 07:15