0

I am trying to print the man page for ls and I am getting output in my file with repeated characters. I am relatively new to bash and I dont know where to start with this issue. This is the command I typed

man ls | cat > file.txt

I expected output like in the terminal

DESCRIPTION
     For each operand that names a file of a type other than directory, ls displays its
     name as well as any requested, associated information.  For each operand that names a
     file of type directory, ls displays the names of files contained within that direc-
     tory, as well as any requested, associated information.

     If no operands are given, the contents of the current directory are displayed.  If
     more than one operand is given, non-directory operands are displayed first; directory
     and non-directory operands are sorted separately and in lexicographical order.

     The following options are available:

     -@      Display extended attribute keys and sizes in long (-l) output.

     -1      (The numeric digit ``one''.)  Force output to be one entry per line.  This is
             the default when output is not to a terminal.

     -A      List all entries except for . and ...  Always set for the super-user.

     -a      Include directory entries whose names begin with a dot (.).

     -B      Force printing of non-printable characters (as defined by ctype(3) and cur-
             rent locale settings) in file names as \xxx, where xxx is the numeric value
             of the character in octal.

     -b      As -B, but use C escape codes whenever possible.

     -C      Force multi-column output; this is the default when output is to a terminal.

But what I got as output in my file was like this

DDEESSCCRRIIPPTTIIOONN
     For each operand that names a _f_i_l_e of a type other than directory, llss
     displays its name as well as any requested, associated information.  For
     each operand that names a _f_i_l_e of type directory, llss displays the names
     of files contained within that directory, as well as any requested, asso-
     ciated information.

     If no operands are given, the contents of the current directory are dis-
     played.  If more than one operand is given, non-directory operands are
     displayed first; directory and non-directory operands are sorted sepa-
     rately and in lexicographical order.

     The following options are available:

     --@@      Display extended attribute keys and sizes in long (--ll) output.

     --11      (The numeric digit ``one''.)  Force output to be one entry per
             line.  This is the default when output is not to a terminal.

     --AA      List all entries except for _. and _._..  Always set for the super-
             user.

     --aa      Include directory entries whose names begin with a dot (_.).

     --BB      Force printing of non-printable characters (as defined by
             ctype(3) and current locale settings) in file names as \_x_x_x,
             where _x_x_x is the numeric value of the character in octal.

     --bb      As --BB, but use C escape codes whenever possible.

     --CC      Force multi-column output; this is the default when output is to
             a terminal.

     --cc      Use time when file status was last changed for sorting (--tt) or

What would make it do this and how can I get the man page in readable text?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441

1 Answers1

2

Some systems have a man program which notices whether it is sending output to the terminal or to a pipe and behaves differently in each case.

For example, on ubuntu linux, man man has an option:

MAN_KEEP_FORMATTING
    Normally,  when output is not being directed to a terminal (such
    as to a file or a pipe), formatting characters are discarded  to
    make  it  easier to read the result without special tools.  How-
    ever, if $MAN_KEEP_FORMATTING is set  to  any  non-empty  value,
    these  formatting  characters  are retained.  This may be useful
    for wrappers around man that can  interpret  formatting  charac-
    ters.

In your case, it seems that man does not behave differently when sending output to a pipe.

There may be an option to turn on the behaviour you are looking for, but it may be simpler just to strip the unwanted characters out of the output. A common method is to use col:

man ls | col -bx > file.txt
jhnc
  • 11,310
  • 1
  • 9
  • 26
  • This is correct, though it's probably worth explicitly pointing out the problem in the OP's case: (s)he thinks the problem is that `man` is giving different output, but the real problem is that it's giving the *same* output (and the OP is reading the file using some mechanism that doesn't support that style of output). – ruakh Feb 13 '19 at 18:20