78

I need to view large logs files using a bash shell. I was using less to open the files, but since the lines are too lengthy there is some kind of line/word wrapping going on.

Since the files are Log4J logs, and there is a pattern in the beginning of each line, having lines wrapped makes it difficult to analyze the output, so I started using less -S which chops long lines.

But now I need to use tail -f, and it also line wraps the output. Is it possible to disable line wrap in a bash shell for all the commands?

Note: there is an answer to a different question that mentions the escape sequence echo -ne '\e[?7l', but it seems to not work on bash.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
nunaxe
  • 953
  • 1
  • 7
  • 6
  • 5
    Note that this is unrelated to your choice of shell (bash), but it can depend on your choice of terminal (xterm, Terminal, gnome-terminal, konsole, …). See [What is the exact difference between a 'terminal', a 'shell', a 'tty' and a 'console'?](http://unix.stackexchange.com/q/4126) – Gilles 'SO- stop being evil' Sep 11 '11 at 18:16
  • 4
    As Gilles mentioned, it is a matter of the terminal emulator. For example, in `screen`, there is a "wrap" command. In `xterm`, there is a `-aw` option to prevent automatic line wraps. Likely there are some X resources for other terminal emulators. What emulator are you using? – Arcege Sep 12 '11 at 20:04
  • I am using `gnome-terminal`. – nunaxe Sep 17 '11 at 08:53
  • 1
    @Arcege: Actually, it is `+aw` for `xterm` :) But, this solves the issue for **output** (e.g., on `ls -l` with a bunch on long Windows-ish names from TPB, it works fine), for **input**, when entering a command, you probably still want wrap (or, at least you don't want it the way it is now on `+aw`: try write beyond the "column border" and then backtrack for a huge GFX bug). – Emanuel Berg Nov 09 '12 at 23:37
  • By the way, it seems neither `urxvt` nor `rxvt` can disable line wraps. – Emanuel Berg Nov 09 '12 at 23:50
  • What about "| cut c1-200"? – mathtick Jun 06 '13 at 14:17
  • related https://askubuntu.com/questions/73443/how-to-stop-the-terminal-from-wrapping-lines | https://superuser.com/questions/188865/how-to-make-bash-not-to-wrap-output – Ciro Santilli OurBigBook.com Mar 09 '18 at 06:43

12 Answers12

80

Try:

less -S +F filename

=

less --chop-long-lines +F filename

And then:

  • Press Ctrlc to stop tailing and now you can move left and right using cursor keys.
  • Press Shiftf to resume tailing
  • Press Ctrlc, q to quit

less manual: + If a command line option begins with +, the remainder of that option is taken to be an initial command to less.

For example, +F tells less to scroll forward, and keep trying to read when the end of file is reached

Boris Marcos
  • 911
  • 1
  • 6
  • 3
  • This is great! Been having my terminal span two screens, but since I read this yesterday I just pipe the wide output to `less -S`. Also very useful on the laptop. – Jonatan Öström Dec 16 '18 at 12:41
55

Found a good answer from superuser, that works out of the box for gnome-terminal, and probably for other terminals as well:

setterm -linewrap off

enter image description here

Yuri Ghensev
  • 651
  • 5
  • 5
  • This is `setterm --linewrap off` for me using gnome-terminal with zsh on Ubuntu 18.04. – lindhe Jan 24 '20 at 17:15
  • 6
    how do i scroll horizontally then? – whyer Oct 30 '20 at 11:15
  • 1
    You don't. This is just a simple method for reducing noise. Other answers here deal with horizontal scrolling but are more cumbersome. You could even pipe everything into Vim `| vim -`. – Yuri Ghensev Apr 30 '22 at 22:53
  • If this is in a script, how can I reset the linewrap option off at the end of the script or when the script is killed? – Flimm Apr 11 '23 at 12:32
25

You can temporarily disable line wrapping by entering this command::

tput rmam

To restore line wrapping use this command:

tput smam
WinEunuuchs2Unix
  • 1,163
  • 1
  • 13
  • 27
21

Supposing you have the COLUMNS variable defined, you can execute

tail -f your-app.log | cut -c -$COLUMNS

otherwise substitute $COLUMNS with the columns width of the terminal, as obtained by stty -a.

enzotib
  • 50,671
  • 14
  • 120
  • 105
  • 2
    Another alternative to `$COLUMNS` (is not immediately updated on `SIGWINCH`, only on the next prompt) and `stty -a` (harder to use in script) is `tput cols`. – manatwork Sep 11 '11 at 12:43
  • Thanks enzotib. That works, but it trims the line and we are unable to see the end of the long lines. Is is possible to make `cut` behave like `less -S` where the long line are buffered and we are able to see the complete line using the directional keys? – nunaxe Sep 11 '11 at 15:12
  • Uhm, not that simple. I'm thinking of wired solutions as e.g. two separate `screen` windows each with a `tail -f | cut` or a script emulating in some way the `less` behavior. But do not have a solution, at the moment. – enzotib Sep 11 '11 at 16:16
  • 3
    Side-note: cut only counts bytes; unlike `less -S`, it's going to screw up on coloured text, or anything with ANSI escapes. Might screw up Unicode too. – ELLIOTTCABLE Jun 08 '13 at 20:39
  • Yes, `less -S` is more useful with colored text. You can use `less -S -E` to exit immediately - useful for cutting colored output at $COLUMNS. – blueyed May 13 '15 at 16:42
  • I like this answer best, even if it doesn't adapt to window size changes – Edward Falk Jul 21 '16 at 00:40
  • I aliased `alias cut-cols="cut -c -$(tput cols)"` – sancho.s ReinstateMonicaCellio Dec 04 '16 at 10:55
  • -1, should be: cut -c1-$COLUMNS – thang Jul 25 '17 at 18:38
  • @thang: if the first field of list is missed, it default to 1, read the manpage – enzotib Jul 30 '17 at 10:34
  • cut -c $COLUMNS do not work. cut -c 1-$COLUMNS work – Kirill Mikhailov Jul 28 '21 at 05:22
11

Do you actually need tail -f or would something like less +F do? Since it sounds like you still want an interactive pager, it seems to me it would be much easier to stick with less than to reimplement one yourself.

A final note: have you considered tail -f file | less?

jw013
  • 50,274
  • 9
  • 137
  • 141
  • I made some experiences with `screen` and `xterm` as those emulators have options to prevent line wraps. But there is a big difference between truncating the line and hiding the tail of the line so we can view it if we want. Using `tail -f file | less -S` is not perfect as it seems to disable the `-f` but it is the best solution I found. – nunaxe Sep 17 '11 at 08:56
  • @Boris Marcos 's answer is perhaps better. It uses `less` directly. – adentinger Mar 18 '19 at 21:55
7

Two good answers/examples here

http://www.commandlinefu.com/commands/view/1710/tail-a-log-file-with-long-lines-truncated

tail -f logfile.log | cut -b 1-80

tail -f logfile.log | cut -b -$(tput cols)

One caveat: at least on the built in terminal on my Mac cut does not seem to handle tab characters very well. It seems it displays them with e.g., 8 spaces but just calculates them as 4 space wide or something like that. In other words, if your terminal is 80 characters wide and your output contains several tabs per line you must set the width to maybe 60 or something like that. YMMV.

d-b
  • 1,703
  • 2
  • 17
  • 26
  • `cut` only counts bytes. It doesn't handle multi-byte characters well. Nor does it handle codes that set colours in the terminal. – Flimm Apr 11 '23 at 12:34
3

The terminator (http://software.jessies.org/terminator/) terminal emulator allow to not wrap long lines and has horizontal scrolling (but is written in Java).

gentledevil
  • 272
  • 1
  • 3
  • How do I enable it? It doesn't seem to work by default. – defhlt Aug 29 '12 at 13:26
  • As of this writing, horizontal scroll is on by default. Great, cross-platform recommendation. Thanks! – user7089 Jan 26 '13 at 00:18
  • Please make this answer better by showing how one would enable this feature. I've searched in Preferences and can't seem to locate this option. FWIW, I'm running `terminator v1.91`. – AnthonyK Jan 18 '19 at 23:28
  • 3
    @AnthonyK There are two unrelated terminal emulators sharing the name `terminator`, [this one](https://github.com/software-jessies-org/jessies/wiki/Terminator) which gentledevil is referring to and [that one](https://launchpad.net/terminator) which you are using. – jlliagre Feb 11 '19 at 01:06
3

There are a lot of comments which stray from the question. OP's question was

But now I need to use tail -f, and it also line wraps the output. Is it possible to disable line wrap in a bash shell for all the commands?

Some comments were made about the autowrap feature, stating that not all terminals have it. Perhaps. But (aside from terminator, whose developers document no terminal description) all of the examples given were xterm, rxvt and some look-alike or descendent. Those are all related.

The +aw option in xterm corresponds to the autoWrap resource. Consulting the manual, it says that

   autoWrap (class AutoWrap)
           Specifies  whether  or  not  auto-wraparound should be enabled.
           This is the same as the VT102 DECAWM.  The default is "true".

and in XTerm Control Sequences it says

CSI ? Pm h
          ...
          DEC Private Mode Set (DECSET).
            Ps = 7  -> Wraparound Mode (DECAWM).

which certainly does not "depend on your choice of terminal", since any terminal with VT100-compatibility supports the feature. xterm and rxvt do this, for example. The others do as well.

Whether the feature would be useful to OP is debatable. Suppressing line-wrapping is only one aspect of the problem:

  • The shell knows the width of the terminal—but that can be overridden by setting COLUMNS to a "large" value.
  • Of course that means that applications will spend a lot of time writing on the right-margin (and some, getting it wrong, will start a new line anyway).
  • OP probably assumed that the application would scroll left/right to make the wide terminal usable. (terminator does this — partly — but its other drawbacks cancel that out, except for those who only use the terminal for cat'ing a logfile to the screen).
  • what OP actually is looking for is a set of tools that can be told to disable line-wrap, especially for viewing logfiles. If the terminal works well enough for general use, it is irrelevant to the choice of tools which one uses within the terminal.

There are pagers which can do what is needed, e.g., multitail which lists in its features

Line wrapping can be switched off, after that one can scroll to the left/right with the cursor keys

Being ncurses-based, it should work on any of the cited terminals.

Thomas Dickey
  • 75,040
  • 9
  • 171
  • 268
2

There are a few examples on this page piping tail with -f ... from my tests it doesn't work properly, if you really need piping (eg. if you need to pipe something more, like grep) you may use something like:

watch -n 1 'tail -n $(($LINES-3)) file.log | cut -c -$COLUMNS'

not the best for performance probably but works... else if no extra pipping required you may use something different like:

less -S +F file.log
nars
  • 121
  • 3
1

Use below options with less. It will disable word wrap and preserve text colors, if specified.

less -SR +F filename

Kamal Nayan
  • 141
  • 1
  • 7
-1

vi

inside vi type

:set nowrap

I beleive there is also a plugin for vi that will give you tail like behaviour.

-3

If you really want to see the full lines you can copy and paste the text into a text editor like Atom or Notepad++ and disable line wraps in there.