Is there a way to display a list comprised of the last n terminal commands made by a particular user ?


Every user has a .bash_history file, present in his home directory. i.e. /home/<USERNAME>/.bash_history. It stores all the command executed by a particular user.

You can use this file to see the command executed by a user. (You need root privilege to access file of other user.)

tail -n /home/<USERNAME>/.bash_history

Don't forget to replace n with actual number.

  • @g_p Wow, this one is reaaaaaally cool and simple, thanks a lot ! – user43389 Jan 27 '15 at 16:24

If your shell is bash, then the best way is to use the history command:

(HISTFILE=/home/user/.bash_history; history -c; history -r; history n)

While a bit complicated, this provides all the benefits of the history command (such as timestamps, if you have enabled it). The parentheses (()) ensure the commands are run in a subshell and don't affect the current shell.

  • nice trick...+1 for HISTFILE – g_p Jan 27 '15 at 17:04

tail -n /home/userinquestion/.bash_history would seem the obvious answer - assuming they have bash as their shell, of course..

If there is a terminal open, it may not have written commands to the history file yet.

You will need to use su or sudo if the user is not yourself.

  • you are forgetting to address the "last n" ;) – Rinzwind Jan 27 '15 at 16:14
  • Blimey Rinzwind that was quick! – Mark Williams Jan 27 '15 at 16:14
  • guess why I removed my answer ;D – Rinzwind Jan 27 '15 at 16:15

One other option is to create a simple shell script that does this:

tail --lines=$1 /home/$(whoami)/.bash_history

This takes g-P's answer and makes it just a little prettier by doing more stuff automatically.

  1. Run nano history-lookup.sh (or whatever name you want - just end it in .sh)

  2. Paste that code in

  3. Press Ctrl+O then Ctrl+X to save and exit.

  4. Run bash history-lookup.sh NUMBER-OF-ENTRIES. It should spit them out nicely onto the screen.


Please try:

history | tail -n20 > history20.txt

By sending the result to a text file, you can print, review, email, etc. the text file at any time.

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.