Ubuntu Command History: How to View Recent Commands

Navigating the Linux Command Line: Accessing Command History
For users transitioning from Windows, the Linux command line interface can initially appear complex. However, accessing a list of previously executed commands is a straightforward process.
The history command provides a numbered record of recent commands. For example:
> history
1 ps -ef
2 kill 24188
3 ps -ef
4 tail logfile.log
Searching Your Command History with Grep
When dealing with an extensive command history, locating a specific command can be expedited using grep. Suppose you recall using the ftp command but have forgotten the server's domain.
You can filter the history output like this:
> history | grep ftp
321 ftp ftp.cdrom18.com
This efficiently narrows down the search results.
Identifying Frequently Used Commands
Determining which commands are used most often requires a more elaborate command sequence.
The following command accomplishes this:
> history|awk '{print $2}'|awk 'BEGIN {FS="|"} {print $1}'|sort|uniq -c|sort -r
114 ls
105 ./runreports.sh
97 cd
24 uptime
15 mysql
13 vi
This pipeline extracts the commands, counts their occurrences, and presents them in descending order of frequency.
This particular command was originally sourced from Lifehacker, a valuable resource for tech tips and tricks. The principles demonstrated within this command are applicable to a variety of other scenarios.
Further explorations of useful Linux commands will be shared in future posts.