Support >
  About independent server >
  Share bash history command extraction method in Linux
Share bash history command extraction method in Linux
Time : 2024-12-13 14:31:19
Edit : Jtti

In daily work, the history command is often used to view the history of commands to obtain information about the commands executed by users. Let's see how to use the history command to extract commands that users execute in the bash shell. By default, date and time stamps are not displayed when the history command is executed, but the bash shell provides command-line interface tools for editing user command history.

See the complete list of last executed commands and their actions:

history

List all commands with date and time stamps:

export HISTTIMEFORMAT="%F %T "

Use the history command to list the respective date and time stamps for all command machines:

history

A breakdown of the format placeholders used HISTTIMEFORMAT='%F %T ':

%F: This represents the full date in the format YYYY-MM-DD.

%T: This represents the time in the HH:MM:SS format.

To ignore a command in the history, you can set it by using the "export" command followed by the command you want to ignore.

export HISTIGNORE='ls -l:pwd:date:'

Among them, "ls -l", "pwd", and "date" are specified, which means that any command containing these strings will not be saved in the history.

Ignoring repeated commands in the history ensures a cleaner history:

export HISTCONTROL=ignoredups

The ignoredups option tells the system to ignore repeated commands when recording history. If the same command is executed multiple times in a row, only the first occurrence of the command is stored in the history.

To permanently store the HISTTIMEFORMAT, HISTIGNORE, and configuration HISTCONTROL in a Linux environment, you add them to the.bash_profile file, which is executed each time a new shell session is started.

nano ~/.bash_profile

Add the following line to the.bash_profile file to set the environment variables:

export HISTTIMEFORMAT="%F %T "

export HISTIGNORE="some:commands:to:ignore"

export HISTCONTROL=ignoredups

To apply the changes, restart the terminal or run the following command in the terminal:

source ~/.bash_profile

unset the export commands, you can use the unset command to delete their values, which will restore these Settings to the default configuration:

unset HISTTIMEFORMAT

unset HISTCONTROL

After these commands are executed, the timestamp display in the command history is restored to the default setting, and duplicate commands are no longer automatically filtered out.

To list commands executed by a specific user, you can use.bash_history the user's file, which stores the history of commands executed in the Bash shell.

sudo cat /home/username/.bash_history

If you want to filter the command history by a specific pattern, use the grep command as well as commands that contain words.

sudo cat /home/username/.bash_history | grep "ls"

To disable the storage of command history in Linux, use HISTFILE:

unset HISTFILE

By canceling the HISTFILE variable, you can organize the system to store the command history, ensuring that the commands executed on the terminal will not be recorded for future sessions. This change applies only to the current session and does not persist across different terminal sessions. If you want to make this change permanent, consider updating the shell's configuration.bash_profile file to HISTFILE to unset the variable every time you log in.

You can use the up and down arrow keys to view previously used commands, which can be useful or annoying. To delete or clear all entries from the bash history list, you can use the '-c' option.

history -c

To clear the command history for all users, you can delete or truncate the history file /home/[username]/.bash_history located in each user.

> /home/[user name]/.bash_history

or

rm /home/[user name]/.bash_history

To filter the commands in the history command output, you can use the grep command, which will display only the commands in the history that contain the keywords shown.

history | grep "ls"

history | grep "pwd"

history | grep "date"

JTTI-Ellis
JTTI-Selina
JTTI-Defl
JTTI-Eom
JTTI-COCO