/bin/ls sorts differently than just ls
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
$ ls |sort
xyz-0.0.1-1554490900.rpm
xyz-0.0.1-1554745305.rpm
xyz-0.0.1-1554751021.rpm
xyz-0.0.1-1555513460.rpm
xyz-0.0.1-1555951745.rpm
xyz-0.0.1-1554323568.rpm
$ /bin/ls |sort
xyz-0.0.1-1554323568.rpm
xyz-0.0.1-1554490900.rpm
xyz-0.0.1-1554745305.rpm
xyz-0.0.1-1554751021.rpm
xyz-0.0.1-1555513460.rpm
xyz-0.0.1-1555951745.rpm
$ which ls
alias ls='/bin/ls --color'
/bin/ls
Note that the sorting is different between the two commands (ls |sort
results in incorrect sorting). This must be due to the color flag, but why?
linux command-line bash unix
add a comment |
$ ls |sort
xyz-0.0.1-1554490900.rpm
xyz-0.0.1-1554745305.rpm
xyz-0.0.1-1554751021.rpm
xyz-0.0.1-1555513460.rpm
xyz-0.0.1-1555951745.rpm
xyz-0.0.1-1554323568.rpm
$ /bin/ls |sort
xyz-0.0.1-1554323568.rpm
xyz-0.0.1-1554490900.rpm
xyz-0.0.1-1554745305.rpm
xyz-0.0.1-1554751021.rpm
xyz-0.0.1-1555513460.rpm
xyz-0.0.1-1555951745.rpm
$ which ls
alias ls='/bin/ls --color'
/bin/ls
Note that the sorting is different between the two commands (ls |sort
results in incorrect sorting). This must be due to the color flag, but why?
linux command-line bash unix
add a comment |
$ ls |sort
xyz-0.0.1-1554490900.rpm
xyz-0.0.1-1554745305.rpm
xyz-0.0.1-1554751021.rpm
xyz-0.0.1-1555513460.rpm
xyz-0.0.1-1555951745.rpm
xyz-0.0.1-1554323568.rpm
$ /bin/ls |sort
xyz-0.0.1-1554323568.rpm
xyz-0.0.1-1554490900.rpm
xyz-0.0.1-1554745305.rpm
xyz-0.0.1-1554751021.rpm
xyz-0.0.1-1555513460.rpm
xyz-0.0.1-1555951745.rpm
$ which ls
alias ls='/bin/ls --color'
/bin/ls
Note that the sorting is different between the two commands (ls |sort
results in incorrect sorting). This must be due to the color flag, but why?
linux command-line bash unix
$ ls |sort
xyz-0.0.1-1554490900.rpm
xyz-0.0.1-1554745305.rpm
xyz-0.0.1-1554751021.rpm
xyz-0.0.1-1555513460.rpm
xyz-0.0.1-1555951745.rpm
xyz-0.0.1-1554323568.rpm
$ /bin/ls |sort
xyz-0.0.1-1554323568.rpm
xyz-0.0.1-1554490900.rpm
xyz-0.0.1-1554745305.rpm
xyz-0.0.1-1554751021.rpm
xyz-0.0.1-1555513460.rpm
xyz-0.0.1-1555951745.rpm
$ which ls
alias ls='/bin/ls --color'
/bin/ls
Note that the sorting is different between the two commands (ls |sort
results in incorrect sorting). This must be due to the color flag, but why?
linux command-line bash unix
linux command-line bash unix
asked 6 hours ago
Josh M.Josh M.
67451227
67451227
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Do:
/bin/ls --color > file1
/bin/ls > file2
and compare content, you'll see the difference.
Aliasing ls
to /bin/ls --color=auto
is likely better idea, it will stop ls
from using color codes when not writing directly to terminal (like when piping to next program or writing to a file).
Thank you. I assume this is just a "known thing" and people understand ls + sorting should be done directly via/bin/ls
vs.ls
.
– Josh M.
5 hours ago
4
@JoshM. Well, sorting should be done byls
itself, if possible. Sorting withsort
is parsing, not recommended in general.
– Kamil Maciorowski
5 hours ago
2
@JoshM., rather than using/bin/ls
, change your alias to what Tomak suggested and you will get the better behavior automatically.
– John1024
5 hours ago
1
@JoshM. You can also usels
for an unaliasedls
orcommand ls
for/bin/ls
.
– Freddy
5 hours ago
2
Yes, @Freddy, very true. But, usingalias ls='/bin/ls --color'
is just a recipe for trouble.
– John1024
5 hours ago
add a comment |
In the sorted colored output ls|sort
, we can see that the last line xyz-0.0.1-1554323568.rpm
is the first line
of the non-colored output. The other lines are sorted equally.
If we have at a look at the colored escape codes (non-sorted), we can see that the first
line starts with a different escape code ^[[0m
. This is causing the wrong order when sorted (^[[01
before ^[[0m
).
$ /bin/ls --color xyz* | cat -A
^[[0m^[[01;31mxyz-0.0.1-1554323568.rpm^[[0m$
^[[01;31mxyz-0.0.1-1554490900.rpm^[[0m$
^[[01;31mxyz-0.0.1-1554745305.rpm^[[0m$
^[[01;31mxyz-0.0.1-1554751021.rpm^[[0m$
^[[01;31mxyz-0.0.1-1555513460.rpm^[[0m$
^[[01;31mxyz-0.0.1-1555951745.rpm^[[0m$
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
});
}
});
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%2f1428322%2fbin-ls-sorts-differently-than-just-ls%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
Do:
/bin/ls --color > file1
/bin/ls > file2
and compare content, you'll see the difference.
Aliasing ls
to /bin/ls --color=auto
is likely better idea, it will stop ls
from using color codes when not writing directly to terminal (like when piping to next program or writing to a file).
Thank you. I assume this is just a "known thing" and people understand ls + sorting should be done directly via/bin/ls
vs.ls
.
– Josh M.
5 hours ago
4
@JoshM. Well, sorting should be done byls
itself, if possible. Sorting withsort
is parsing, not recommended in general.
– Kamil Maciorowski
5 hours ago
2
@JoshM., rather than using/bin/ls
, change your alias to what Tomak suggested and you will get the better behavior automatically.
– John1024
5 hours ago
1
@JoshM. You can also usels
for an unaliasedls
orcommand ls
for/bin/ls
.
– Freddy
5 hours ago
2
Yes, @Freddy, very true. But, usingalias ls='/bin/ls --color'
is just a recipe for trouble.
– John1024
5 hours ago
add a comment |
Do:
/bin/ls --color > file1
/bin/ls > file2
and compare content, you'll see the difference.
Aliasing ls
to /bin/ls --color=auto
is likely better idea, it will stop ls
from using color codes when not writing directly to terminal (like when piping to next program or writing to a file).
Thank you. I assume this is just a "known thing" and people understand ls + sorting should be done directly via/bin/ls
vs.ls
.
– Josh M.
5 hours ago
4
@JoshM. Well, sorting should be done byls
itself, if possible. Sorting withsort
is parsing, not recommended in general.
– Kamil Maciorowski
5 hours ago
2
@JoshM., rather than using/bin/ls
, change your alias to what Tomak suggested and you will get the better behavior automatically.
– John1024
5 hours ago
1
@JoshM. You can also usels
for an unaliasedls
orcommand ls
for/bin/ls
.
– Freddy
5 hours ago
2
Yes, @Freddy, very true. But, usingalias ls='/bin/ls --color'
is just a recipe for trouble.
– John1024
5 hours ago
add a comment |
Do:
/bin/ls --color > file1
/bin/ls > file2
and compare content, you'll see the difference.
Aliasing ls
to /bin/ls --color=auto
is likely better idea, it will stop ls
from using color codes when not writing directly to terminal (like when piping to next program or writing to a file).
Do:
/bin/ls --color > file1
/bin/ls > file2
and compare content, you'll see the difference.
Aliasing ls
to /bin/ls --color=auto
is likely better idea, it will stop ls
from using color codes when not writing directly to terminal (like when piping to next program or writing to a file).
answered 6 hours ago
TomekTomek
48936
48936
Thank you. I assume this is just a "known thing" and people understand ls + sorting should be done directly via/bin/ls
vs.ls
.
– Josh M.
5 hours ago
4
@JoshM. Well, sorting should be done byls
itself, if possible. Sorting withsort
is parsing, not recommended in general.
– Kamil Maciorowski
5 hours ago
2
@JoshM., rather than using/bin/ls
, change your alias to what Tomak suggested and you will get the better behavior automatically.
– John1024
5 hours ago
1
@JoshM. You can also usels
for an unaliasedls
orcommand ls
for/bin/ls
.
– Freddy
5 hours ago
2
Yes, @Freddy, very true. But, usingalias ls='/bin/ls --color'
is just a recipe for trouble.
– John1024
5 hours ago
add a comment |
Thank you. I assume this is just a "known thing" and people understand ls + sorting should be done directly via/bin/ls
vs.ls
.
– Josh M.
5 hours ago
4
@JoshM. Well, sorting should be done byls
itself, if possible. Sorting withsort
is parsing, not recommended in general.
– Kamil Maciorowski
5 hours ago
2
@JoshM., rather than using/bin/ls
, change your alias to what Tomak suggested and you will get the better behavior automatically.
– John1024
5 hours ago
1
@JoshM. You can also usels
for an unaliasedls
orcommand ls
for/bin/ls
.
– Freddy
5 hours ago
2
Yes, @Freddy, very true. But, usingalias ls='/bin/ls --color'
is just a recipe for trouble.
– John1024
5 hours ago
Thank you. I assume this is just a "known thing" and people understand ls + sorting should be done directly via
/bin/ls
vs. ls
.– Josh M.
5 hours ago
Thank you. I assume this is just a "known thing" and people understand ls + sorting should be done directly via
/bin/ls
vs. ls
.– Josh M.
5 hours ago
4
4
@JoshM. Well, sorting should be done by
ls
itself, if possible. Sorting with sort
is parsing, not recommended in general.– Kamil Maciorowski
5 hours ago
@JoshM. Well, sorting should be done by
ls
itself, if possible. Sorting with sort
is parsing, not recommended in general.– Kamil Maciorowski
5 hours ago
2
2
@JoshM., rather than using
/bin/ls
, change your alias to what Tomak suggested and you will get the better behavior automatically.– John1024
5 hours ago
@JoshM., rather than using
/bin/ls
, change your alias to what Tomak suggested and you will get the better behavior automatically.– John1024
5 hours ago
1
1
@JoshM. You can also use
ls
for an unaliased ls
or command ls
for /bin/ls
.– Freddy
5 hours ago
@JoshM. You can also use
ls
for an unaliased ls
or command ls
for /bin/ls
.– Freddy
5 hours ago
2
2
Yes, @Freddy, very true. But, using
alias ls='/bin/ls --color'
is just a recipe for trouble.– John1024
5 hours ago
Yes, @Freddy, very true. But, using
alias ls='/bin/ls --color'
is just a recipe for trouble.– John1024
5 hours ago
add a comment |
In the sorted colored output ls|sort
, we can see that the last line xyz-0.0.1-1554323568.rpm
is the first line
of the non-colored output. The other lines are sorted equally.
If we have at a look at the colored escape codes (non-sorted), we can see that the first
line starts with a different escape code ^[[0m
. This is causing the wrong order when sorted (^[[01
before ^[[0m
).
$ /bin/ls --color xyz* | cat -A
^[[0m^[[01;31mxyz-0.0.1-1554323568.rpm^[[0m$
^[[01;31mxyz-0.0.1-1554490900.rpm^[[0m$
^[[01;31mxyz-0.0.1-1554745305.rpm^[[0m$
^[[01;31mxyz-0.0.1-1554751021.rpm^[[0m$
^[[01;31mxyz-0.0.1-1555513460.rpm^[[0m$
^[[01;31mxyz-0.0.1-1555951745.rpm^[[0m$
add a comment |
In the sorted colored output ls|sort
, we can see that the last line xyz-0.0.1-1554323568.rpm
is the first line
of the non-colored output. The other lines are sorted equally.
If we have at a look at the colored escape codes (non-sorted), we can see that the first
line starts with a different escape code ^[[0m
. This is causing the wrong order when sorted (^[[01
before ^[[0m
).
$ /bin/ls --color xyz* | cat -A
^[[0m^[[01;31mxyz-0.0.1-1554323568.rpm^[[0m$
^[[01;31mxyz-0.0.1-1554490900.rpm^[[0m$
^[[01;31mxyz-0.0.1-1554745305.rpm^[[0m$
^[[01;31mxyz-0.0.1-1554751021.rpm^[[0m$
^[[01;31mxyz-0.0.1-1555513460.rpm^[[0m$
^[[01;31mxyz-0.0.1-1555951745.rpm^[[0m$
add a comment |
In the sorted colored output ls|sort
, we can see that the last line xyz-0.0.1-1554323568.rpm
is the first line
of the non-colored output. The other lines are sorted equally.
If we have at a look at the colored escape codes (non-sorted), we can see that the first
line starts with a different escape code ^[[0m
. This is causing the wrong order when sorted (^[[01
before ^[[0m
).
$ /bin/ls --color xyz* | cat -A
^[[0m^[[01;31mxyz-0.0.1-1554323568.rpm^[[0m$
^[[01;31mxyz-0.0.1-1554490900.rpm^[[0m$
^[[01;31mxyz-0.0.1-1554745305.rpm^[[0m$
^[[01;31mxyz-0.0.1-1554751021.rpm^[[0m$
^[[01;31mxyz-0.0.1-1555513460.rpm^[[0m$
^[[01;31mxyz-0.0.1-1555951745.rpm^[[0m$
In the sorted colored output ls|sort
, we can see that the last line xyz-0.0.1-1554323568.rpm
is the first line
of the non-colored output. The other lines are sorted equally.
If we have at a look at the colored escape codes (non-sorted), we can see that the first
line starts with a different escape code ^[[0m
. This is causing the wrong order when sorted (^[[01
before ^[[0m
).
$ /bin/ls --color xyz* | cat -A
^[[0m^[[01;31mxyz-0.0.1-1554323568.rpm^[[0m$
^[[01;31mxyz-0.0.1-1554490900.rpm^[[0m$
^[[01;31mxyz-0.0.1-1554745305.rpm^[[0m$
^[[01;31mxyz-0.0.1-1554751021.rpm^[[0m$
^[[01;31mxyz-0.0.1-1555513460.rpm^[[0m$
^[[01;31mxyz-0.0.1-1555951745.rpm^[[0m$
answered 5 hours ago
FreddyFreddy
3535
3535
add a comment |
add a comment |
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%2f1428322%2fbin-ls-sorts-differently-than-just-ls%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