23

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.

16

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.

  • 1
    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 1: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 3: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
  • 2
    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 4 '14 at 19:34
1

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.