Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I like being able to name files and directories with an underscore prefix if it's something I want to keep separate from other files and directories at the same level. On Windows and Mac, for example, prefixing a file with an underscore sorts it to the top, in front of files starting with an alphanumeric character.

My googling has turned up that it has to do with the LC_COLLATE and my current locale (en_US). That's fine, though I really don't understand why en_US doesn't sort as expected.

Based on the ICU Collate demonstration site setting locale to en_US_POSIX certainly appears to have the sort order I'm looking for (you have to edit the sample data and add some underscores to test it out). But I don't really see how to apply this in my Linux shell.

Ideally, I'd like to be able to set up something in my bash config so that ls always sorts underscores first. How would I go about doing this?

share|improve this question
    
I can't reproduce using ICU Collate with defaults or with en_US_POSIX.txt via "Fetch rules for locale". Can you explain the settings you used? –  Mikel Jun 1 '12 at 17:58
    
Similar question askubuntu.com/questions/47702/… –  Mikel Jun 1 '12 at 19:16
    
@Mikel using the link I supplied above, add some underscores to the test data and then submit to see the results of the sort. –  Tom Auger Jun 2 '12 at 13:36
    
That's exactly what I did, and strings beginning with underscores get sorted in the middle rather than the beginning, as if the underscores were not there. –  Mikel Jun 2 '12 at 15:54
    
@Mikel wow, you're right. I had to go in and change the "sort order" to "Phonebook" sort order before it started putting underscores first. –  Tom Auger Jun 2 '12 at 19:42

4 Answers 4

up vote 5 down vote accepted

If you can't get ls to sort the way you want, try shell expansion.

You can use file name patterns to run ls with a list of files that the shell already sorted, bypassing the method that ls uses.

ls -lf _* [!_]*

Assuming you have the files

_a a _b b _c c

this is like running

ls -lf _a _b _c a b c

Explanation:

_* is a shell pattern matching any file name beginning with an underscore, expanded in alphabetic order.

[!_]* matches any file name not beginning with an underscore, expanded in alphabetic order.

-f tells ls to not sort, because the shell already did.

More information: bash filename expansion

If there are directories in the current directory you will want to run the command like this to avoid ls listing files in the directories:

ls -lfd _* [!_]*
share|improve this answer
4  
By the way, DOS/Windows/OSX don't really put underscores before anything else: they sort case-insensitively with the underscore put before letters, but some other punctuation characters go before or after the underscore. Using _ to make files appear first is an OS-specific hack; and the unix version of this hack is to start the file name with a capital letter: the default unix convention is to use only lowercase letters in file names. –  Gilles Jun 2 '12 at 13:53
2  
Or zeros; e.g. 00README. –  mattdm Jun 2 '12 at 14:56
1  
@Gilles +1 for the unix best practice of using caps on important files to make them ls first. At the end of the day, if that's the convention, it's probably best that I simply adopt that, rather than attempt to force unix to behave the way other OSes do so I can use conventions that were developed for Mac or Windows. Thanks for the great tip. –  Tom Auger Jun 2 '12 at 19:46
1  
@TomAuger -f tells ls not to do its own sorting, so it displays its arguments in the order they are passed. The result of each the shell wildcard expansion _* and [!_]* is a lexicographically sorted list. –  Gilles Jun 2 '12 at 19:51
1  
@TomAuger The arguments to ls are sorted (in two groups: the ones starting with _, then the others) when they are generated by the shell. Run echo ls -lf _* [!_]* to see what happens. The -f flag tells ls not to do any sorting. –  Gilles Jun 4 '12 at 18:02

Unfortunately Linux uses glibc for its locale info, not ICU, so there is no way to directly apply this to Linux without expending a lot of effort either retrofitting ICU into glibc or supplementing the locale info in glibc.

share|improve this answer
    
+1 Good to know. –  Tom Auger Jun 2 '12 at 13:37

If you don't care to mix lowercase and uppercase, set your locale to C, which takes characters in their numerical order. _ falls between uppercase and lowercase.

$ LC_COLLATE=C ls    
BAR  FOO  _score  _under  hello  world
$ LC_COLLATE=en_US ls                    
BAR  FOO  hello  _score  _under  world

The locale settings LC_MESSAGES (language of error messages), LC_CTYPE (character sets) and LC_TIME (date and time format) are vey useful. LC_COLLATE and LC_NUMERIC are usually more trouble than they're worth, I don't recommend setting them. Proper lexicographic sorting is more complicated than LC_COLLATE is supposed to specify, and it can cause all sorts of weird behaviors when you use character ranges in regular expressions. LC_NUMERIC is mostly cosmetic, except when something goes horribly wrong because some program produced a number with a decimal separator other than ..

share|improve this answer
    
+1 Very interesting. So using this form, you're temporarily setting the environment variable LC_COLLATE just for that one instance of ls? Is that right? –  Tom Auger Jun 2 '12 at 13:40
    
Any way to make the underscores appear BEFORE the upper case letters? –  Tom Auger Jun 2 '12 at 13:42
    
@TomAuger Yes, VAR=value cmd sets VAR to value only in the environment of cmd and doesn't touch the value (or absence of value) in the shell where you run it. To make the underscore appear before uppercase, you would need to define your own locale settings. This is possible, but awkward to use, because at least under Linux, the standard library only looks for locale definitions in /usr/lib/locale — there's no ~/.locale or environment variable where you could put your en_tom setting. –  Gilles Jun 2 '12 at 13:49
    
@TomAuger If this is only about the ls command, go with Mikel's suggestion. –  Gilles Jun 2 '12 at 13:50

Adding the -f switch (no sorting) made it show that way for me.

man ls

[root@dusknoir ~/java/test]# ls -fl
total 0
-rw-r--r--  1 root  wheel  0 Jun  1 13:27 _1
-rw-r--r--  1 root  wheel  0 Jun  1 13:27 _2
-rw-r--r--  1 root  wheel  0 Jun  1 13:27 _3
-rw-r--r--  1 root  wheel  0 Jun  1 13:27 1
-rw-r--r--  1 root  wheel  0 Jun  1 13:27 2
-rw-r--r--  1 root  wheel  0 Jun  1 13:27 3
share|improve this answer
3  
Only because that's how they're stored in the filesystem. –  Ignacio Vazquez-Abrams Jun 1 '12 at 17:33
1  
Sorry, but this answer is plain wrong. Test: touch 3 1 _1 _3 2 _2 && ls -fl outputs 2 . 1 3 _2 _3 .. _1 –  Marco Jun 1 '12 at 17:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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