Stop ls from coloring filenames when stdout is redirected

A common way to save all filenames in a directory is to do:

ls > filenames

My directory looks like so:

$ls | head
TCGA-A1-A0SN
TCGA-A1-A0SN.xml
TCGA-A2-A04N
TCGA-A2-A04V
TCGA-A2-A0CM
TCGA-A2-A0CM.xml

However when I call “ls > filenames” on my shell (bash, CentOS) it returns a file like so:

^[[0m^[[01;34mTCGA-A1-A0SN^[[0m
^[[0mTCGA-A1-A0SN.xml^[[0m
^[[01;34mTCGA-A2-A04N^[[0m
^[[01;34mTCGA-A2-A04V^[[0m
^[[01;34mTCGA-A2-A0CM^[[0m
^[[0mTCGA-A2-A0CM.xml^[[0m

I would like to know 1) Why this happens, and 2) What I can do to save the filenames in regular text format.

EDIT: product of calling “alias ls” is

$alias ls
alias ls='ls --color="always"'


Source: bash

Leave a Reply