search
HomeSystem TutorialLINUXDetailed analysis of how to display date and time in the history command

We are all familiar with the history command. It stores all commands executed by bash on the terminal into the .bash_history file to help us review the commands previously executed by the user.

By default, the history command directly displays the commands executed by the user without outputting the date and time when the command was run, even if the history command records this time.

When you run the history command, it will check an environment variable called HISTTIMEFORMAT. This environment variable specifies how to format the output of the time recorded in the history command.

If the value is null or not set at all, it will be the same as most systems display by default, and the date and time will not be displayed.

HISTTIMEFORMAT Use strftime to format display times (strftime - converts dates and times to strings). The history command outputs the date and time to help you track down the problem more easily.

·%T: Replaced with time (%H:%M:%S).
·%F: Equivalent to %Y-%m-%d (ISO 8601:2000 standard date format).

The following is the default output of the history command.

# history
 1 yum install -y mysql-server mysql-client
 2 service mysqld start
 3 sysdig proc.name=sshd
 4 sysdig -c topprocs_net
 5 sysdig proc.name=sshd
 6 sysdig proc.name=sshd | more
 7 sysdig fd.name=/var/log/auth.log | more
 8 sysdig fd.name=/var/log/mysqld.log
 9 sysdig -cl
 10 sysdig -i httplog
 11 sysdig -i proc_exec_time
 12 sysdig -i topprocs_cpu
 13 sysdig -c topprocs_cpu
 14 sysdig -c tracers_2_statsd
 15 sysdig -c topfiles_bytes
 16 sysdig -c topprocs_cpu
 17 sysdig -c topprocs_cpu "fd.name contains sshd"
 18 sysdig -c topprocs_cpu "proc.name contains sshd"
 19 csysdig
 20 sysdig -c topprocs_cpu
 21 rpm --import https://s3.amazonaws.com/download.draios.com/DRAIOS-GPG-KEY.public
 22 curl -s -o /etc/yum.repos.d/draios.repo http://download.draios.com/stable/rpm/draios.repo
 23 yum install -y epel-release
 24 yum update
 25 yum makecache
 26 yum -y install kernel-devel-$(uname -r)
 27 yum -y install sysdig
 28 sysdig
 29 yum install httpd mysql
 30 service httpd start

Depending on your needs, there are three different ways to set environment variables.

·Temporarily set the current user’s environment variables
·Permanently set environment variables for current/other users
·Permanently set environment variables for all users

Note: Don’t forget to add a space before the last single quote, otherwise the output will be very confusing.

method 1:

Run the following command to temporarily set the HISTTIMEFORMAT variable for the current user. This will take effect until the next reboot.

# export HISTTIMEFORMAT='%F %T '

Method 2:

Add the HISTTIMEFORMAT variable to the .bashrc or .bash_profile file to make it permanent.

# echo 'HISTTIMEFORMAT="%F %T "' >> ~/.bashrc
或
# echo 'HISTTIMEFORMAT="%F %T "' >> ~/.bash_profile

Run the following command to make the changes in the file take effect.

# source ~/.bashrc
或
# source ~/.bash_profile

Method 3:

Add the HISTTIMEFORMAT variable to the /etc/profile file to make it permanent for all users.

# echo 'HISTTIMEFORMAT="%F %T "' >> /etc/profile

Run the following command to make the changes in the file take effect.

# source /etc/profile

The output result is:

# history
 1 2017-08-16 15:30:15 yum install -y mysql-server mysql-client
 2 2017-08-16 15:30:15 service mysqld start
 3 2017-08-16 15:30:15 sysdig proc.name=sshd
 4 2017-08-16 15:30:15 sysdig -c topprocs_net
 5 2017-08-16 15:30:15 sysdig proc.name=sshd
 6 2017-08-16 15:30:15 sysdig proc.name=sshd | more
 7 2017-08-16 15:30:15 sysdig fd.name=/var/log/auth.log | more
 8 2017-08-16 15:30:15 sysdig fd.name=/var/log/mysqld.log
 9 2017-08-16 15:30:15 sysdig -cl
 10 2017-08-16 15:30:15 sysdig -i httplog
 11 2017-08-16 15:30:15 sysdig -i proc_exec_time
 12 2017-08-16 15:30:15 sysdig -i topprocs_cpu
 13 2017-08-16 15:30:15 sysdig -c topprocs_cpu
 14 2017-08-16 15:30:15 sysdig -c tracers_2_statsd
 15 2017-08-16 15:30:15 sysdig -c topfiles_bytes
 16 2017-08-16 15:30:15 sysdig -c topprocs_cpu
 17 2017-08-16 15:30:15 sysdig -c topprocs_cpu "fd.name contains sshd"
 18 2017-08-16 15:30:15 sysdig -c topprocs_cpu "proc.name contains sshd"
 19 2017-08-16 15:30:15 csysdig
 20 2017-08-16 15:30:15 sysdig -c topprocs_cpu
 21 2017-08-16 15:30:15 rpm --import https://s3.amazonaws.com/download.draios.com/DRAIOS-GPG-KEY.public
 22 2017-08-16 15:30:15 curl -s -o /etc/yum.repos.d/draios.repo http://download.draios.com/stable/rpm/draios.repo
 23 2017-08-16 15:30:15 yum install -y epel-release
 24 2017-08-16 15:30:15 yum update
 25 2017-08-16 15:30:15 yum makecache
 26 2017-08-16 15:30:15 yum -y install kernel-devel-$(uname -r)
 27 2017-08-16 15:30:15 yum -y install sysdig
 28 2017-08-16 15:30:15 sysdig
 29 2017-08-16 15:30:15 yum install httpd mysql
 30 2017-08-16 15:30:15 service httpd start

The above is the detailed content of Detailed analysis of how to display date and time in the history command. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:脚本之家. If there is any infringement, please contact admin@php.cn delete
Top 3 Open Source Virtual Data Room (VDR) for LinuxTop 3 Open Source Virtual Data Room (VDR) for LinuxMay 08, 2025 am 11:35 AM

Virtual Data Rooms (VDRs) offer secure document storage and sharing, ideal for sensitive business information. This article explores three open-source VDR solutions for on-premises deployment on Linux, eliminating the need for cloud-based services a

Upscayl: An Open-Source Image Upscaling Tool for LinuxUpscayl: An Open-Source Image Upscaling Tool for LinuxMay 08, 2025 am 11:19 AM

Upscayl: Your Free and Open-Source Solution for High-Resolution Images on Linux Linux users who frequently work with images know the frustration of low-resolution pictures. Luckily, Upscayl offers a powerful, free, and open-source solution. This des

Ghostty - A Feature-Rich Terminal Emulator for LinuxGhostty - A Feature-Rich Terminal Emulator for LinuxMay 08, 2025 am 11:14 AM

The terminal emulator landscape is evolving rapidly, with developers leveraging modern hardware, GPU acceleration, containerization, and even AI/LLMs to enhance console experiences. Enter Ghostty, a new open-source, cross-platform terminal emulator

Innotop - A CLI Based top-like Monitor Tool for MySQLInnotop - A CLI Based top-like Monitor Tool for MySQLMay 08, 2025 am 10:48 AM

Innotop: Powerful MySQL monitoring command line tool Innotop is an excellent command line program, similar to the top command, used to monitor local and remote MySQL servers running under the InnoDB engine. It provides a comprehensive set of features and options to help database administrators (DBAs) track various aspects of MySQL performance, troubleshoot issues and optimize server configuration. Innotop allows you to monitor critical MySQL metrics, such as: MySQL replication status User statistics Query list InnoDB buffer pool InnoDB I/O Statistics Open table Locked table etc… The tool regularly refreshes its data to provide server status

How to Back Up Linux Data with Restic ToolHow to Back Up Linux Data with Restic ToolMay 08, 2025 am 10:34 AM

Restic: Your Comprehensive Guide to Secure Linux Backups Data loss can cripple a Linux system. Accidental deletions, hardware failures, or system corruption necessitate a robust backup strategy. Restic is a leading solution, providing speed, securi

10 Top Most Popular Linux Distributions in 202410 Top Most Popular Linux Distributions in 2024May 08, 2025 am 10:15 AM

Top 10 Most Popular Linux Distributions in 2025 Entering 2025, we are excited to share with Linux enthusiasts the most popular distribution this year so far. DistroWatch has always been the most reliable source of information about open source operating systems, with particular attention to Linux distributions and BSD versions. It continuously collects and presents a lot of information about Linux distributions, making them easier to access. While it doesn't measure the popularity or usage of a distribution very well, DistroWatch remains the most accepted measure of popularity within the Linux community. It uses page click ranking (PHR) statistics to measure the popularity of Linux distributions among website visitors. [You can

Top 15 Tiling Window Managers for Linux in 2025Top 15 Tiling Window Managers for Linux in 2025May 08, 2025 am 10:08 AM

Linux Window Managers: A Comprehensive Guide to the Best Tiling Options Linux window managers orchestrate how application windows behave, quietly managing the visual arrangement of your open programs. This article explores top-tier tiling window man

How to Replace Numbers Dynamically Using sed in LinuxHow to Replace Numbers Dynamically Using sed in LinuxMay 08, 2025 am 09:33 AM

The sed command (stream editor) in Linux system is a powerful text processing tool that is widely used for text manipulation tasks, including searching, finding and replacing text, and even executing advanced scripting. This article will guide you through the basics of sed, explain how to use it for dynamic number replacement, and provide practical examples for beginners. What is sed? The sed command processes text line by line, allowing you to: Search for specific patterns. Replace text or number. Delete or insert rows. Convert text in various ways. It works in a non-interactive way, meaning it can process files or text streams without human intervention. Basic syntax of sed command sed [Options] 'Command' file illustrate: Options

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool