Your shell keeps a record of the commands you've previously entered. You can access this list, which is incredibly useful when you want to find and reuse a command without retyping it. The history command is a fundamental tool in most Unix and Linux environments.
Viewing Your Command History
To see the list of commands you have used, simply type the history command. This feature provides a detailed log of your activity, making it easy to track your history in linux.
history
Re-running Previous Commands
The shell provides several shortcuts to make re-running commands easier.
- Up Arrow: Want to run the same command you just did? Just press the up arrow key to cycle backward through your history.
- The
!!Shortcut: To execute the most recent command again, you can use!!. For example, if you just rancat file1, typing!!and pressing Enter will runcat file1again.
Searching Your History
One of the most powerful history shortcuts is Ctrl-R. This initiates a reverse search. After pressing Ctrl-R, start typing any part of the command you're looking for, and the shell will display the most recent match. You can press Ctrl-R repeatedly to cycle through older matches. Once you find the command you want, just press Enter to execute it.
Managing the History List
Beyond just viewing your history, you can also manage it directly.
- Clearing History: If you want to clear the command history for your current session, you can use the
history -c linuxcommand. This removes all entries from the history list in memory. - Writing to File: To save the current session's history to your history file (usually
~/.bash_history), you can usehistory -w linux. This is useful for preserving commands before closing a session. - Deleting a Specific Entry: You can remove a single command from your history using
history -d <offset>. The offset is the number shown next to the command in thehistoryoutput. For example,history -d 101would delete the 101st entry. This is a key function ofhistory -d linux.
Other Useful Terminal Tools
As your terminal window fills up, you might want to clean it. Use the clear command to wipe your display and start with a fresh screen.
clear
Another indispensable feature is tab completion. If you start typing the beginning of a command, filename, or directory and press the Tab key, the shell will attempt to autocomplete it. If there are multiple possibilities, it may show you the options or do nothing. Pressing Tab a second time will often list all possible completions.