Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to list the components of the current working directory in a text file

ls -1 > textfile

Output looks fine with more.


However when I view this textfile with emacs, several strange characters appear


Can anyone explain what is going on here?

share|improve this question
look like a end of line issue – Kiwy Feb 13 '14 at 10:07
up vote 6 down vote accepted

Those "strange characters" are escape sequences for coloring the output.

This will print that number in blue color:

echo -e '\033[01;34m1010729039145\033[0m'

See man console_codes for more details.

You can tell ls in which cases it shall output colors:

          colorize the output; WHEN can be 'never', 'auto', or 'always' (the default); more info below

Looks like your ls is actually an alias for ls --color=always. (Type type ls to verify.)

Usually ls --color=auto will do the right thing: It will print colors on screen but no into files.

If you really want the color in your file, your have to decide, if you want to see the actual escape sequences or the interpreted colors.

For example the less command will default to printing the actual sequences, but you can tell it to show the colors instead with the -R option:

          Like -r, but only ANSI "color" escape sequences are output in "raw" form.

Try less textfile vs. less -R textfile.

share|improve this answer

Maybe you have some misconfigurations in emacs, try this:

M-x ansi-color-for-comint-mode-on

Or add this line to your .emacs:

(add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)

You can see this for more details.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.