8

In UNIX, how to find the most commonly used commands in history?

  • I think this is off topic here, but google shows several good lists for reference. Obviously usage will vary by distro and availability of commands as well as by user, their purpose, and their knowledge of other commands, but there are certainly a few that float to the top and show up in all the lists on the subject. – Caleb Jun 15 '11 at 11:33
  • 7
    Do you mean "shell commands history file" or "history of Unix"? – alex Jun 15 '11 at 12:09
11

It rather depends on what you intend by 'command'. From one perspective a command is simply an executable called from the command line, therefore ls and ls -alh /tmp/foo/bar/*tar.bz are both the command ls. Another perspective is that a command is the sum of its parts, meaning that the above example is 2 distinct commands.

If your intention is to count wholly unique commands in the history, you could execute: history | cut -d' ' -f4- | uniq -c | sort -n

Please be aware that this was tested on a Debian system, the output syntax of your history command may vary slightly. Adjust the number '4' in the command to select alternate columns.

If you wish to consider only the first command without arguments, then you could execute: history | cut -d' ' -f4- | cut -d' ' -f1 | uniq -c | sort -n

To include the arguments but not any chained or piped commands that follow you could execute: history | cut -d ' ' -f4- | cut -d'|' -f1 | uniq -c | sort -n

Please note that each of these sorts the number of commands numerically with the highest frequency appearing last.

  • I needed to add an additional sort before uniq to get an accurate count since it only checks adjacent lines: history | cut -d' ' -f4- | cut -d' ' -f1 | sort | uniq -c | sort -n – nocash Aug 27 '14 at 17:53
4

I have seen both vucar and tok answers, they are very useful but you could use the amazing tool awk too:

if you want to list of commands you use most often:

history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head

to find the most used command:

history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head -1 

for me it was vi:

94 vi
4

Another approach would be to use BSD process accounting, which is also available on Linux (though I don't know whether it is enabled by default). It basically keeps a tab of all programs that are run (and some other measures, like CPU time consumed etc.) for as long as the accounting is active. From there a few aggregate statistics are available, among others: kind of a list of all programs ever run on the system, sorted by etc. top number of uses.

The commands to look for are lastcomm for the running log and sa for the aggregated statistics. Getting back to the original question, assuming that the system in question would have had accounting enabled since it was installed, then sa -n would give a list of all commands ever executed on the system (for all users), sorted by frequency. For pipelines, each part of the pipeline would of course be counted separately, as each part comprises a new process to be forked.

For more details, the Linux journal has an article explaining the workings. I myself got the idea from the NetBSD guide; many of the more generic administration tips in there are not specific to NetBSD but apply to other Unix systems as well.

2

Top 20 most used CLI commands:

history |  cut -c7- | sort  | uniq -c | sort -nr | head -n 20
1

You can use a single cut command:

history | cut -c 7-100  | sort  | uniq -c | sort -nr
1

If it's just for the current session, then hash with no arguments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.