Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question
    
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
5  
Do you mean "shell commands history file" or "history of Unix"? –  alex Jun 15 '11 at 12:09

4 Answers 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
share|improve this answer

You can use a single cut command:

history | cut -c 7-100  | sort  | uniq -c | sort -nr
share|improve this answer

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 seperately, 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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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