How can I make "ls" show dotfiles first?
⚠️ 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
.
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
The ls(1) manpage lists:
-v natural sort of (version) numbers within text
This seems change how periods are sorted and does group dotfiles first. I have:
alias ls='ls -vAF'
alias ll='ls -l'
in my ~/.bashrc.