Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit output of all Linux commands

Tags:

linux

bash

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?

like image 252
daniel Avatar asked Dec 01 '11 01:12

daniel


People also ask

How can you limit the output Linux?

The -n option tells head to limit the number of lines of output. Alternatively, to limit the output by number of bytes, the -c option would be used.

How do I limit my ls results?

How do I limit my ls results? The ls | cut -f 1,n file command you suggested would output the first and nth field on each line of text in file , and would completely ignore the output of ls .

What is Ulimit command in Linux?

ulimit is a built-in Linux shell command that allows viewing or limiting system resource amounts that individual users consume. Limiting resource usage is valuable in environments with multiple users and system performance issues.

What is filter in Unix Linux?

In UNIX/Linux, filters are the set of commands that take input from standard input stream i.e. stdin, perform some operations and write output to standard output stream i.e. stdout. The stdin and stdout can be managed as per preferences using redirection and pipes. Common filter commands are: grep, more, sort.


2 Answers

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
like image 110
andrewdotnich Avatar answered Sep 29 '22 12:09

andrewdotnich


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

alias lm="ls -al | more"
alias cam="cat $@ | more"
like image 34
jaypal singh Avatar answered Sep 29 '22 12:09

jaypal singh