LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Outputting all history (https://www.linuxquestions.org/questions/linux-newbie-8/outputting-all-history-4175502292/)

Mr. Alex 04-19-2014 12:30 PM

Outputting all history
 
Hi guys. http://www.tldp.org/LDP/GNU-Linux-To...html/x1712.htm says

Quote:

This log is called the “history”. To access it type:

history n

This will only list the last n commands. Type “history” (without options) to see the the entire history list.
But it doesn't work for me. When I type in "history" I get just a small part of ~/.zsh_history file. I can't find how to output all history. Must be easy, but it doesn't work for me for some reason. Help please.

tronayne 04-19-2014 12:51 PM

Are you using BASH, KornShell or the Z Shell? There is an environment variable you can set for either of those, HISTSIZE; e.g., in your ~/.profile file add
Code:

export HISTSIZE=1000
(the default value is 500 in BASH and KornShell, don't know what it is in Z Shell).

Don't have a .profile in your home directory? Create one; it's a text file, it should not be executable, just a plain old text file.

It is executed once when you log in to set the environment variable (so, you need to log out and log back in again to have it take effect).

Hope this helps some.

jpollard 04-19-2014 01:10 PM

The history log file is a rotating buffer of commands. It doesn't hold every command - only the N commands specified by HISTSIZE. I believe the default is 500.

Also note - on login the contents are whatever was in there earlier, so that could include yesterdays entries as well.

Mr. Alex 04-20-2014 06:11 AM

tronayne, I use zsh.

Code:

$ cat ~/.zshrc | head -n 3
export HISTFILE=~/.zsh_history
export HISTSIZE=1000
export SAVEHIST=1000

So if follow your logic I should get list of 1000 commands when I type in "history". But I get only a portion of what I have in ~/.zsh_history.

tronayne 04-20-2014 08:01 AM

Quote:

Originally Posted by Mr. Alex (Post 5155919)
So if follow your logic I should get list of 1000 commands when I type in "history". But I get only a portion of what I have in ~/.zsh_history.

OK, you can alias the history command to, say, the size of the screen you're working in.

There are two environment variables, COLUMNS and LINES that may or may not be set; those two variables can be used by a number of terminal-type utilities and it doesn't hurt to set them (in .profile or .zsh, which I am not familiar with, at log in:
Code:

# Use a common screen size to start
export COLUMNS=80
export LINES=40
# Screen is probably larger, so make COLUMNS and LINES the screen size
eval `resize`

The resize is used to "set environment and terminal settings to current xterm window size" (see the manual page).

Now, we're part way there; set up an alias that will show you a screen-size list of history:
Code:

alias hi='history -${LINES}'
Then, just type hi and get a screen-full.

Want more?
Code:

history -250 | pg (or more or less).
Now, that presupposes that your history file is not wiped out when you exit the terminal window (some shell programs do that, you only get current session history, I think C-Shell does that, maybe BASH, too, but I don't use BASH and don't know). If that is happening the is probably a way to shut it off and I'd look at the manual page to find out how. KornShell does not wipe it out and I always have 1,000 lines of history available over multiple sessions; YMMV.

Hope this helps some.

Mr. Alex 04-21-2014 09:02 AM

Tronayne, thanks for your advices. One more strange thing. "history n" doesn't actually output last n commands. The n here means "since what command to output the list to the last command" as I can understand from the behaviour of my shell. If I type in "history 20" it outputs all commands starting from 20'th command. "history 1", as it turned out, outputs all. Is it normal? Web manuals didn't mention it.

tronayne 04-21-2014 09:28 AM

That's normal behavior.

On my system (KornShell)
Code:

hisotry 1
displays all 1,000 lines in ~/.sh_history; however
Code:

history -50
produces the last 50 history entries. Not especially intuitive.

The "trick" is the - in front of the 50.

There is a manual page for history, it's a GNU utility (and, of course, has a gazillion options). You might find it interesting.

Hope this helps some.


All times are GMT -5. The time now is 08:30 AM.