HB Computer Security | Aide Memoire
  • Introduction
  • Core Technical Skills
    • Core Skills
      • Linux
        • Getting to Know Linux
          • Using the Shell
            • Shells, Terminals, and Virtual Consoles
            • Choose your Shell
            • Shell Commands
              • Non-PATH Commands
              • Command History
                • Command Line Editing
                  • Keystrokes for Navigating Command Lines
                  • Keystrokes for Editing Command Lines
                  • Keystrokes for Cutting and Pasting Text from within Command Lines
                • Command Line Recall
                  • Keystrokes for Command Line Recall
              • Connecting and Expanding Commands
                • Piping Between Commands
                • Sequential Commands
                • Expanding Commands
            • Shell Variables
              • Common Shell Variables
            • Aliases
            • Create your Own Shell Environment
              • Modification Ideas
          • Navigating the Linux File System (LFS)
            • Filesystem Commands
            • Listing Files and Directories
            • File Permissions and Ownership
              • Modifying Permissions with chmod
              • Modifying Default Permissions with umask
              • Change File Ownership with chown
            • Copying, Moving, and Removing Files
            • Finding Files
              • locate
              • find
              • grep
            • Downloading Files
              • axel
              • wget
              • curl
                • User-Agent: Googlebot
          • Working with Text Files
            • Using vim and vi to Edit Text Files
              • Starting with vi
              • Adding Text
              • Moving Around in the Text
            • Text Manipulation
        • System Administration
          • Installing Linux
            • Installing from Live Media
            • Installing in the Enterprise
            • Partitioning Hard Disks
              • Tips for Creating Partitions
          • Account Administration
            • The root Account
              • Becoming root with su
              • sudo
                • The /etc/sudoers File
                • Granting sudo privileges
                  • visudo Guidance
                • Useful sudo Hints
            • Other Administrative Accounts
            • Standard User Accounts
              • Risks of userdel: Orphaned Files
          • Graphical Remote Administration
            • Cockpit
              • Installation Guide
            • Remote Desktop Protocol with xrdp
              • Installation and Configuration
            • Remote Desktop with vnc
              • Installation and Configuration
              • Running VNC as a System Service
          • Managing Running Processes
            • Listing Processes
              • ps
              • top
              • htop
            • Backgrounding and Foregrounding
              • Starting a Background Process
              • Using Foreground and Background Commands
            • Killing and Recining Processes
              • kill and killall
          • Managing Software
            • Managing Software from the Desktop
            • Going Beyond the Limitations of Software Center
              • Debian Packages
                • Advanced Package Tool (apt)
                • Repositories
                • dpkg
        • Shell Scripting
          • Variables
            • Command Substitution
            • Arguments
          • Reading User Input
          • if, else, and elif
          • BOOLEAN Logic
          • Loops
            • for Loops
            • while Loops
          • Functions
          • Local Vs Global Variables
          • Summary
        • Securing Linux
      • Windows
        • Security Hardening
Powered by GitBook
On this page
  • Customise History ENV Variables
  • HISTCONTROL
  • HISTIGNORE
  • HISTTIMEFORMAT

Was this helpful?

  1. Core Technical Skills
  2. Core Skills
  3. Linux
  4. Getting to Know Linux
  5. Using the Shell
  6. Shell Commands

Command History

Ever had that moment where you think, "I swear I just typed that exact command five minutes ago"? Well, the shell's got your back.

If you've ever knocked up a super long command or just want a quick redo of something you've done, the shell has some nice tricks up its sleeve. Enter the shell history – it's like your diary, logging all those commands you've entered. Want to see? Just throw in the history command if you're using bash. From there, you've got the power to pull up those old commands, tweak them, or just run them again.

Stick aroundfor the next bit, where we'll dive into the magic of command-line editing; finishing off half-typed command lines, and navigating through your command history.

Customise History ENV Variables

The way the history command operates is steered by three environment variables:

  • HISTCONTROL

  • HISTIGNORE

  • HISTTIMEFORMAT

HISTCONTROL

The HISTCONTROL variable defines whether or not to remove any duplicate commands from your history, or commands that begin with spaces, or both!

By default, both are removed

You may find it more useful to only omit duplicates:

export HISTCONTROL=ignoredups

HISTIGNORE

The HISTIGNORE variable is particularly useful for filtering out basic commands that are run frequently, such as ls, exit, history, bg, etc:

export HISTIGNORE="&:ls:[bf]g:exit:history"

mkdir test

cd test

~/test# ls

~/test# pwd
/home/kali/test

~/test# ls

~/test# history
1 export HISTIGNORE="&:ls:[bf]g:exit:history"
2 mkdir test
3 cd test
4 pwd

As you can see, the ls command is missing form history.

HISTTIMEFORMAT

Lastly, HISTTIMEFORMAT controls date and/or time stamps in the output of the history command:

history
1 2018-02-12 13:37:33 export HISTIGNORE="&:ls:[bf]g:exit:history"
2 2018-02-12 13:37:38 mkdir test
3 2018-02-12 13:37:40 cd test
4 2018-02-12 13:37:43 pwd
5 2018-02-12 13:37:51 export HISTTIMEFORMAT='%F %T '

In this example, we used %F (Year-Month-Day ISO 8601 format) and %T (24-hour time). Other formats can be found in the strftime man page.

PreviousNon-PATH CommandsNextCommand Line Editing

Last updated 1 year ago

Was this helpful?