translated

This commit is contained in:
geekpi 2020-08-11 08:49:16 +08:00
parent 6b5b74977e
commit f15f959af4
2 changed files with 180 additions and 181 deletions

View File

@ -1,181 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Summarizing your command usage on Linux)
[#]: via: (https://www.networkworld.com/article/3567050/summarizing-your-command-usage-on-linux.html)
[#]: author: (Sandra Henry-Stocker https://www.networkworld.com/author/Sandra-Henry_Stocker/)
Summarizing your command usage on Linux
======
With a modest string of commands, you can get a quick look at what commands you're using on your Linux system and how often.
Getty Images
Getting a summary of the commands you use on a Linux system requires only a relatively modest string of commands along with a couple pipes to tie them all together. When your history buffer preserves the most recent 1,000 or 2,000 commands, summarizing command activity can get rather tedious. This post provides a handy way to summarize command usage and highlight those commands used most frequently.
To start, keep in mind that a typical entry in a command history might look like this. Note that command is displayed after the command sequence number and followed by its arguments.
```
91 sudo apt-get install ccrypt
^
+-- command
```
Note that the history command, adhering to the HISTSIZE setting, will determine how many commands will be preserved. This could be 500, 1,000 or more. If you don't like how many commands are preserved for you, you can add or change the HISTSIZE setting in your .bashrc or other start-up file.
```
$ echo $HISTSIZE
1000
$ history | wc -l
1000
$ grep HISTSIZE ~/.bashrc
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
```
One of the primary benefits of remembering a significant number of commands is that it makes it easy for you to rerun commands you've used in the past without having to retype or remember them. It also allows you to easily review how far you've moved through some series of tasks that you might be working on. When you use the **history** command by itself, you'll see something like this with your oldest commands listed first:
```
$ history
7 vi tasks
8 alias
9 echo $HISTTIMEFORMAT
10 history
11 date
```
Viewing the most recent commands requires looking at the bottom of the recorded commands:
```
$ history | tail -4
1007 echo $HISTSIZE
1008 history | wc -l
1009 history
1010 history | tail -4
```
Alternately, you could use the **tail** command to view the bottom of your **.bash_history** file, but the numbers shown by the **history** command allow you to rerun the commands by typing things like **!1010** and are generally more useful.
To prepare a summary of the commands used (such as **vi** and **echo**), you can start by using **awk** to separate that information from the rest of each command saved in our history:
```
$ history | awk '{print $2}'
vi
alias
echo
history
date
```
If you then pass the list of commands in your history to the **sort** command to group the commands in alphabetical order, you'll get something like this:
```
$ history | awk '{print $2}' | sort
7z
7z
alias
apropos
cd
cd
```
Next, passing the output of that **sort** command to **uniq -c** will count how many of each command were used.
```
$ history | awk '{print $2}' | sort | uniq -c
2 7z
1 alias
2 apropos
38 cd
21 chmod
```
Last, adding a second **sort** command to sort the command group counts in reverse numeric order will list your most heavily used commands first.
```
$ history | awk '{print $2}' | sort | uniq -c | sort -nr
178 ls
95 vi
63 cd
53 sudo
41 more
```
This gives you an idea which commands you use the most, but won't include any commands that you may be intentionally omitting from your history file with a setting like this one:
```
HISTIGNORE="pwd:clear:man:history"
```
### When history changes
For the default history format, the first field in **history** command output will be the sequence number for each command, and the second will be the command that was used. For this reason, all of the **awk** commands shown above were set to display **$2**.
```
$ alias cmds='history | awk '\''{print $2}'\'' | sort | uniq -c | sort -nr'
```
If you've modified the format of your history entries with a setting like the one shown below that adds date and time settings to your command history, you will also have to modify the alias that you're setting up.
```
$ echo $HISTTIMEFORMAT
%d/%m/%y %T
```
This date/time information can be very helpful at times, but means that you have to pick out the 4th field instead of the 2nd in your command history to summarize your command usage because your history entries will look like this:
```
91 05/07/20 16:37:39 sudo apt-get install ccrypt
^
+-- command
```
The alias for examining your command history would, therefore, look like this instead after changing the $2 to $4.
```
$ alias cmds='history | awk '\''{print $4}'\'' | sort | uniq -c | sort -nr'
```
To store either alias in your **.bashrc** or other startup file, make sure you insert a backslash in front of the **$** sign so that bash doesn't try to interpret **$4**.
```
alias cmds='history | awk '\''{print \$2}'\'' | uniq -c | sort -nr'
alias cmds='history | awk '\''{print \$4}'\'' | uniq -c | sort -nr'
```
Note that the date and time information is stored on separate lines in your history file from the command themselves. So, when this information is added, the bash history file will have twice as many lines, though your history command output will not:
```
$ wc -l .bash_history
2000 .bash_history
$ history | wc -l
1000
```
### Wrap-up
You can always decide how much command history you want to preserve and what commands are not worth recording to make your command summaries most useful.
Join the Network World communities on [Facebook][1] and [LinkedIn][2] to comment on topics that are top of mind.
--------------------------------------------------------------------------------
via: https://www.networkworld.com/article/3567050/summarizing-your-command-usage-on-linux.html
作者:[Sandra Henry-Stocker][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.networkworld.com/author/Sandra-Henry_Stocker/
[b]: https://github.com/lujun9972
[1]: https://www.facebook.com/NetworkWorld/
[2]: https://www.linkedin.com/company/network-world

View File

@ -0,0 +1,180 @@
[#]: collector: (lujun9972)
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Summarizing your command usage on Linux)
[#]: via: (https://www.networkworld.com/article/3567050/summarizing-your-command-usage-on-linux.html)
[#]: author: (Sandra Henry-Stocker https://www.networkworld.com/author/Sandra-Henry_Stocker/)
汇总 Linux 上命令的使用率
======
使用合适的命令,你可以快速了解 Linux 系统上使用的命令以及执行的频率。
汇总 Linux 系统上使用的命令只需相对简单的命令串以及几条管道将它们绑定在一起。当你的历史记录缓冲区保留了最近的 1,000 或 2,000 条命令时,汇总命令活动可能会变得很乏味。这篇文章提供了一种方便的方法来汇总命令并高亮显示最常用的命令。
首先,请记住,典型的历史命令记录可能看起来像这样。请注意,命令显示在命令序列号之后,并紧跟其参数。
```
91 sudo apt-get install ccrypt
^
+-- command
```
请注意history 命令遵循 HISTSIZE 的设置,这会决定保留多少条命令。可能是 500、1,000 或更多。如果你不喜欢它的设置,那么可以在 .bashrc 或其他启动文件中添加或更改 HISTSIZE 设置。
```
$ echo $HISTSIZE
1000
$ history | wc -l
1000
$ grep HISTSIZE ~/.bashrc
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
```
记住大量命令的主要好处之一是,它可以让你轻松地重新运行过去使用的命令,而不必重新输入或记住它们。它还能让你轻松地查看你在一个任务中已经做了多少工作。单独使用 **history** 命令时,你会看到类似下面这样,最早的在最前面:
```
$ history
7 vi tasks
8 alias
9 echo $HISTTIMEFORMAT
10 history
11 date
```
查看最新使用命令需要查看命令的底部:
```
$ history | tail -4
1007 echo $HISTSIZE
1008 history | wc -l
1009 history
1010 history | tail -4
```
另外,你可以使用 ** tail **命令查看 **.bash_history** 文件的底部,但是 **history** 命令显示的数字可以让你输入如 **!1010** 这样的数字重新运行命令,这点通常更有用。
要准备已使用命令的汇总(例如 **vi****echo**),你可以首先使用 **awk** 将命令与 history 中保存的其他信息分隔开来:
```
$ history | awk '{print $2}'
vi
alias
echo
history
date
```
如果你将历史记录中的命令列表传递给 **sort** 命令以按字母顺序对命令进行分组,那么会得到以下内容:
```
$ history | awk '{print $2}' | sort
7z
7z
alias
apropos
cd
cd
```
接下来,将 **sort** 命令的输出传递给 **uniq -c** ,这将计算每个命令使用了多少次。
```
$ history | awk '{print $2}' | sort | uniq -c
2 7z
1 alias
2 apropos
38 cd
21 chmod
```
最后,添加第二个 **sort** 命令按倒序对命令组计数进行排序,这将先列出最常用的命令。
```
$ history | awk '{print $2}' | sort | uniq -c | sort -nr
178 ls
95 vi
63 cd
53 sudo
41 more
```
这样可以让你了解使用最多的命令,但不会包括任何你可能故意从历史记录文件中删除的命令,例如:
```
HISTIGNORE="pwd:clear:man:history"
```
### 当历史变更时
对于默认的历史记录格式,**history** 命令输出中的第一个字段将是每个命令的序号,第二个字段是使用的命令。因此,上面所有 **awk** 命令都设置成显示 **$2**。
```
$ alias cmds='history | awk '\''{print $2}'\'' | sort | uniq -c | sort -nr'
```
如果你像下面那样将日期和时间添加了到 history 命令中,那么你还必须修改所设置的别名。
```
$ echo $HISTTIMEFORMAT
%d/%m/%y %T
```
这个日期/时间信息有时会很有帮助,但是这意味着你必须在选择 history 命令的第 4 个字段而不是第 2 个字段来汇总命令,因为你的历史记录条目将如下所示:
```
91 05/07/20 16:37:39 sudo apt-get install ccrypt
^
+-- command
```
因此,在将 $2 变为 $4 之后,用于检查 history 命令的别名将改为这样。
```
$ alias cmds='history | awk '\''{print $4}'\'' | sort | uniq -c | sort -nr'
```
可将别名保存在 **.bashrc** 或其他启动文件中,请确保在 **$** 符号前面插入反斜杠,以便 bash 不会尝试解释 **$4**。
```
alias cmds='history | awk '\''{print \$2}'\'' | uniq -c | sort -nr'
alias cmds='history | awk '\''{print \$4}'\'' | uniq -c | sort -nr'
```
请注意日期和时间信息与命令本身保存在历史记录文件的不同行中。因此添加此信息后bash 历史记录文件的行数将增加一倍,尽管在输出 history 命令时不会:
```
$ wc -l .bash_history
2000 .bash_history
$ history | wc -l
1000
```
### 总结
你始终可以决定要保留多少命令历史记录以及哪些记录不需要,以使命令汇总最有用。
加入 [Facebook][1] 和 [LinkedIn][2] 上的 Network World 社区,评论热门主题。
--------------------------------------------------------------------------------
via: https://www.networkworld.com/article/3567050/summarizing-your-command-usage-on-linux.html
作者:[Sandra Henry-Stocker][a]
选题:[lujun9972][b]
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.networkworld.com/author/Sandra-Henry_Stocker/
[b]: https://github.com/lujun9972
[1]: https://www.facebook.com/NetworkWorld/
[2]: https://www.linkedin.com/company/network-world