Home Fish shell Enable Timestamp For History Command In Fish Shell

Enable Timestamp For History Command In Fish Shell

By sk
Published: Updated: 5.2K views

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
> ls

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

Sample output:

# 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
[...]
Enable Timestamp For History Command In Fish Shell
Enable Timestamp For History Command In Fish Shell

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 '

Sample output:

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!

Here, the %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 '

Sample output:

2020-11-30 ls
2020-11-30 touch ostechnix.txt
[...]

To display only the time, then use this:

> history --show-time='%T '

Sample output:

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 '

Sample output:

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 history.fish file:

function history
    builtin history --show-time='%F %T '
end

Now the history command will show you the timestamp without any flags:

Show date and time in history command's output in fish shell in Linux
Show date and time in history command's output in fish shell in Linux

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.

Related read:

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More