7

ls manpage says:

List  information  about  the FILEs (the current directory by
default).  Sort entries alphabetically if none of -cftuvSUX nor --sort
is specified.

and

-a, --all    do not ignore entries starting with .

and

-1     list one file per line.

but

/bin/ls -a1

lists entries alphabetically by the filename with any leading . removed, which is not alphabetically.

The sort command does the same.

Is there any way to get ls to work the normal way (with . files first - which isn't alphabetic either)?

  • 4
    The period isn't in the alphabet, hence it's locale-dependent, per steeldriver's answer. – wjandrea Mar 14 at 12:54
  • You do understand that files and folders that start with . are hidden, correct? – Terrance Mar 14 at 13:19
  • 1
    I think that's why the OP uses the -a option. – Arronical Mar 14 at 14:44
  • It's doing exactly what it says in the man page: sorting the files alphabetically. It sounds like you want them sorted in ASCIIbetical order instead. – Mark Mar 14 at 21:55
11

The order is determined by the collation sequence of your locale.

Ex. in my default locale (which is en_CA.UTF-8)

$ ls -A1
a
.b
c

whereas in the C locale

$ LC_COLLATE=C ls -A1
.b
a
c

(I used -A instead of -a to eliminate the . and .. directories).


Another way to get the order you want might be to use the -v (version sort) option as suggested in Grouping hidden files and directories with ls - although that's not really what it is intended for:

  -v     natural sort of (version) numbers within text

Since ls is almost certainly already aliased inside your .bashrc file, you could simply modify the alias(es) there to include the -v.

  • None of my LC_ variables are set. I'm surprised that the default does something complicated rather something simple. LC_ALL=C now in my startup files. – David Ledger Mar 14 at 12:47
  • 1
    @DavidLedger to see your locale settings, type locale (they don't appear explicitly as environment variables unless you set them as such) – steeldriver Mar 14 at 12:50
  • Yes, that was set to en_US.UTF-8 I'm a shell person since '83, so locale generally makes no difference at all. Thanks. – David Ledger Mar 14 at 12:58
  • Try adding export LC_ALL=C to your .profile. – John Wiersba Mar 14 at 21:29

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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