将 man 重定向到一个文件,它与控制台中的文本不相同

Redirecting man to a file it is not identical to the text in the console

我正在尝试打印 ls 的手册页,但在我的文件中得到了包含重复字符的输出。我对 bash 比较陌生,我不知道从哪里开始解决这个问题。 这是我输入的命令

man ls | cat > file.txt

我期待终端中的输出

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.

但是我在文件中得到的输出是这样的

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

是什么让它这样做,我怎样才能获得可读文本的手册页?

一些系统有一个 man 程序,它会注意它是将输出发送到终端还是管道,并且在每种情况下表现不同。

例如,在 ubuntu linux 上,man man 有一个选项:

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.

在你的例子中,似乎 man 在将输出发送到管道时 not 表现不同。

可能有一个选项可以打开您正在寻找的行为,但从输出中去除不需要的字符可能更简单。一种常见的方法是使用 col:

man ls | col -bx > file.txt