How can I make "ls" show dotfiles first?
Somewhere along the way I screwed up my ls command and now I get this ordering when running
$ ls -AhHl --color=auto
-rwxr-xr-x 1 clang clang 640 Mar 1 02:46 apple-touch-icon-precomposed.png
-rwxr-xr-x 1 clang clang 784 Jul 12 02:54 crossdomain.xml
-rwxr-xr-x 1 clang clang 1.2K Mar 1 02:46 favicon.ico
drwxr-xr-x 8 clang clang 4.0K Jul 12 23:50 .git
-rw-r--r-- 1 clang clang 17 Feb 29 19:48 .gitignore
-rwxr-xr-x 1 clang clang 1.4K Jul 12 02:54 humans.txt
What did I do that made ls ignore the dotfiles and instead order by first letter?
Output of locale
:
$ locale
LANG=
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=en_US.UTF-8
Solution 1:
⚠️ This answer is a little dated. Please check out the other answers, particularly those using aliases or
ls -v
.
Try adding
export LC_COLLATE="C"
in your dotfiles, or changing the LC_ALL
assignment to:
export LC_ALL="C"
This controls the way sorting on character level works — while the default would be to sort dotfiles inline, this will make sort
list dotfiles first.
However, note that this will basically stop support for your actual locale across all locale-aware utilities.
To go further, quoting the GNU Coreutils manual (emphasis mine):
If you use a non-POSIX locale (e.g., by setting
LC_ALL
toen_US
), then sort may produce output that is sorted differently than you're accustomed to.In that case, set the
LC_ALL
environment variable toC
. Note that setting onlyLC_COLLATE
has two problems. First, it is ineffective ifLC_ALL
is also set. Second, it has undefined behavior ifLC_CTYPE
(orLANG
, ifLC_CTYPE
is unset) is set to an incompatible value. For example, you get undefined behavior ifLC_CTYPE
isja_JP.PCK
butLC_COLLATE
isen_US.UTF-8
.
Solution 2:
To avoid any system wide changes without real need, one can change only the way how ls
works for the current user by adding the alias to the .bashrc
:
alias ll='LC_COLLATE=C ls -alF'
This sorts dot files first, allows to properly handle (show and sort) "uncommon" character sets like cyrillic. The only culprit that the sorting will be case-sensitive.
Source: http://ubuntuforums.org/showthread.php?t=816753