Whenever a command is entered in the terminal, it will be saved at the end of the history file in Linux. You can easily retrieve these commands at any time using
history command. The shell is also tracking the timestamp of all command entries, so that we can easily find when a specific command is executed. We already have shown you how to enable timestamp in Bash and Zsh shells. Today we will see how to enable timestamp for history command in Fish shell in Linux. In addition, we will also learn how to create a simple function to show the date and time stamps in history command output in fish shell.
Enable Timestamp For History Command In Fish Shell
Starting from version 2.6, the
fish shell supports timestamps in the built-in
history command. Let us run a few commands in the
fish shell session:
> lsb_release -a
> uname -r
> hostname -f
> mkdir ostechnix
> cd ostechnix/
> touch ostechnix.txt
If you run the
history command without any flags, you will see all of these previously executed commands without the timestamps.
> history ls touch ostechnix.txt cd ostechnix/ mkdir ostechnix hostname -f uname -r lsb_release -a [...]
To enable timestamp for
history command in
fish shell, use
--show-time flag as shown below:
> history --show-time
# Monday 30 November 2020 02:39:52 PM history # Monday 30 November 2020 02:36:52 PM ls # Monday 30 November 2020 02:36:47 PM touch ostechnix.txt # Monday 30 November 2020 02:36:39 PM cd ostechnix/ # Monday 30 November 2020 02:36:36 PM mkdir ostechnix # Monday 30 November 2020 02:34:11 PM hostname -f # Monday 30 November 2020 02:33:51 PM uname -r # Monday 30 November 2020 02:33:42 PM lsb_release -a [...]
As you can see, the history command shows the timestamp on the top of each command. I don't like the way the fish shell shows the date and time stamps. So, I customized the history command output like below:
> history --show-time='%F %T '
2020-11-30 14:47:12 history --show-time 2020-11-30 14:39:52 history 2020-11-30 14:36:52 ls 2020-11-30 14:36:47 touch ostechnix.txt 2020-11-30 14:36:39 cd ostechnix/ 2020-11-30 14:36:36 mkdir ostechnix 2020-11-30 14:34:11 hostname -f 2020-11-30 14:33:51 uname -r 2020-11-30 14:33:42 lsb_release -a [...]
Now it is perfect!
%F option displays the date in
YYYY-MM-DD (Year-Month-Date) format. And the
%T option shows the time in the format
HH:MM:SS (Hour-Minute-Seconds) format.
If you want to show only the date, use this command:
> history --show-time='%F '
2020-11-30 ls 2020-11-30 touch ostechnix.txt [...]
To display only the time, then use this:
> history --show-time='%T '
14:36:52 ls 14:36:47 touch ostechnix.txt [...]
You can also use the following different formats as well:
> history --show-time='%d/%m/%y %H:%M:%S '
This shows the history output in the following format:
30/11/20 14:36:52 ls 30/11/20 14:36:47 touch ostechnix.txt [...]
Here is another version:
> history --show-time='%h/%d - %H:%M:%S '
Nov/30 - 14:36:52 ls Nov/30 - 14:36:47 touch ostechnix.txt [...]
A fish function to show date and time stamps in history command output
If you want to save a few strokes, you could use a
function like below.
> nano ~/.config/fish/functions/history.fish
Note: If the
~/.config/fish/functions/ directory doesn't exist, just create it.
Add the following lines in
function history builtin history --show-time='%F %T ' end
history command will show you the timestamp without any flags:
For more details, refer fish man page:
> man fish
You know now how to show the date and time in
history command output in fish shell in Linux. You also learned how to use a simple
function to enable timestamp for history command in fish shell. Hope you find this useful.