- sort - Why does `ls` list the following files in seemingly different orders? - Unix & Linux Stack Exchange
- When providing a list of files as arguments to ls, should ls list them in the same order as they are specified as arguments? - Unix & Linux Stack Exchange
shall sort directory and non-directory operands separately according to the collating sequence in the current locale.
なんだか locale に関連しているようで難しい・・・。同じ Ubuntu でも GCP 上と Colab で違うというケースがあって、LANG=C.UTF-8
にしたら同じになった。ただ、これが正しいのかどうか・・・。
tips: sort は LANG=C で4倍速くなる - Qiita もあるし、順序を固定したいなら
LANG=C ls
しておくと良いかな?
Ubuntu 18.04 上では
LANG=en_US.UTF-8 ls -1 *.pfa SourceHanSans-Regular-Alphabetic.pfa SourceHanSans-Regular-AlphaNum.pfa ...
みたいなことが起こるが、Ubuntu 18.04 上で LANG=C ls -1 *.pfa
および macOS 上では
SourceHanSans-Regular-AlphaNum.pfa SourceHanSans-Regular-Alphabetic.pfa ...
となるようだ。LC_COLLATE=C
を指定すると LANG
が何であれ
SourceHanSans-Regular-AlphaNum.pfa SourceHanSans-Regular-Alphabetic.pfa ...
となるようにも見える。難しい。ソースコードとしては大体
- https://github.com/wertarbyte/coreutils/blob/master/src/ls.c
- https://opensource.apple.com/source/file_cmds/file_cmds-82/ls/ls.c
あたりになるのだろうか?
FreeBSD の場合、https://github.com/freebsd/freebsd-src/tree/main/bin/ls に実装があって、lsコマンドをハックしてみよう - Yahoo! JAPAN Tech Blog に解説があるので読んでみるのも面白いかもしれない。ただ、ちょっといじった程度では Linux 上でコンパイルするのはきつそうな感じではあるのだが・・・。