Saturday, April 27, 2024
20
rated 0 times [  20] [ 0]  / answers: 1 / hits: 4505  / 3 Years ago, fri, october 22, 2021, 8:13:45

How can I make a list with most used commands in terminal?



I know that this question may be unuseful for any future proposals for some of us, but even like this, the list can be useful when we don't remember a command used once or some times in the past, when we can search at the end of this list.


More From » command-line

 Answers
5

We will use the records from .bash_history file to do this. The next command will give you a list of all commands in order that you used them most often:



history | awk 'BEGIN {FS="[ 	]+||"} {print $3}' | sort | uniq -c | sort -nr


If you want only top 10, you must to add head at the command above:



history | awk 'BEGIN {FS="[ 	]+||"} {print $3}' | sort | uniq -c | sort -nr | head


To get a specific top, for example top 5, use head with -n 5 option:



Top 5 commands



If you want the list in reverse order (top with the rarely used commands), don't use r oprion for second sort:



history | awk 'BEGIN {FS="[ 	]+||"} {print $3}' | sort | uniq -c | sort -n


And finally to get a list with the commands used once for example, use grep ' 1 ' (change 1 with the desired number):



history | awk 'BEGIN {FS="[ 	]+||"} {print $3}' | sort | uniq -c | grep ' 1 '


To deal with sudo commands (like sudo vim foo), instead of just {print $3} in the awk command, use:



{if($3 ~ /sudo/) print $4; else print $3}


So the entire command would look like:



history | awk 'BEGIN {FS="[ 	]+||"} {if($3 ~ /sudo/) print $4; else print $3}' | sort | uniq -c | sort -nr


For example:



$ history | awk 'BEGIN {FS="[ 	]+||"} {print $3}' | sort | uniq -c | sort -nr | head
284 vim
260 git
187 find
174 man
168 echo
149 rm
134 awk
115 pac
110 sudo
102 l

$ history | awk 'BEGIN {FS="[ ]+||"} {if($3 ~ /sudo/) print $4; else print $3}' | sort | uniq -c | sort -nr | head
298 vim
260 git
189 find
174 man
168 echo
153 rm
134 awk
115 pac
102 l
95 cd


You can see the jump in counts for vim, rm, etc.


[#30595] Friday, October 22, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
utilitere

Total Points: 394
Total Questions: 110
Total Answers: 114

Location: Solomon Islands
Member since Wed, Mar 29, 2023
1 Year ago
;