5

I'm looking for a way to limit the amount of output produced by all command line programs in Linux, and preferably tell me when it is limited.

I'm working over a server which has a lag on the display. Occasionally I will accidentally run a command which outputs a large amount of text to the terminal, such as cat on a large file or ls on a directory with many files. I then have to wait a while for all the output to be printed to the terminal.

So is there a way to automatically pipe all output into a command like head or wc to prevent too much output having to be printed to terminal?

5 Answers 5

3

I don't know about the general case, but for each well-known command (cat, ls, find?) you could do the following:

  • hardlink a copy to the existing utility
  • write a tiny bash function that calls the utility and pipes to head (or wc, or whatever)
  • alias the name of the utility to call your function.

So along these lines (utterly untested):

$ ln `which cat` ~/bin/old_cat

function trunc_cat () {
   `old_cat $@ | head -n 100`
}

alias cat=trunc_cat
Sign up to request clarification or add additional context in comments.

1 Comment

yeah I'd thought of something like that, but was hoping for something more general
1

Making aliases of all your commands would be a good start. Something like

alias lm="ls -al | more"
alias cam="cat $@ | more"

Comments

1

Perhaps using screen could help?

2 Comments

I use byobu, I still have to wait for all the output to display though. Is there something in this I'm unaware of that would help?
Press Ctrl+A Esc and the terminal will freeze at the current location. You can skip to the end with Esc >.
1

this makes me think of bash-completion.

As complete command in bash enables you to specify handler when a program is not found,

what about write your own handler and clear $PATH, in order to execute every command with redirection to a filtering pipe?

#Did not try it myself.

Comments

0

Assuming you're working over a network connection, like ssh, into a remote server then try piping the output of the command to less. That way you can manage and navigate the output from the program on the server better. Use 'j' and 'k' to move up and down per line and 'ctrl-u' and 'ctrl-d' to move 1/2 a page up and down. When you do this only the relevant text (i.e. what fits on the screen) will be transmitted over the network.

1 Comment

I was looking for something more automatic than having to always pipe to less. This also has the problem of opening less for small amounts of output and losing colourisation for e.g. ls.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.