27

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.

Kevin Bowen
  • 19,615
  • 55
  • 79
  • 83
nunos
  • 3,410
  • 6
  • 23
  • 25

2 Answers2

22

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.

Kevin Bowen
  • 19,615
  • 55
  • 79
  • 83
  • 2
    I have already set the LC_COLLATE to C, as I have described in the last paragraph of my question. Still, I am getting different results in both the terminal and the nautilus explorer and I have rebooted the computer in the meanwhile. Any idea why that's happening? – nunos Jan 14 '13 at 14:37
  • @nunos Do you have any aliases set for ls in your .bashrc? – Kevin Bowen Jan 15 '13 at 01:02
  • @nunos Interesting. What files system type are you checking this on? The only place that I have issues with ls -l sorting is on an ntfs formatted drive which doesn't appear to honor the LC_COLLATE variable. All of my ext3 filesystems seem to respect that setting using the ls command as well as in Thunar and Nautilus. – Kevin Bowen Jan 15 '13 at 03:53
  • that might very well be the problem then. I am using the ls -l command on a NTFS drive, since I have a dual boot with windows. – nunos Jan 15 '13 at 13:16
  • 3
    update-locale LC_COLLATE=C does not help to get folders with underscores at the top of a sorted list if you use capital letters at the beginning of your folder names! – malisokan Jan 04 '14 at 19:34
  • In ls -l files with names beginning in an underscore in fact get sorted before file names beginning with characters. However: file names beginnign with 0...9 are sorted before file names beginning with underscore and - strangely! - a directory beginning with Ö (O-Umlaut) like Öffentlich (which is a customary thing on German installations) are sorted below all files! I don't know if this is yet another exception to what malisokan wrote before. – Adalbert Hanßen Jan 14 '18 at 18:22
  • @AdalbertHanßen thank for mentioning digits before underscores. – Jason Harrison May 09 '23 at 17:42
  • if you use underscore dot then the file will be sorted before filenames beginning with digits. Perhaps this needs a special name like "put-at-front" like the https://en.wikipedia.org/wiki/Interrobang. – Jason Harrison May 09 '23 at 17:44
1

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