When looking at the content of files, I'd like to automatically cat short files but less those that exceed the screen size. I could use something with wc -l, but is there a better way that maybe also considers the window size / current amount of lines available?

  • 1
    While many tools do this, note that it breaks expectability. I.e. the user cannot known in advance what will happen. I find this annoying for instance in git log which does this exact thing. You never know in advance what you're going to get (do you have to hit q?).
    – bitmask
    Jun 22, 2012 at 19:47
  • 4
    NOTE: In general you should avoid using cat to view files. Typically you should only use cat to send the contents of a file to stdout so it can be piped to another process. The reason to avoid using cat for viewing is that it sends the raw bytes to the terminal, which can cause unexpected terminal commands to be executed (e.g., some users have gotten stuck when a file happened to contain the right bytes to disable the terminal keyboard). You should usually use a pager like less or an editor, which will format special characters for display so they aren't interpreted by the terminal.
    – Chris Page
    Jun 25, 2012 at 6:25
  • 2
    If you're going to use cat to view file contents directly in the terminal, at least use cat -v so it escapes special characters.
    – Chris Page
    Jun 25, 2012 at 6:26
  • @bitmask good point, that's why I asked Is split-screen cating possible? which might be a better solution Jun 25, 2012 at 8:15
  • @ChrisPage My keyboard wasn't killed, but I think I managed to screw up the codepage once... Thanks for pointing cat -v out Jun 25, 2012 at 8:16

2 Answers 2


Maybe 'less -F file_to_read' is the option : it exits less if the window is sufficient to display all the file, and wait on the pager if it is not the case

  • sounds good, but that does not output anything in that case, and the exit codes is the same in both cases Jun 22, 2012 at 9:53
  • I just try 'less -F /etc/passwd' and it display the file correctely (with or without pager). You are right, the return code is 0 in both cases.
    – Dom
    Jun 22, 2012 at 10:44
  • 12
    That command works as requested, but with a minor glitch: it clears the screen after displaying the file. Adding the -X option will stop the clearing, so short files will be displayed like cat does. Unfortunately with -X the screen will not be cleared neither after displaying long, scrolled files.
    – manatwork
    Jun 22, 2012 at 11:12
  • 1
    @manatwork that's great, thanks. Not clearing the screen is actually ok, since that's what would (not) happen if cat were called as well Jun 22, 2012 at 11:50

To give you the formula which involves the wc-based check:

(($(wc -l<input_file)<=$(tput lines))) && echo 'will fit' || echo 'not enough'

There is a $LINES shell variable which can also be used:

(($(wc -l<input_file)<=LINES)) && echo 'will fit' || echo 'not enough'

But $LINES is updated only when at the command prompt. To understand what I mean, run this and resize the terminal window during the sleep:

( sleep 3; echo $LINES; tput lines )
  • Thanks, this has the advantage that it can be tuned to use less already if e.g. 2/3 of the screen were used by cat. Jun 22, 2012 at 11:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .