Why does the `ls` command sort files like this?Why does 'sort' ignore special characters, like the asterisk?Can I sort files A-Z and at the same time Z-A?Lotus notes 8.5 Inbox return to default sort and select next unread emailUnix shell: Multi-level sort in opposite directionsWhy does here-string argument to echo omit first word?What does “>> file command” do, and how does it differ from “command >> file”?Why does plink or bash not recognize command on first line of remote commands file?
Is a memoized pure function itself considered pure?
Hangman game in Python - need feedback on the quality of code
LINQ for generating all possible permutations
What's the point of fighting monsters in Zelda BoTW?
Why does the `ls` command sort files like this?
3D cryptic featuring Mao, Stalin and Simba's uncle
How many petaflops does it take to land on the moon? What does Artemis need with an Aitken?
Should open strings on guitar be tuned to the key of the song played?
If I said I had $100 when asked, but I actually had $200, would I be lying by omission?
Unlock your Lock
How can I draw lines between cells from two different tabulars to indicate correlation?
Is the internet in Madagascar faster than in UK?
Is there a word or phrase that means "use other people's wifi or Internet service without consent"?
Joining lists with same elements
Why is the UK so keen to remove the "backstop" when their leadership seems to think that no border will be needed in Northern Ireland?
Retroactively modifying humans for Earth?
Changing JPEG to RAW to use on Lightroom?
74S vs 74LS ICs
Open subspaces of CW complexes
Do you pay one or two mana to bounce a transformed Delver of Secrets with Repeal?
Why did my folder names end up like this, and how can I fix this using a script?
Weighted smooth histogram
How to prevent a hosting company from accessing a VM's encryption keys?
Why does matter stays collapsed following the supernova explosion?
Why does the `ls` command sort files like this?
Why does 'sort' ignore special characters, like the asterisk?Can I sort files A-Z and at the same time Z-A?Lotus notes 8.5 Inbox return to default sort and select next unread emailUnix shell: Multi-level sort in opposite directionsWhy does here-string argument to echo omit first word?What does “>> file command” do, and how does it differ from “command >> file”?Why does plink or bash not recognize command on first line of remote commands file?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
As I was trying to reverse engineer the ls command, I came upon an interesting behavior. When I make 3 files, foo.png
, foopa.png
, and fooqa.png
, ls sorts them as foopa.png
, foo.png
, and fooqa.png
. I also tried it using the .gif extension and it seems to be that it happens when p and q are replaced by the first letter of the extension and the next letter in the alphabet; so in the case of .gif it would be g and h. (fooga.gif
, then foo.gif
, then fooha.gif
)
Why does it order the output this way?
linux command-line sorting ls coreutils
New contributor
add a comment |
As I was trying to reverse engineer the ls command, I came upon an interesting behavior. When I make 3 files, foo.png
, foopa.png
, and fooqa.png
, ls sorts them as foopa.png
, foo.png
, and fooqa.png
. I also tried it using the .gif extension and it seems to be that it happens when p and q are replaced by the first letter of the extension and the next letter in the alphabet; so in the case of .gif it would be g and h. (fooga.gif
, then foo.gif
, then fooha.gif
)
Why does it order the output this way?
linux command-line sorting ls coreutils
New contributor
1
Don't post links to images of text.
– RalfFriedl
8 hours ago
@RalfFriedl Oh sorry about that, I'll fix it
– mooncat39
8 hours ago
add a comment |
As I was trying to reverse engineer the ls command, I came upon an interesting behavior. When I make 3 files, foo.png
, foopa.png
, and fooqa.png
, ls sorts them as foopa.png
, foo.png
, and fooqa.png
. I also tried it using the .gif extension and it seems to be that it happens when p and q are replaced by the first letter of the extension and the next letter in the alphabet; so in the case of .gif it would be g and h. (fooga.gif
, then foo.gif
, then fooha.gif
)
Why does it order the output this way?
linux command-line sorting ls coreutils
New contributor
As I was trying to reverse engineer the ls command, I came upon an interesting behavior. When I make 3 files, foo.png
, foopa.png
, and fooqa.png
, ls sorts them as foopa.png
, foo.png
, and fooqa.png
. I also tried it using the .gif extension and it seems to be that it happens when p and q are replaced by the first letter of the extension and the next letter in the alphabet; so in the case of .gif it would be g and h. (fooga.gif
, then foo.gif
, then fooha.gif
)
Why does it order the output this way?
linux command-line sorting ls coreutils
linux command-line sorting ls coreutils
New contributor
New contributor
edited 8 hours ago
mooncat39
New contributor
asked 9 hours ago
mooncat39mooncat39
314 bronze badges
314 bronze badges
New contributor
New contributor
1
Don't post links to images of text.
– RalfFriedl
8 hours ago
@RalfFriedl Oh sorry about that, I'll fix it
– mooncat39
8 hours ago
add a comment |
1
Don't post links to images of text.
– RalfFriedl
8 hours ago
@RalfFriedl Oh sorry about that, I'll fix it
– mooncat39
8 hours ago
1
1
Don't post links to images of text.
– RalfFriedl
8 hours ago
Don't post links to images of text.
– RalfFriedl
8 hours ago
@RalfFriedl Oh sorry about that, I'll fix it
– mooncat39
8 hours ago
@RalfFriedl Oh sorry about that, I'll fix it
– mooncat39
8 hours ago
add a comment |
2 Answers
2
active
oldest
votes
It depends on the collation order of your locale:
>LANG=en_IE.UTF-8 ls -1 foo*
foopa.png
foo.png
fooqa.png
>LANG=C ls -1 foo*
foo.png
foopa.png
fooqa.png
C collation order is purely alphabetical (ASCII order). Other collation orders (such as English) may consider spaces and special characters such as dots as separators and either handle "words" separately or just ignore these separators (which appears to be the case here).
Note that the non-UTF-8 locale sorts using alphabetic ASCII, too:
>LANG=en_IE ls -1 foo*
foo.png
foopa.png
fooqa.png
add a comment |
ls
sorts alphabetically according to strcoll() unless you tell it otherwise. It doesn't treat the period specially, so it won't sort by the "filename extension" after any periods that are there.
It is perplexing that period comes between p and q in the sort order, though! In the default C locale, period comes before any letters (man ascii
).
Here's a concise puzzle for other answerers, which I confirmed with ltrace -e strcoll /bin/ls
on Ubuntu 14.04:
Why does f.g
come before fga.g
,
but f.gif
comes after fga.gif
?
1
This can be explained if the collation order for english just ignores the dots. You get the same order forfg fgag fgif fgagif
.
– xenoid
7 hours ago
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "3"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
mooncat39 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1475533%2fwhy-does-the-ls-command-sort-files-like-this%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
It depends on the collation order of your locale:
>LANG=en_IE.UTF-8 ls -1 foo*
foopa.png
foo.png
fooqa.png
>LANG=C ls -1 foo*
foo.png
foopa.png
fooqa.png
C collation order is purely alphabetical (ASCII order). Other collation orders (such as English) may consider spaces and special characters such as dots as separators and either handle "words" separately or just ignore these separators (which appears to be the case here).
Note that the non-UTF-8 locale sorts using alphabetic ASCII, too:
>LANG=en_IE ls -1 foo*
foo.png
foopa.png
fooqa.png
add a comment |
It depends on the collation order of your locale:
>LANG=en_IE.UTF-8 ls -1 foo*
foopa.png
foo.png
fooqa.png
>LANG=C ls -1 foo*
foo.png
foopa.png
fooqa.png
C collation order is purely alphabetical (ASCII order). Other collation orders (such as English) may consider spaces and special characters such as dots as separators and either handle "words" separately or just ignore these separators (which appears to be the case here).
Note that the non-UTF-8 locale sorts using alphabetic ASCII, too:
>LANG=en_IE ls -1 foo*
foo.png
foopa.png
fooqa.png
add a comment |
It depends on the collation order of your locale:
>LANG=en_IE.UTF-8 ls -1 foo*
foopa.png
foo.png
fooqa.png
>LANG=C ls -1 foo*
foo.png
foopa.png
fooqa.png
C collation order is purely alphabetical (ASCII order). Other collation orders (such as English) may consider spaces and special characters such as dots as separators and either handle "words" separately or just ignore these separators (which appears to be the case here).
Note that the non-UTF-8 locale sorts using alphabetic ASCII, too:
>LANG=en_IE ls -1 foo*
foo.png
foopa.png
fooqa.png
It depends on the collation order of your locale:
>LANG=en_IE.UTF-8 ls -1 foo*
foopa.png
foo.png
fooqa.png
>LANG=C ls -1 foo*
foo.png
foopa.png
fooqa.png
C collation order is purely alphabetical (ASCII order). Other collation orders (such as English) may consider spaces and special characters such as dots as separators and either handle "words" separately or just ignore these separators (which appears to be the case here).
Note that the non-UTF-8 locale sorts using alphabetic ASCII, too:
>LANG=en_IE ls -1 foo*
foo.png
foopa.png
fooqa.png
edited 6 hours ago
answered 7 hours ago
xenoidxenoid
5,3283 gold badges10 silver badges21 bronze badges
5,3283 gold badges10 silver badges21 bronze badges
add a comment |
add a comment |
ls
sorts alphabetically according to strcoll() unless you tell it otherwise. It doesn't treat the period specially, so it won't sort by the "filename extension" after any periods that are there.
It is perplexing that period comes between p and q in the sort order, though! In the default C locale, period comes before any letters (man ascii
).
Here's a concise puzzle for other answerers, which I confirmed with ltrace -e strcoll /bin/ls
on Ubuntu 14.04:
Why does f.g
come before fga.g
,
but f.gif
comes after fga.gif
?
1
This can be explained if the collation order for english just ignores the dots. You get the same order forfg fgag fgif fgagif
.
– xenoid
7 hours ago
add a comment |
ls
sorts alphabetically according to strcoll() unless you tell it otherwise. It doesn't treat the period specially, so it won't sort by the "filename extension" after any periods that are there.
It is perplexing that period comes between p and q in the sort order, though! In the default C locale, period comes before any letters (man ascii
).
Here's a concise puzzle for other answerers, which I confirmed with ltrace -e strcoll /bin/ls
on Ubuntu 14.04:
Why does f.g
come before fga.g
,
but f.gif
comes after fga.gif
?
1
This can be explained if the collation order for english just ignores the dots. You get the same order forfg fgag fgif fgagif
.
– xenoid
7 hours ago
add a comment |
ls
sorts alphabetically according to strcoll() unless you tell it otherwise. It doesn't treat the period specially, so it won't sort by the "filename extension" after any periods that are there.
It is perplexing that period comes between p and q in the sort order, though! In the default C locale, period comes before any letters (man ascii
).
Here's a concise puzzle for other answerers, which I confirmed with ltrace -e strcoll /bin/ls
on Ubuntu 14.04:
Why does f.g
come before fga.g
,
but f.gif
comes after fga.gif
?
ls
sorts alphabetically according to strcoll() unless you tell it otherwise. It doesn't treat the period specially, so it won't sort by the "filename extension" after any periods that are there.
It is perplexing that period comes between p and q in the sort order, though! In the default C locale, period comes before any letters (man ascii
).
Here's a concise puzzle for other answerers, which I confirmed with ltrace -e strcoll /bin/ls
on Ubuntu 14.04:
Why does f.g
come before fga.g
,
but f.gif
comes after fga.gif
?
edited 7 hours ago
answered 8 hours ago
Camille GoudeseuneCamille Goudeseune
8721 gold badge15 silver badges31 bronze badges
8721 gold badge15 silver badges31 bronze badges
1
This can be explained if the collation order for english just ignores the dots. You get the same order forfg fgag fgif fgagif
.
– xenoid
7 hours ago
add a comment |
1
This can be explained if the collation order for english just ignores the dots. You get the same order forfg fgag fgif fgagif
.
– xenoid
7 hours ago
1
1
This can be explained if the collation order for english just ignores the dots. You get the same order for
fg fgag fgif fgagif
.– xenoid
7 hours ago
This can be explained if the collation order for english just ignores the dots. You get the same order for
fg fgag fgif fgagif
.– xenoid
7 hours ago
add a comment |
mooncat39 is a new contributor. Be nice, and check out our Code of Conduct.
mooncat39 is a new contributor. Be nice, and check out our Code of Conduct.
mooncat39 is a new contributor. Be nice, and check out our Code of Conduct.
mooncat39 is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1475533%2fwhy-does-the-ls-command-sort-files-like-this%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Don't post links to images of text.
– RalfFriedl
8 hours ago
@RalfFriedl Oh sorry about that, I'll fix it
– mooncat39
8 hours ago