3

Is it possible to set a fixed number for the "history" command to only show a certain amount of history items like 100, instead of everything from the beginning of time?

2 Answers 2

4

history n prints only n lines of the history. For example:

$ history 4
 2000  type history
 2001  help
 2002  help history
 2003  history 4
$

So we can make an alias in your .bashrc:

alias h="history 100"

From help history:

history: history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg...]
    Display or manipulate the history list.

    Display the history list with line numbers, prefixing each modified
    entry with a `*'.  An argument of N lists only the last N entries.

This is useful if you want to keep a large history set, maybe even an unlimited one. With HISTCONTROL=ignoreboth:erasedups and reverse incremental search, it's a special occasion when you actually run the history command.

7
  • This doesn't seem to be working for me. Do you mean if I type for example "history 100" I should get a history list that displays 100 commands? This didn't work for me, but I'm probably not following you correctly
    – Justin
    Sep 20, 2014 at 18:47
  • Yep, I've got over 300 commands in my history and I'm using bash. When I type "history 100" I get the entire history list of commands (including 'history 100') as if I just simply typed "history" without any number following it
    – Justin
    Sep 20, 2014 at 18:51
  • It appears to. Could you give me an explicit example of you doing this yourself so I can follow along? I think I'm just not fully understanding
    – Justin
    Sep 20, 2014 at 18:56
  • Nice! That makes sense now. I apologize for my confusion, I'm just getting into Linux/Ubuntu. My confusion was partly because I was expecting it to generate a new history list starting at 1 and ending at 100. I didn't realize that it would keep the same numbers from the history list. Thanks!
    – Justin
    Sep 20, 2014 at 19:03
  • @Justin Oh, that's because all your old commands are still there. history only shifts numbers when commands are removed. (For example, if you use the HISTSIZE variable.)
    – muru
    Sep 20, 2014 at 19:05
3

Setting HISTFILESIZE and HISTSIZE you can restrict the size of history command.

For example HISTFILESIZE=100 and HISTSIZE=100. It will restrict history file to store 100 lines and HISTFILE which stores your cureent session command in memory to 100 lines.

When the shell starts up, the history is initialized from the file named by the HISTFILE variable (default ~/.bash_history). The file named by the value of HISTFILE is truncated, if necessary, to contain no more than the number of lines specified by the value of the HISTFILESIZE variable.

You can add HISTFILESIZE=100 and HISTSIZE=100 in your ~/.bashrc file . Change the 100 with the number you want.

Explanation

HISTFILESIZE=10 and HISTSIZE=10

  1. You start your session.
    1. Your HISTFILE (file that stores your bash command history), is truncated to contain HISTFILESIZE=10 lines.
  2. You write 50 lines.
  3. At the end of your 50 commands, only commands 41 to 50 are in your history list, whose size is determined by HISTSIZE=10.
  4. You end your session.
    1. Assuming histappend is not enabled, commands 41 to 50 are saved to your HISTFILE which now has the 10 commands it held at the beginning plus the 10 newly written commands.
    2. Your HISTFILE is truncated to contain HISTFILESIZE=10 lines.
  5. You now have 10 commands in your history - the last 10 that you just typed in the session you just finished.
  6. When you start a new session, you start over at 1 with a HISTFILE of HISTFILESIZE=10.

HISTFILESIZE=10 and HISTSIZE=5

  1. You start your session.
    1. Your HISTFILE (file that stores your bash command history), is truncated to contain at most HISTFILESIZE=10 lines.
  2. You write 50 lines.
  3. At the end of your 50 commands, only commands 46 to 50 are in your history list, whose size is determined by HISTSIZE=5.
  4. You end your session.
    1. Assuming histappend is not enabled, commands 46 to 50 are saved to your HISTFILE which now has the 10 commands it held at the beginning plus the 5 newly written commands.
    2. Your HISTFILE is truncated to contain HISTFILESIZE=10 lines.
  5. You now have 10 commands in your history - 5 from a previous session and the last 5 that you just typed in the session you just finished.
  6. When you start a new session, you start over at 1 with a HISTFILE of HISTFILESIZE=10.

HISTFILESIZE=5 and HISTSIZE=10

  1. You start your session.
    1. Your HISTFILE (file that stores your bash command history), is truncated to contain at most HISTFILESIZE=5 lines.
  2. You write 50 lines.
  3. At the end of your 50 commands, only commands 41 to 50 are in your history list, whose size is determined by HISTSIZE=10.
  4. You end your session.
    1. Assuming histappend is not enabled, commands 41 to 50 are saved to your HISTFILE which now has the 5 commands it held at the beginning plus the 10 newly written commands.
    2. Your HISTFILE is truncated to contain HISTFILESIZE=5 lines.
  5. You now have 5 commands in your history - the last 5 that you just typed in the session you just finished.
  6. When you start a new session, you start over at step 1 with a HISTFILE of HISTFILESIZE=5.

This great explanation from:stackoverflow.com

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .