1

The less command accepts its defaults with an environment variable LESS, so you can

export LESS='-F -g -i -M -R -S -w -X -z-4'

at the beginning of your session.

Is it possible to change the default lines count returned by head and tail in a similar fashion?

An alias is not an option, because it breaks explicit option setting (e.g. in a script):

head -n 15 -5

fails with an error in both GNU and busybox head at least.

  • 1
    What do you mean with "breaks explicit option setting"? What exactly will break if you set something explicitly? – Anthon Mar 3 '14 at 13:12
  • 2
    From the info page for head you can read that using -5 is obsolete: For compatibility 'head' also supports an obsolete option syntax '-COUNTOPTIONS', which is recognized only if it is specified first. At least for head from coreutils. – Anthon Mar 3 '14 at 14:34
  • 1
    Still, old scripts may be using that form and an alias breaks their execution. – malteo Mar 3 '14 at 15:13
5

Since the old style options like -5, +5 are only recognised as the first argument, you could do:

head()
  case $1 in
    ([-+][0-9]*) command head "$@";;
    (*) command head -n 15 "$@"
  esac

That will affect the heads invoked by your current shell.

If you want to affect all head invocations, you'd need to write it as a script that appears first in your $PATH:

mkdir -p ~/bin && cat > ~/bin/head << \EOF &&
#! /bin/sh -
case $1 in
  ([-+][0-9]*) ;;
  (*) set -- -n 15 "$@"
esac
exec /usr/bin/head "$@"
EOF
chmod +x ~/bin/head
PATH=~/bin:$PATH export PATH
1

Use an alias, e.g. alias head="/usr/bin/head -n 15" (no, it will not break any later options).

Use a function, e.g.

head() {
  /usr/bin/head -n 15 "$@"
}

Cobble up your own, as the head(1) info for GNU suggests, i.e., use sed 15q in a script.

  • It breaks it in the case of head -n 15 -2 with both busybox and GNU head and tail at least – Stéphane Chazelas Mar 3 '14 at 14:00
  • 1
    Breaks BSD head and tail shipped with OSX too – malteo Mar 3 '14 at 14:35
0

Using an alias is certainly an option if you are working with head from coreutils version 8.13. That is because

head -n 5 -n 15 file_name

will give you the first 15 lines of file_name (assuming that file has enough lines. The second -n option overrides the first.

So you can create an alias:

alias head="/usr/bin/head -n 5"

to set the default to five and then use:

head -n 15 file_name

The same holds true for tail.

  • It breaks it in the case of head -n 15 -2 with both busybox and GNU head and tail at least. – Stéphane Chazelas Mar 3 '14 at 14:01
  • Breaks BSD head and tail shipped with OSX too – malteo Mar 3 '14 at 14:31
  • 2
    @StephaneChazelas -2 is obsolete syntax according to the head info page. head --help and man help` don't even mention that anymore. – Anthon Mar 3 '14 at 14:31
  • 1
    Yes, but still used in many scripts. GNU once removed support for those but had to bring it back as it was causing outrage – Stéphane Chazelas Mar 3 '14 at 15:05
-1

the best way is to do it "on the fly"

 sudo tail -f /path/to/file

will display the last 10 lines (live) of the mentioned file [10 lines is the default for tail]

you can also display multiple files

sudo tail -f -n 20 /path/to/file1 /path/to/file2

will display two seperate blocks of text each with the last 20 lines of file1 and file 2 respectively. I find this one useful for displaying two live "tails" of logfiles whilst doing some other actions and verifying display.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.