Tuesday 29 May 2018

linux - Finding the number of commands in command history


I'm trying to find the number of commands I've executed. At first I thought the command below would work:


wc -l < $HISTFILE

But now I know that the number this command returns doesn't show the number of commands, because a command can be multi-line.


How can I find the number of commands in my command history?



Answer



There's this command called fc that processes the command history list.


According to man fc,



−l (The letter ell.) List the commands rather than invoking an editor on them. The commands shall be written in the sequence indicated by the first and last operands, as affected by −r, with each command preceded by the command number.



we can use fc -l to list the commands previously entered. By default, this command prints the last 16 commands, in the following format:


 [number of command]  [command]

Now if we could get the last command and only print its number, that would be the number of commands in the history file. If we look at man fc again,



−number A negative decimal number representing the command that was executed number of commands previously. For example, −1 is the immediately previous command.



we can see that by specifying -n, we get the last n commands. So the command, fc -l -1 would return the last command.


❯ fc -l -1
3088 man fc

Now that we have that, we can simply print the first column of it, using awk:


❯ fc -l -1 | awk '{print $1}'
3089

No comments:

Post a Comment

Where does Skype save my contact&#39;s avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...