/bin/ls sorts differently than just lsWhy does OS X Lion's terminal treat the PS1 prompt codes differently than Snow Leopard?Associating a renamed executable with our application fails on command line in WindowsWindows “tree” command sorts randomlyWhat is the different between “sudo su” and “sudo bash”?Kali Linux: Renaming files in binFile sorting confusionwhat is `ssh-agent bin/bash` and why I have to `ssh-add` everytimeusing awk on only files which START with 'xyz'SSH executes the command differentlyWhy does symlink to VLC break but but .bash_profile alias works?
How much cash can I safely carry into the USA and avoid civil forfeiture?
Why didn't the Space Shuttle bounce back into space as many times as possible so as to lose a lot of kinetic energy up there?
How bug prioritization works in agile projects vs non agile
Combinatorics problem, right solution?
Drawing a german abacus as in the books of Adam Ries
What is the unit of time_lock_delta in LND?
Extracting Dirichlet series coefficients
A Note on N!
Von Neumann Extractor - Which bit is retained?
How to not starve gigantic beasts
Creating a chemical industry from a medieval tech level without petroleum
What is the term for a person whose job is to place products on shelves in stores?
How can I wire a 9-position switch so that each position turns on one more LED than the one before?
Can a stored procedure reference the database in which it is stored?
Why did Rep. Omar conclude her criticism of US troops with the phrase "NotTodaySatan"?
A Paper Record is What I Hamper
A faster way to compute the largest prime factor
How do I reattach a shelf to the wall when it ripped out of the wall?
Do I need to watch Ant-Man and the Wasp and Captain Marvel before watching Avengers: Endgame?
How important is it that $TERM is correct?
std::unique_ptr of base class holding reference of derived class does not show warning in gcc compiler while naked pointer shows it. Why?
"The cow" OR "a cow" OR "cows" in this context
Cayley's Matrix Notation
Is Electric Central Heating worth it if using Solar Panels?
/bin/ls sorts differently than just ls
Why does OS X Lion's terminal treat the PS1 prompt codes differently than Snow Leopard?Associating a renamed executable with our application fails on command line in WindowsWindows “tree” command sorts randomlyWhat is the different between “sudo su” and “sudo bash”?Kali Linux: Renaming files in binFile sorting confusionwhat is `ssh-agent bin/bash` and why I have to `ssh-add` everytimeusing awk on only files which START with 'xyz'SSH executes the command differentlyWhy does symlink to VLC break but but .bash_profile alias works?
.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
1
To sort the output ofls
, in many cases, you may found useful to specify directly the-v
option:ls -v
. Better to avoid the deprecated and risky parsing of its output... In many modern distributions the alias ofls
is directly set to/bin/ls --color=auto
, as they suggest you in an answer.
– Hastur
2 days ago
1
The thing you're missing is how ANSI colour codes are signalled in a terminal. I'm not sure if it's constant or varies between terminals, but for me red can be triggered by printing33[91m
. Once you imagine the colour codes that are output byls
(normally consumed by the terminal), you can see why your sort would break (and then, the terminal strips them out so you can't see why the sort broke). I'm guessing the results were grouped by colour, then sorted by name?
– Basic
2 days ago
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 Apr 22 at 18:31
Josh M.Josh M.
75051329
75051329
1
To sort the output ofls
, in many cases, you may found useful to specify directly the-v
option:ls -v
. Better to avoid the deprecated and risky parsing of its output... In many modern distributions the alias ofls
is directly set to/bin/ls --color=auto
, as they suggest you in an answer.
– Hastur
2 days ago
1
The thing you're missing is how ANSI colour codes are signalled in a terminal. I'm not sure if it's constant or varies between terminals, but for me red can be triggered by printing33[91m
. Once you imagine the colour codes that are output byls
(normally consumed by the terminal), you can see why your sort would break (and then, the terminal strips them out so you can't see why the sort broke). I'm guessing the results were grouped by colour, then sorted by name?
– Basic
2 days ago
add a comment |
1
To sort the output ofls
, in many cases, you may found useful to specify directly the-v
option:ls -v
. Better to avoid the deprecated and risky parsing of its output... In many modern distributions the alias ofls
is directly set to/bin/ls --color=auto
, as they suggest you in an answer.
– Hastur
2 days ago
1
The thing you're missing is how ANSI colour codes are signalled in a terminal. I'm not sure if it's constant or varies between terminals, but for me red can be triggered by printing33[91m
. Once you imagine the colour codes that are output byls
(normally consumed by the terminal), you can see why your sort would break (and then, the terminal strips them out so you can't see why the sort broke). I'm guessing the results were grouped by colour, then sorted by name?
– Basic
2 days ago
1
1
To sort the output of
ls
, in many cases, you may found useful to specify directly the -v
option: ls -v
. Better to avoid the deprecated and risky parsing of its output... In many modern distributions the alias of ls
is directly set to /bin/ls --color=auto
, as they suggest you in an answer.– Hastur
2 days ago
To sort the output of
ls
, in many cases, you may found useful to specify directly the -v
option: ls -v
. Better to avoid the deprecated and risky parsing of its output... In many modern distributions the alias of ls
is directly set to /bin/ls --color=auto
, as they suggest you in an answer.– Hastur
2 days ago
1
1
The thing you're missing is how ANSI colour codes are signalled in a terminal. I'm not sure if it's constant or varies between terminals, but for me red can be triggered by printing
33[91m
. Once you imagine the colour codes that are output by ls
(normally consumed by the terminal), you can see why your sort would break (and then, the terminal strips them out so you can't see why the sort broke). I'm guessing the results were grouped by colour, then sorted by name?– Basic
2 days ago
The thing you're missing is how ANSI colour codes are signalled in a terminal. I'm not sure if it's constant or varies between terminals, but for me red can be triggered by printing
33[91m
. Once you imagine the colour codes that are output by ls
(normally consumed by the terminal), you can see why your sort would break (and then, the terminal strips them out so you can't see why the sort broke). I'm guessing the results were grouped by colour, then sorted by name?– Basic
2 days ago
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).
15
@JoshM. Well, sorting should be done byls
itself, if possible. Sorting withsort
is parsing, not recommended in general.
– Kamil Maciorowski
Apr 22 at 19:43
6
@JoshM., rather than using/bin/ls
, change your alias to what Tomak suggested and you will get the better behavior automatically.
– John1024
Apr 22 at 19:46
5
@JoshM. You can also usels
for an unaliasedls
orcommand ls
for/bin/ls
.
– Freddy
Apr 22 at 19:58
10
Yes, @Freddy, very true. But, usingalias ls='/bin/ls --color'
is just a recipe for trouble.
– John1024
Apr 22 at 20:00
1
How does this answer answer the OP's question?
– pts
yesterday
|
show 2 more comments
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).
15
@JoshM. Well, sorting should be done byls
itself, if possible. Sorting withsort
is parsing, not recommended in general.
– Kamil Maciorowski
Apr 22 at 19:43
6
@JoshM., rather than using/bin/ls
, change your alias to what Tomak suggested and you will get the better behavior automatically.
– John1024
Apr 22 at 19:46
5
@JoshM. You can also usels
for an unaliasedls
orcommand ls
for/bin/ls
.
– Freddy
Apr 22 at 19:58
10
Yes, @Freddy, very true. But, usingalias ls='/bin/ls --color'
is just a recipe for trouble.
– John1024
Apr 22 at 20:00
1
How does this answer answer the OP's question?
– pts
yesterday
|
show 2 more comments
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).
15
@JoshM. Well, sorting should be done byls
itself, if possible. Sorting withsort
is parsing, not recommended in general.
– Kamil Maciorowski
Apr 22 at 19:43
6
@JoshM., rather than using/bin/ls
, change your alias to what Tomak suggested and you will get the better behavior automatically.
– John1024
Apr 22 at 19:46
5
@JoshM. You can also usels
for an unaliasedls
orcommand ls
for/bin/ls
.
– Freddy
Apr 22 at 19:58
10
Yes, @Freddy, very true. But, usingalias ls='/bin/ls --color'
is just a recipe for trouble.
– John1024
Apr 22 at 20:00
1
How does this answer answer the OP's question?
– pts
yesterday
|
show 2 more comments
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 Apr 22 at 18:44
TomekTomek
62956
62956
15
@JoshM. Well, sorting should be done byls
itself, if possible. Sorting withsort
is parsing, not recommended in general.
– Kamil Maciorowski
Apr 22 at 19:43
6
@JoshM., rather than using/bin/ls
, change your alias to what Tomak suggested and you will get the better behavior automatically.
– John1024
Apr 22 at 19:46
5
@JoshM. You can also usels
for an unaliasedls
orcommand ls
for/bin/ls
.
– Freddy
Apr 22 at 19:58
10
Yes, @Freddy, very true. But, usingalias ls='/bin/ls --color'
is just a recipe for trouble.
– John1024
Apr 22 at 20:00
1
How does this answer answer the OP's question?
– pts
yesterday
|
show 2 more comments
15
@JoshM. Well, sorting should be done byls
itself, if possible. Sorting withsort
is parsing, not recommended in general.
– Kamil Maciorowski
Apr 22 at 19:43
6
@JoshM., rather than using/bin/ls
, change your alias to what Tomak suggested and you will get the better behavior automatically.
– John1024
Apr 22 at 19:46
5
@JoshM. You can also usels
for an unaliasedls
orcommand ls
for/bin/ls
.
– Freddy
Apr 22 at 19:58
10
Yes, @Freddy, very true. But, usingalias ls='/bin/ls --color'
is just a recipe for trouble.
– John1024
Apr 22 at 20:00
1
How does this answer answer the OP's question?
– pts
yesterday
15
15
@JoshM. Well, sorting should be done by
ls
itself, if possible. Sorting with sort
is parsing, not recommended in general.– Kamil Maciorowski
Apr 22 at 19:43
@JoshM. Well, sorting should be done by
ls
itself, if possible. Sorting with sort
is parsing, not recommended in general.– Kamil Maciorowski
Apr 22 at 19:43
6
6
@JoshM., rather than using
/bin/ls
, change your alias to what Tomak suggested and you will get the better behavior automatically.– John1024
Apr 22 at 19:46
@JoshM., rather than using
/bin/ls
, change your alias to what Tomak suggested and you will get the better behavior automatically.– John1024
Apr 22 at 19:46
5
5
@JoshM. You can also use
ls
for an unaliased ls
or command ls
for /bin/ls
.– Freddy
Apr 22 at 19:58
@JoshM. You can also use
ls
for an unaliased ls
or command ls
for /bin/ls
.– Freddy
Apr 22 at 19:58
10
10
Yes, @Freddy, very true. But, using
alias ls='/bin/ls --color'
is just a recipe for trouble.– John1024
Apr 22 at 20:00
Yes, @Freddy, very true. But, using
alias ls='/bin/ls --color'
is just a recipe for trouble.– John1024
Apr 22 at 20:00
1
1
How does this answer answer the OP's question?
– pts
yesterday
How does this answer answer the OP's question?
– pts
yesterday
|
show 2 more comments
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 Apr 22 at 19:49
FreddyFreddy
59019
59019
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
1
To sort the output of
ls
, in many cases, you may found useful to specify directly the-v
option:ls -v
. Better to avoid the deprecated and risky parsing of its output... In many modern distributions the alias ofls
is directly set to/bin/ls --color=auto
, as they suggest you in an answer.– Hastur
2 days ago
1
The thing you're missing is how ANSI colour codes are signalled in a terminal. I'm not sure if it's constant or varies between terminals, but for me red can be triggered by printing
33[91m
. Once you imagine the colour codes that are output byls
(normally consumed by the terminal), you can see why your sort would break (and then, the terminal strips them out so you can't see why the sort broke). I'm guessing the results were grouped by colour, then sorted by name?– Basic
2 days ago