How do I list folders with underscores first?

So as pretty much everyone, before using Ubuntu I used a Windows machine, and one of the things I got used to in Windows was to use the underscore as the first character of a folder name as some sort of special marking so that it would be appear first on listing, both in command line and file explorer.

For instance, in my eBooks folder (in Windows) looks something like this:

_read
      Book1.pdf
      Book2.pdf
_reading
      Book3.pdf
Book4.pdf
Book5.pdf

but in Ubuntu's Nautilus it looks like this:

Book4.pdf
Book5.pdf    
_read
      Book1.pdf
      Book2.pdf
_reading
      Book3.pdf

What is happening is that it is completely ignoring the underscore and it treats "_read" as "read" and therefore not listing it in first as wished.

I have tried setting the environment variable LC_COLLATE to C. This helps somehow since it lists the underscore together, but at the end, and not at the beginning.

Thanks.


Since you have not specified the locale that you are currently using, this solution assumes that you are using en_US.UTF-8 as your default.

You can check this by running the command locale from the command line(Ctrl+Alt+t).

For example:

~ $ locale
LANG=en_US.UTF-8
LANGUAGE=en_US
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=

The variable that affects the sorting order of files is called LC_COLLATE.

According to the Ubuntu help pages, the LC_COLLATE variable determines "How strings (file names...) are alphabetically sorted. Using the "C" or "POSIX" locale here results in a strcmp()-like sort order, which may be preferable to language-specific locales."

The sort order will need to be changed from LC_COLLATE="en_US.UTF-8" to LC_COLLATE=C.

From the command line(Ctrl+Alt+t), enter:

update-locale LC_COLLATE=C

Important: You need to log out of your system and then log back in for the changes to take effect.

Running the locale command again, it should show that the LC_COLLATE variable has been successfully changed. Running the command ls -l again should reflect this change.


caja (and I think Nautilus before it) has an option in Preferences to sort folders before files. Would that help?