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.
If your shell is
bash, then the best way is to use the
(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.
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.
One other option is to create a simple shell script that does this:
#!/bin/bash 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.
nano history-lookup.sh(or whatever name you want - just end it in
Paste that code in
Press Ctrl+O then Ctrl+X to save and exit.
bash history-lookup.sh NUMBER-OF-ENTRIES. It should spit them out nicely onto the screen.
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.