Limit output of all Linux commands
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
How to limit terminal output of a bash command in a script
The grep
option you're looking for is -o
, or --only-matching
in its long form. It outputs only text that matches the search you gave it, and nothing else. For example:
iwconfig wlan0 | grep -o 'Mode:[^ ]*'
outputs Mode:Managed
on my machine. The single quotes are necessary so that the shell won't try to interpret the [
, ]
and *
characters (with double quotes, if you happened to have a file with precisely the wrong name in your current directory, the shell might wrongly expand your parameter to the name of that file). The regular expression inside the single quotes means "the text Mode:
, followed by any number of non-space characters", which is exactly what you were looking for.
How do I limit the results of the command find in bash?
You can do this:
find . -name "file2015-0*" | head -400 | xargs -I filename mv filename ..
If you want to simulate what it does use echo
:
find . -name "file2015-0*" | head -400 | xargs -I filename echo mv filename ..
Bash command line and input limit
The limit for the length of a command line is not imposed by the shell, but by the operating system. This limit is usually in the range of hundred kilobytes. POSIX denotes this limit ARG_MAX
and on POSIX conformant systems you can query it with
$ getconf ARG_MAX # Get argument limit in bytes
E.g. on Cygwin this is 32000, and on the different BSDs and Linux systems I use it is anywhere from 131072 to 2621440.
If you need to process a list of files exceeding this limit, you might want to look at the xargs
utility, which calls a program repeatedly with a subset of arguments not exceeding ARG_MAX
.
To answer your specific question, yes, it is possible to attempt to run a command with too long an argument list. The shell will error with a message along "argument list too long".
Note that the input to a program (as read on stdin or any other file descriptor) is not limited (only by available program resources). So if your shell script reads a string into a variable, you are not restricted by ARG_MAX
. The restriction also does not apply to shell-builtins.
Linux: How to not limit output from top based on screen width
Try setting COLUMNS
environment variable:
$ COLUMNS=10000 top
How to limit the number of results from find?
GNU head has an option called -z for changing the line terminator to NUL, which can be used for this task as shown below.
find -name '*.jpg' -print0 \
| head -z -n 50 \
| xargs -0 cp -t /destination
Limit top command to only display top X processes on command line
> top
then, press n
to set maximum tasks displayed.
When operating top, one of the most important key is help (h
or ?
) to see the available options (n
is given in help).
UPDATE (after the the comment):
PERSONAL Configuration File
might help for the batch mode. Run top
then set the maximum tasks displayed with n
and use the W
interactive command to create or update the configuration file. top
will be ran according to the configuration file next time.
how do I limit the number of history items in bash?
This is controlled by the HISTSIZE and HISTFILESIZE built-in shell variables. Quoting from the documentation in man bash
:
HISTSIZE
- The number of commands to remember in the command history (see HISTORY below). If the value is 0, commands are not saved in the history list. Numeric values less than zero result in every command
being saved on the history list (there is no limit). The shell sets the default value to 500 after reading any startup files.
HISTFILESIZE
- The maximum number of lines contained in the history file. When this variable is assigned a value, the history file is truncated, if necessary, to contain no more than that number of lines by removing
the oldest entries. The history file is also truncated to this size after writing it when a shell exits. If the value is 0, the history file is truncated to zero size. Non-numeric values and numeric
values less than zero inhibit truncation. The shell sets the default value to the value of HISTSIZE after reading any startup files.
~/.bashrc
is an appropriate place to set these values. They don't need to be exported, since they're internal to the shell:
HISTFILESIZE=25
...will do fine, no export
needed.
Related Topics
How to Create a Bash Variable Like $Random
Linux Perf Record: Difference Between Count (-C) and Frequency (-F) Options
Displaying or Redirecting a Shell's Job Control Messages
Where Is $Path Set? Specifically Where Is My MAC Port Path Being Set
How to Prevent an X Window from Receiving User Input
Elegant Way to Set Base Address of Elf Image with Linux Binutils
How to Set Firefox Binary Path of Firefox in Selenium in Linux
How to Format a String in Bash Shell
How to to Delete a Line Given with a Variable in Sed
Overhead of Supporting Floating Point Arithmetic Inside the Linux Kernel
Get and Use a Password with Special Characters in Bash Shell
Sched_Fifo Higher Priority Thread Is Getting Preempted by the Sched_Fifo Lower Priority Thread