Making the “ls” command sort “a” before “B” (vs a->b->A->B)

…衆ロ難τιáo~ 提交于 2019-12-10 15:07:32

问题


I am trying to find a way to have the results of an ls command be printed in a case insensitive manner.

currently an ls command results in:

Apple
Boy
Chart
Dock
apples
boys
charts
docks

what i want is this:

Apple
apples
Boy
boys
Chart
charts
Dock
docks

is this possible?


回答1:


ls (at least if you're using the GNU coreutils version; ls --version to check that) sorts file names according to the current locale.

The set of available locales varies from system to system (locale -a for a list), but on my system this:

LC_COLLATE=en_US.utf8 ls

sorts names with a and A before b and B -- though it might not be exactly in the order you're looking for.

This works even when ls lists files in multiple columns, something that's difficult to do with sort -f.

(I have $LC_COLLATE set to C specifically so that locale-sensitive sorting is done in ASCII order.)




回答2:


Just pipe the result to sort -f.




回答3:


# alias ll='(LC_COLLATE=en_US.utf8 && export LC_COLLATE && shopt -s nocaseglob && /bin/ls -alF --color=auto )'
# ll



回答4:


As a follow up to [Keith Thompson]'s answer, I tested on a Linux system and LC_COLLATE=C did not work for me, but LC_COLLATE="en_US.UTF-8" did. I put the following in my startup script:

export LANG=en_US.UTF-8
export LC_COLLATE="en_US.UTF-8"

This did not work on OS X.




回答5:


Add to .profile:

alias llg='ls -ltr | grep -i'

then you can use following command:

llg "week"


来源:https://stackoverflow.com/questions/18413646/making-the-ls-command-sort-a-before-b-vs-a-b-a-b

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!