I would like to create a log of all commands typed in terminal

I would like to have a log of all command that are typed on the terminal.

I have check "script" but this generates a file with the commands and the responds all together. Is there a way to have the commands only?


Solution 1:

If you are using the defaullt shells all the commands are already logged to $HOME/.bash_history . There are several environment variables which affect the history keeping, you can read about those with:

info bash
Type: / HIST

If you want to apply the setting to all users edit /etc/profile.

Example:

export HISTFILESIZE=5000

Solution 2:

Everything a user types into the terminal by hand is saved into ~/.bash_history, where ~ is shorthand for the currenly logged in user's home directory. Note also that files beginning with a . are hidden; in Nautilus, you can show them by hitting Ctrl+H.

Take a look at it by typing

cat .bash_history

To view your history, annotated with sequential numbers, type

history

You can execute commands you see there again by typing !2129, for example, which would execute command number 2129 as shown by the history command.

Note that the history command shows you up to the second logs, wheras .bash_history is only saved after you log off from the terminal.

Little piece of interesting information:

  • Commands that begin with a space are not saved to .bash_history. If you run a command like rm -R mydir/, you may want to prefix it with a space to prevent accidentally running it again by hitting the up-arrow.

As Joāo says, the size of the saved history can be controlled with export HISTFILESIZE=5000.