限制所有Linux命令的输出

时间:2022-10-02 16:58:22

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.

我正在寻找一种方法来限制Linux中所有命令行程序产生的输出量,最好告诉我何时它是有限的。

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.

我在服务器上工作,显示器有滞后。偶尔我会不小心运行一个向终端输出大量文本的命令,例如大文件上的cat或者有很多文件的目录上的ls。然后我必须等待一段时间才能将所有输出打印到终端。

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?

那么有没有办法自动将所有输出传输到head或wc等命令中,以防止必须将多少输出打印到终端?

5 个解决方案

#1


3  

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

我不知道一般情况,但对于每个众所周知的命令(cat,ls,find?),您可以执行以下操作:

  • hardlink a copy to the existing utility
  • 将副本硬链接到现有实用程序

  • write a tiny bash function that calls the utility and pipes to head (or wc, or whatever)
  • 写一个小的bash函数,调用实用程序和管道头(或wc,或其他)

  • 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

#2


1  

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

制作所有命令的别名将是一个良好的开端。就像是

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

#3


1  

Perhaps using screen could help?

也许使用屏幕可以帮助?

#4


1  

this makes me think of bash-completion.

这让我想到了bash-completion。

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

由于bash中的完整命令允许您在找不到程序时指定处理程序,

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

如何编写自己的处理程序并清除$ PATH,以便通过重定向执行每个命令到过滤管道?

#Did not try it myself.

#Did不要自己尝试。

#5


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.

假设您正在通过网络连接(如ssh)进入远程服务器,然后尝试将命令的输出管道化为less。这样,您可以更好地管理和导航服务器上的程序输出。使用'j'和'k'每行上下移动,'ctrl-u'和'ctrl-d'上下移动1/2页。当您这样做时,只有相关的文本(即适合屏幕的内容)将通过网络传输。

#1


3  

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

我不知道一般情况,但对于每个众所周知的命令(cat,ls,find?),您可以执行以下操作:

  • hardlink a copy to the existing utility
  • 将副本硬链接到现有实用程序

  • write a tiny bash function that calls the utility and pipes to head (or wc, or whatever)
  • 写一个小的bash函数,调用实用程序和管道头(或wc,或其他)

  • 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

#2


1  

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

制作所有命令的别名将是一个良好的开端。就像是

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

#3


1  

Perhaps using screen could help?

也许使用屏幕可以帮助?

#4


1  

this makes me think of bash-completion.

这让我想到了bash-completion。

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

由于bash中的完整命令允许您在找不到程序时指定处理程序,

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

如何编写自己的处理程序并清除$ PATH,以便通过重定向执行每个命令到过滤管道?

#Did not try it myself.

#Did不要自己尝试。

#5


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.

假设您正在通过网络连接(如ssh)进入远程服务器,然后尝试将命令的输出管道化为less。这样,您可以更好地管理和导航服务器上的程序输出。使用'j'和'k'每行上下移动,'ctrl-u'和'ctrl-d'上下移动1/2页。当您这样做时,只有相关的文本(即适合屏幕的内容)将通过网络传输。