The reason is that ls
always colorizes its output even if it is connected to a terminal. From man ls
:
--color[=WHEN]
colorize the output. WHEN defaults to 'always' or can be
'never' or 'auto'. More info below
Many other tools such as grep
do not retain colors when standard output is terminal but for some reasons ls
was designed to act differently. Colorizing output is achieved by using ANSI escape codes that are interpreted by your terminal. Redirect ls
output to a file and open it in editor:
$ ls --color -lhaF1 > /tmp/RESULT
$ less /tmp/RESULT
total 12K
drwxr-xr-x 3 ja users 4.0K Feb 2 09:47 ESC[0mESC[01;34m.ESC[0m/
drwxrwxrwt 12 root root 4.0K Feb 2 09:51 ESC[30;42m..ESC[0m/
drwxr-xr-x 2 ja users 4.0K Feb 2 09:47 ESC[01;34m.invisibleESC[0m/
-rw-r--r-- 1 ja users 0 Feb 2 08:35 a|a
If you have $LESS
variable set you probably need to unset it before running less
in order to see raw escape codes instead of color. So, to sum up, when using --color
what grep
gets is not what you see but a bunch of escape codes together with it. To fix it either don't use --colors
at all or set it to --auto
:
$ ls --color=auto -lhaF1 | grep -E '^d.*[0-9]{2}:[0-9]{2} \.'
ls ...
? – Anthon Feb 02 '16 at 08:53--colors
in second code – danielr1996 Feb 02 '16 at 09:11ls
is at/bin/ls
. – Skaperen May 18 '22 at 00:55