Posted: . At: 12:03 PM. This was 6 years ago. Post ID: 12268
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


Get your most popular shell commands with a simple one-liner.


This simple one-liner will print a list of your most executed shell commands. This is very useful for keeping track of the Linux utilities and scripts that a user executes most often.

jason@Yog-Sothoth » ~ » $ history | 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 -n10
     1	68  13.6%  sudo
     2	44  8.8%   ls
     3	31  6.2%   for
     4	30  6%     cd
     5	27  5.4%   cowsay
     6	26  5.2%   tr
     7	25  5%     cat
     8	24  4.8%   grep
     9	21  4.2%   mc
    10	14  2.8%   date

Get a bar chart of the most executed commands on your system.

jason@Yog-Sothoth » ~ » $ history | tr -s ' ' | cut -d ' ' -f3 | sort | uniq -c | sort -n | tail | perl -lane 'print $F[1], "\t", $F[0], " ", "▄" x ($F[0] / 12)'
date	14 ▄
mc	21 ▄
grep	24 ▄▄
cat	25 ▄▄
tr	26 ▄▄
cowsay	27 ▄▄
cd	29 ▄▄
for	31 ▄▄
ls	44 ▄▄▄
sudo	66 ▄▄▄▄▄

This is yet another solution to list commands that the user has executed most often.

jason@Yog-Sothoth » ~ » $ awk '{CMD[$1]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' .bash_history | grep -v "#" | sort -nr | nl | column -c3 -s " " -t | head -n 20
1	68  13.6%  sudo
2	44  8.8%   ls
3	31  6.2%   for
4	30  6%     cd
5	27  5.4%   cowsay
6	26  5.2%   tr
7	25  5%     cat
8	24  4.8%   grep
9	21  4.2%   mc
10	14  2.8%   date
11	12  2.4%   time
12	12  2.4%   man
13	11  2.2%   utmpdump
14	9   1.8%   shuf
15	9   1.8%   ps
16	9   1.8%   aterm
17	7   1.4%   dmesg
18	7   1.4%   dconf
19	6   1.2%   ssh
20	6   1.2%   (echo

This is another example, this lists the most-run Linux commands and how often each was run.

jason@Yog-Sothoth » ~ » $ history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head
66 sudo
40 ls
38 cat
31 for
31 cd
26 tr
23 mc
22 date
19 ps
12 time

This could be very useful to know commands are the most used on your Linux machine. This would give a user a good picture of how they use their system day to day.

Add date stamps to each entry that is output by the history command.

Run these commands to add a date stamp to commands listed by the history command. This is very useful to know exactly when a command was run.

echo 'export HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bashrc
source ~/.bashrc

Then run the history command, and the timestamps will appear before each entry.

jason@jason-Lenovo-H50-55:~$ history | tail -n 25
 1061  20/07/18 21:36:47 ./start.sh &exit
 1062  20/07/18 21:36:47 sudo apt update
 1063  20/07/18 21:36:47 sudo apt upgrade
 1064  20/07/18 21:36:47 cd Steam/steamapps/common/Arma\ 3\ Server/
 1065  20/07/18 21:36:47 ls
 1066  20/07/18 21:36:47 exit
 1067  20/07/18 13:02:24 env
 1068  20/07/18 13:02:37 export HISTTIMEFORMAT="%d/%m/%y %T "
 1069  20/07/18 13:02:40 env
 1070  20/07/18 13:02:44 ps ax
 1071  20/07/18 13:02:55 cat .bash_history
 1072  20/07/18 13:03:06 bash
 1073  20/07/18 13:03:13 cd Documents/
 1074  20/07/18 13:03:14 ls
 1075  20/07/18 13:03:18 ls -hula
 1076  20/07/18 13:03:26 cat ~/.bash_history
 1077  20/07/18 13:03:42 cat ~/.bash_history | head -n 20
 1078  20/07/18 13:03:53 cat ~/.bash_history | tail -n 20
 1079  20/07/18 13:07:40 echo 'export HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bashrc
 1080  20/07/18 13:07:41 source ~/.bashrc
 1081  20/07/18 13:07:49 ps ax
 1082  20/07/18 13:07:51 ps axhu
 1083  20/07/18 13:07:56 cat ~/.bash_history | tail -n 20
 1084  20/07/18 21:36:55 history
 1085  20/07/18 21:38:41 history | tail -n 25

This could be a very useful tip.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.