When using 'ls' i want to sort directories first and then all files AND sort directories/files with underscore first. Here is an example:


I can sort directories first with

ls --group-directories-first --color --ignore=lost+found

but the underscore at the beginning is ignored. Sorting underscore at the beginning works with

ls -d _* [!_]*

but both doesn't work together. I have tried a lot of combinations of the two commands, nothings works as i want it to work. sometimes the color is lost (whats important for me) or i have the subfolders and files of the directores instead of the folder name.

Also LC_COLLATE=C and LC_LOCALE=C doesn't work.

  • LC_COLLATE=C ls --group-directories-first should work as long as the LC_ALL env var is not set. LC_ALL=C ls --group-directories-first would always work but cause the error messages to be output in English. Mar 7, 2019 at 13:53
  • No luck here. Don't work with LC_COLLATE=C or LC_ALL=C.
    – fmeier
    Mar 7, 2019 at 17:14
  • Note that it's not LC_ALL=C; ls ..., it's LC_ALL=C ls ... (or LC_ALL=C; export LC_ALL; ls, but that would mean setting the locale to C for every command, not just ls), or with csh/tcsh/fish env LC_ALL=C ls ... Mar 7, 2019 at 17:28
  • ls -d _* [!_]* wouldn't make a difference because ls still sorts the file list. You'd need ls -Ud -- _* [!_]* (assuming GNU ls) to disable that sorting. Mar 7, 2019 at 17:32


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.