
Find Your Most Frequently Used Terminal Commands
- Adam Douglas
I was inspired to write this post from watching the video “Find Your Most Used Terminal Commands (Bash, Fish, Zsh)” (YouTube, Odysee) on DistroTube by Derek Taylor and thought it could be interesting. For various reasons some of us want to know what is the most used commands in the terminal. Getting this information is not hard, however there are many ways in which to go about doing it. As well there is the whole criteria of what is considered a command. Some say it’s the entire line that is executed and some believe that each command should be treated separately. Others argue what is the entire point of doing this in the first place. I’m sure the debate will go on till the end of time. So let’s continue on down this rabbit hole and see what we can dig up.
Bash and Zsh
Here are a few variations that all provide the same output of 25 most used commands that displays the usage count and command.
$ history | awk 'BEGIN {FS="[ \t]+|\\|"} {print $3}' | sort | uniq -c | sort -nr | head -n25
$ history | awk '{print $2}' | sort | uniq -c | sort -nr | head -n25
$ history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -nr | head -n25
3353 git
3201 pikaur
2601 ll
2451 sudo
2168 cd
1942 nano
1537 grep
1423 mv
1269 rm
854 find
799 cp
728
658 man
585 eog
550 ls
534 pacaur
454 which
405 evince
397 systemctl
391 cat
337 mkdir
336 history
295 t
277 yarn
274 scp
Fish
Getting the most used commands in Fish is not directly possible as far as I know. The history of commands is recorded, but the recording doesn’t allow for duplicates. Therefore, how can one count all the commands to determine the most used? Presently there is a Fish project issue, “History should contain how often a command was executed” that talks about having this ability, however I really doubt it will happen. This is more of fun thing to do then a necessity.
With all that said, this doesn’t mean one cannot still see what the approximate most used commands are in Fish. Here are a few variations of commands to get the most used 25 commands.
$ history | cut -d ' ' -f1 | sort | uniq -c | sort -nr | head -n25
$ history | awk '{print $1}' | sort | uniq -c | sort -nr | head -n25
$ history | awk 'BEGIN {FS=" "} {print $1}' | sort | uniq -c | sort -nr | head -n25
3353 git
3201 pikaur
2601 ll
2451 sudo
2168 cd
1942 nano
1537 grep
1423 mv
1269 rm
854 find
799 cp
728
658 man
585 eog
550 ls
534 pacaur
454 which
405 evince
397 systemctl
391 cat
337 mkdir
336 history
295 t
277 yarn
274 scp
Command Statistics
This is a very interesting approach I found online that outputs, line number, usage count, percentage used, and
command. I have not been able to figure out how to make this method work beyond using Bash. I would first need to learn
about what exactly the fc
command does. I did figure out that the fc
is a built-in command in Bash and Zsh.
Command example is intended for Bash and Zsh, but I have not tested the command in Zsh.
$ function shstats() { fc -l 1 | awk '{CMD[$2]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' | grep -v "./" | column -c3 -s " " -t | sort -nr | nl | head -n25; }
$ shstats
1 75 15.0905% exit
2 57 11.4688% ls
3 39 7.84708% echo
4 27 5.4326% history
5 20 4.02414% ll
6 19 3.82294% cd
7 17 3.42052% yarn
8 16 3.21932% nano
9 14 2.8169% sudo
10 14 2.8169% find
11 12 2.41449% man
12 12 2.41449% composer
13 11 2.21328% du
14 10 2.01207% md5sum
15 8 1.60966% which
16 8 1.60966% t=$(df|awk
17 7 1.40845% sed
18 7 1.40845% pwd
19 7 1.40845% mv
20 6 1.20724% rm
21 5 1.00604% windowID=$(xprop
22 5 1.00604% mount
23 4 0.804829% cat
24 3 0.603622% youtube-dl-gui
25 3 0.603622% windowID=$(wmctrl
PowerShell
I thought it would be interesting to figure out how to accomplish getting PowerShell most frequently used commands. I’ve not used PowerShell much, so it did take me awhile to figure out. Do keep in mind that as far as I understand it, PowerShell history of commands are only tracked until the session ends. Each session one would have to save the history and then import manually if one wants to have a more accurate most used commands. See my example below on how to save and import history data.
The command example was tested using PowerShell for Linux. There could be some slight differences, but it should work just fine in Microsoft Windows.
PS C:\> get-history | group-object -Property CommandLine | select-object -Property Count, Name | sort-object -Property Count -top 10 -Descending
Count Name
----- ----
6 dir
5 get-history
5 get-history | group-object CommandLine | sort-object Name | select-object Name, Count
5 get-history | select-object -property CommandLine | sort-object | get-unique -asstring | select-object -first 10
4 ls
3 get-history | group-object CommandLine | select-object Count, Name | sort-object -Descending Count
2 get-history | group-object CommandLine | sort-object Name | select-object Count, Name
2 get-history | select-object -property CommandLine | sort-object
2 help sort-object
1 get-help get-process
Save Command History
PS C:\> Get-History | Export-Clixml -Path c:\commands.xml
Import Command History
PS C:\> Add-History -InputObject (Import-Clixml -Path c:\commands.xml)
This is post 48 of 100, and is round 2 of the 100 Days To Offload challenge.
References
- Bash (Unix shell), Wikipedia
- cat, grep, cut, sort, uniq, sed with PowerShell
- fish (Unix Shell), Wikipedia
- How can I make a list with most used commands in terminal?, Ask Ubuntu
- How To List Your Most Used Commands In Terminal?, Fossbytes
- How to see the terminal commands you use most on Linux
- List of command-line interpreters, Wikipedia
- List Your 15 Most Used Terminal Commands, OSXDaily
- PowerShell Documentation
- PowerShell equivalents of unix commands
- PowerShell, Wikipedia
- PowerShell, GitHub
- Shell (computing), Wikipedia
- Statistic bar graph, image by madartzgraphics, Published Nov 9, 2016, Pixabay
- Z shell, Wikipedia
Changelog
-
- change topic
-
- change 100DaysToOffload message
-
- Fix powershell prompt