六个优雅的 Linux 命令行技巧

六个优雅的 Linux 命令行技巧

一些非常有用的命令能让命令行的生活更满足,使用 Linux 命令工作可以获得许多乐趣,但是如果您使用一些命令,它们可以减少您的工作或以有趣的方式显示信息时,您将获得更多的乐趣。在今天的文章中,我们将介绍六个命令,它们可能会使你用在命令行上的时间更加值当。

 

watch

watch 命令会重复运行您给出的任何命令,并显示输出。默认情况下,它每两秒运行一次命令。命令的每次运行都将覆盖上一次运行时显示的内容,因此您始终可以看到最新的数据。

您可能会在等待某人登录时使用它。在这种情况下,您可以使用 watch who 命令或者 watch -n 15 who 命令使每 15 秒运行一次,而不是两秒一次。另外终端窗口的右上角会显示日期和时间。

  1. <span class="pln">$ </span><span class="kwd">watch</span><span class="pun">-</span><span class="pln">n </span><span class="lit">5</span><span class="kwd">who</span>
  2. <span class="typ">Every</span><span class="lit">5.0s</span><span class="pun">:</span><span class="kwd">who</span><span class="pln"> stinkbug</span><span class="pun">:</span><span class="typ">Wed</span><span class="typ">Aug</span><span class="lit">23</span><span class="lit">14</span><span class="pun">:</span><span class="lit">52</span><span class="pun">:</span><span class="lit">15</span><span class="lit">2017</span>
  3. <span class="pln">shs pts</span><span class="pun">/</span><span class="lit">0</span><span class="lit">2017</span><span class="pun">-</span><span class="lit">08</span><span class="pun">-</span><span class="lit">23</span><span class="lit">14</span><span class="pun">:</span><span class="lit">45</span><span class="pun">(</span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">0.11</span><span class="pun">)</span>
  4. <span class="pln">zoe pts</span><span class="pun">/</span><span class="lit">1</span><span class="lit">2017</span><span class="pun">-</span><span class="lit">08</span><span class="pun">-</span><span class="lit">23</span><span class="lit">08</span><span class="pun">:</span><span class="lit">15</span><span class="pun">(</span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">0.19</span><span class="pun">)</span>

您也可以使用它来查看日志文件。如果您显示的数据没有任何变化,则只有窗口角落里的日期和时间会发生变化。

  1. <span class="pln">$ </span><span class="kwd">watch</span><span class="kwd">tail</span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">log</span><span class="pun">/</span><span class="pln">syslog</span>
  2. <span class="typ">Every</span><span class="lit">2.0s</span><span class="pun">:</span><span class="kwd">tail</span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">log</span><span class="pun">/</span><span class="pln">syslog stinkbug</span><span class="pun">:</span><span class="typ">Wed</span><span class="typ">Aug</span><span class="lit">23</span><span class="lit">15</span><span class="pun">:</span><span class="lit">16</span><span class="pun">:</span><span class="lit">37</span><span class="lit">2017</span>
  3. <span class="typ">Aug</span><span class="lit">23</span><span class="lit">14</span><span class="pun">:</span><span class="lit">45</span><span class="pun">:</span><span class="lit">01</span><span class="pln"> stinkbug CRON</span><span class="pun">[</span><span class="lit">7214</span><span class="pun">]:</span><span class="pun">(</span><span class="pln">root</span><span class="pun">)</span><span class="pln"> CMD </span><span class="pun">(</span><span class="pln">command </span><span class="pun">-</span><span class="pln">v debian</span><span class="pun">-</span><span class="pln">sa1 </span><span class="pun">></span><span class="str">/dev/</span><span class="pln">nu</span>
  4. <span class="pln">ll </span><span class="pun">&&</span><span class="pln"> debian</span><span class="pun">-</span><span class="pln">sa1 </span><span class="lit">1</span><span class="lit">1</span><span class="pun">)</span>
  5. <span class="typ">Aug</span><span class="lit">23</span><span class="lit">14</span><span class="pun">:</span><span class="lit">45</span><span class="pun">:</span><span class="lit">17</span><span class="pln"> stinkbug </span><span class="kwd">systemd</span><span class="pun">[</span><span class="lit">1</span><span class="pun">]:</span><span class="typ">Started</span><span class="typ">Session</span><span class="lit">179</span><span class="pln"> of user shs</span><span class="pun">.</span>
  6. <span class="typ">Aug</span><span class="lit">23</span><span class="lit">14</span><span class="pun">:</span><span class="lit">55</span><span class="pun">:</span><span class="lit">01</span><span class="pln"> stinkbug CRON</span><span class="pun">[</span><span class="lit">7577</span><span class="pun">]:</span><span class="pun">(</span><span class="pln">root</span><span class="pun">)</span><span class="pln"> CMD </span><span class="pun">(</span><span class="pln">command </span><span class="pun">-</span><span class="pln">v debian</span><span class="pun">-</span><span class="pln">sa1 </span><span class="pun">></span><span class="str">/dev/</span><span class="pln">nu</span>
  7. <span class="pln">ll </span><span class="pun">&&</span><span class="pln"> debian</span><span class="pun">-</span><span class="pln">sa1 </span><span class="lit">1</span><span class="lit">1</span><span class="pun">)</span>
  8. <span class="typ">Aug</span><span class="lit">23</span><span class="lit">15</span><span class="pun">:</span><span class="lit">05</span><span class="pun">:</span><span class="lit">01</span><span class="pln"> stinkbug CRON</span><span class="pun">[</span><span class="lit">7582</span><span class="pun">]:</span><span class="pun">(</span><span class="pln">root</span><span class="pun">)</span><span class="pln"> CMD </span><span class="pun">(</span><span class="pln">command </span><span class="pun">-</span><span class="pln">v debian</span><span class="pun">-</span><span class="pln">sa1 </span><span class="pun">></span><span class="str">/dev/</span><span class="pln">nu</span>
  9. <span class="pln">ll </span><span class="pun">&&</span><span class="pln"> debian</span><span class="pun">-</span><span class="pln">sa1 </span><span class="lit">1</span><span class="lit">1</span><span class="pun">)</span>
  10. <span class="typ">Aug</span><span class="lit">23</span><span class="lit">15</span><span class="pun">:</span><span class="lit">08</span><span class="pun">:</span><span class="lit">48</span><span class="pln"> stinkbug </span><span class="kwd">systemd</span><span class="pun">[</span><span class="lit">1</span><span class="pun">]:</span><span class="typ">Starting</span><span class="typ">Cleanup</span><span class="pln"> of </span><span class="typ">Temporary</span><span class="typ">Directories</span><span class="pun">...</span>
  11. <span class="typ">Aug</span><span class="lit">23</span><span class="lit">15</span><span class="pun">:</span><span class="lit">08</span><span class="pun">:</span><span class="lit">48</span><span class="pln"> stinkbug </span><span class="kwd">systemd</span><span class="pun">-</span><span class="pln">tmpfiles</span><span class="pun">[</span><span class="lit">7584</span><span class="pun">]:</span><span class="pun">[</span><span class="str">/usr/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">tmpfiles</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="kwd">var</span><span class="pun">.</span><span class="pln">conf</span><span class="pun">:</span><span class="lit">1</span>
  12. <span class="lit">4</span><span class="pun">]</span><span class="typ">Duplicate</span><span class="pln"> line </span><span class="kwd">for</span><span class="pln"> path </span><span class="str">"/var/log"</span><span class="pun">,</span><span class="pln"> ignoring</span><span class="pun">.</span>
  13. <span class="typ">Aug</span><span class="lit">23</span><span class="lit">15</span><span class="pun">:</span><span class="lit">08</span><span class="pun">:</span><span class="lit">48</span><span class="pln"> stinkbug </span><span class="kwd">systemd</span><span class="pun">[</span><span class="lit">1</span><span class="pun">]:</span><span class="typ">Started</span><span class="typ">Cleanup</span><span class="pln"> of </span><span class="typ">Temporary</span><span class="typ">Directories</span><span class="pun">.</span>
  14. <span class="typ">Aug</span><span class="lit">23</span><span class="lit">15</span><span class="pun">:</span><span class="lit">13</span><span class="pun">:</span><span class="lit">41</span><span class="pln"> stinkbug </span><span class="kwd">systemd</span><span class="pun">[</span><span class="lit">1</span><span class="pun">]:</span><span class="typ">Started</span><span class="typ">Session</span><span class="lit">182</span><span class="pln"> of user shs</span><span class="pun">.</span>
  15. <span class="typ">Aug</span><span class="lit">23</span><span class="lit">15</span><span class="pun">:</span><span class="lit">14</span><span class="pun">:</span><span class="lit">29</span><span class="pln"> stinkbug </span><span class="kwd">systemd</span><span class="pun">[</span><span class="lit">1</span><span class="pun">]:</span><span class="typ">Started</span><span class="typ">Session</span><span class="lit">183</span><span class="pln"> of user shs</span><span class="pun">.</span>
  16. <span class="typ">Aug</span><span class="lit">23</span><span class="lit">15</span><span class="pun">:</span><span class="lit">15</span><span class="pun">:</span><span class="lit">01</span><span class="pln"> stinkbug CRON</span><span class="pun">[</span><span class="lit">7828</span><span class="pun">]:</span><span class="pun">(</span><span class="pln">root</span><span class="pun">)</span><span class="pln"> CMD </span><span class="pun">(</span><span class="pln">command </span><span class="pun">-</span><span class="pln">v debian</span><span class="pun">-</span><span class="pln">sa1 </span><span class="pun">></span><span class="str">/dev/</span><span class="pln">nu</span>
  17. <span class="pln">ll </span><span class="pun">&&</span><span class="pln"> debian</span><span class="pun">-</span><span class="pln">sa1 </span><span class="lit">1</span><span class="lit">1</span><span class="pun">)</span>

这里的输出和使用命令 tail -f /var/log/syslog 的���出相似。

 

look

这个命令的名字 look 可能会让我们以为它和 watch 做类似的事情,但其实是不同的。look 命令用于搜索以某个特定字符串开头的单词。

  1. <span class="pln">$ </span><span class="kwd">look</span><span class="pln"> ecl</span>
  2. <span class="pln">eclectic</span>
  3. <span class="pln">eclectic</span><span class="str">'s</span>
  4. <span class="str">eclectically</span>
  5. <span class="str">eclecticism</span>
  6. <span class="str">eclecticism'</span><span class="pln">s</span>
  7. <span class="pln">eclectics</span>
  8. <span class="pln">eclipse</span>
  9. <span class="pln">eclipse</span><span class="str">'s</span>
  10. <span class="str">eclipsed</span>
  11. <span class="str">eclipses</span>
  12. <span class="str">eclipsing</span>
  13. <span class="str">ecliptic</span>
  14. <span class="str">ecliptic'</span><span class="pln">s</span>

look 命令通常有助于单词的拼写,它使用 /usr/share/dict/words 文件,除非你使用如下的命令指定了文件名:

  1. <span class="pln">$ </span><span class="kwd">look</span><span class="kwd">esac</span><span class="pun">.</span><span class="pln">bashrc</span>
  2. <span class="kwd">esac</span>
  3. <span class="kwd">esac</span>
  4. <span class="kwd">esac</span>

在这种情况下,它的作用就像跟在一个 awk 命令后面的 grep ,只打印匹配行上的第一个单词。

 

man -k

man -k 命令列出包含指定单词的手册页。它的工作基本上和 apropos 命令一样。

  1. <span class="pln">$ </span><span class="kwd">man</span><span class="pun">-</span><span class="pln">k logrotate</span>
  2. <span class="pln">dh_installlogrotate </span><span class="pun">(</span><span class="lit">1</span><span class="pun">)</span><span class="pun">-</span><span class="pln"> install logrotate config files</span>
  3. <span class="pln">logrotate </span><span class="pun">(</span><span class="lit">8</span><span class="pun">)</span><span class="pun">-</span><span class="pln"> rotates</span><span class="pun">,</span><span class="pln"> compresses</span><span class="pun">,</span><span class="kwd">and</span><span class="pln"> mails system logs</span>
  4. <span class="pln">logrotate</span><span class="pun">.</span><span class="pln">conf </span><span class="pun">(</span><span class="lit">5</span><span class="pun">)</span><span class="pun">-</span><span class="pln"> rotates</span><span class="pun">,</span><span class="pln"> compresses</span><span class="pun">,</span><span class="kwd">and</span><span class="pln"> mails system logs</span>

 

help

当你完全绝望的时候,您可能会试图使用此命令,help 命令实际上是显示一个 shell 内置命令的列表。最令人惊讶的是它有相当多的参数变量。你可能会看到这样的东西,然后开始想知道这些内置功能可以为你做些什么:

  1. <span class="pln">$ help</span>
  2. <span class="pln">GNU </span><span class="kwd">bash</span><span class="pun">,</span><span class="pln"> version </span><span class="lit">4.4</span><span class="pun">.</span><span class="lit">7</span><span class="pun">(</span><span class="lit">1</span><span class="pun">)-</span><span class="pln">release </span><span class="pun">(</span><span class="pln">i686</span><span class="pun">-</span><span class="pln">pc</span><span class="pun">-</span><span class="pln">linux</span><span class="pun">-</span><span class="pln">gnu</span><span class="pun">)</span>
  3. <span class="typ">These</span><span class="pln"> shell commands are </span><span class="kwd">defined</span><span class="pln"> internally</span><span class="pun">.</span><span class="typ">Type</span><span class="str">`help' to see this list.</span>
  4. <span class="str">Type `</span><span class="pln">help name</span><span class="str">' to find out more about the function `name'</span><span class="pun">.</span>
  5. <span class="typ">Use</span><span class="str">`info bash' to find out more about the shell in general.</span>
  6. <span class="str">Use `</span><span class="kwd">man</span><span class="pun">-</span><span class="pln">k</span><span class="str">' or `info'</span><span class="pln"> to </span><span class="kwd">find</span><span class="pln"> out </span><span class="kwd">more</span><span class="pln"> about commands </span><span class="kwd">not</span><span class="kwd">in</span><span class="kwd">this</span><span class="kwd">list</span><span class="pun">.</span>
  7. <span class="pln">A star </span><span class="pun">(*)</span><span class="kwd">next</span><span class="pln"> to a name means that the command </span><span class="kwd">is</span><span class="pln"> disabled</span><span class="pun">.</span>
  8. <span class="pln">job_spec </span><span class="pun">[&]</span><span class="pln"> history </span><span class="pun">[-</span><span class="pln">c</span><span class="pun">]</span><span class="pun">[-</span><span class="pln">d offset</span><span class="pun">]</span><span class="pun">[</span><span class="pln">n</span><span class="pun">]</span><span class="kwd">or</span><span class="pln"> hist</span><span class="pun">></span>
  9. <span class="pun">((</span><span class="pln"> expression </span><span class="pun">))</span><span class="kwd">if</span><span class="pln"> COMMANDS</span><span class="pun">;</span><span class="kwd">then</span><span class="pln"> COMMANDS</span><span class="pun">;</span><span class="pun">[</span><span class="kwd">elif</span><span class="pln"> C</span><span class="pun">></span>
  10. <span class="pun">.</span><span class="pln"> filename </span><span class="pun">[</span><span class="pln">arguments</span><span class="pun">]</span><span class="pln"> jobs </span><span class="pun">[-</span><span class="pln">lnprs</span><span class="pun">]</span><span class="pun">[</span><span class="pln">jobspec </span><span class="pun">...]</span><span class="kwd">or</span><span class="pln"> jobs </span><span class="pun">></span>
  11. <span class="pun">:</span><span class="kwd">kill</span><span class="pun">[-</span><span class="pln">s sigspec </span><span class="pun">|</span><span class="pun">-</span><span class="pln">n signum </span><span class="pun">|</span><span class="pun">-</span><span class="pln">sigs</span><span class="pun">></span>
  12. <span class="pun">[</span><span class="pln"> arg</span><span class="pun">...</span><span class="pun">]</span><span class="pln"> let arg </span><span class="pun">[</span><span class="pln">arg </span><span class="pun">...]</span>
  13. <span class="pun">[[</span><span class="pln"> expression </span><span class="pun">]]</span><span class="kwd">local</span><span class="pun">[</span><span class="pln">option</span><span class="pun">]</span><span class="pln"> name</span><span class="pun">[=</span><span class="pln">value</span><span class="pun">]</span><span class="pun">...</span>
  14. <span class="kwd">alias</span><span class="pun">[-</span><span class="pln">p</span><span class="pun">]</span><span class="pun">[</span><span class="pln">name</span><span class="pun">[=</span><span class="pln">value</span><span class="pun">]</span><span class="pun">...</span><span class="pun">]</span><span class="pln"> logout </span><span class="pun">[</span><span class="pln">n</span><span class="pun">]</span>
  15. <span class="pln">bg </span><span class="pun">[</span><span class="pln">job_spec </span><span class="pun">...]</span><span class="pln"> mapfile </span><span class="pun">[-</span><span class="pln">d delim</span><span class="pun">]</span><span class="pun">[-</span><span class="pln">n count</span><span class="pun">]</span><span class="pun">[-</span><span class="pln">O </span><span class="kwd">or</span><span class="pun">></span>
  16. <span class="pln">bind </span><span class="pun">[-</span><span class="pln">lpsvPSVX</span><span class="pun">]</span><span class="pun">[-</span><span class="pln">m keymap</span><span class="pun">]</span><span class="pun">[-</span><span class="pln">f </span><span class="kwd">file</span><span class="pun">></span><span class="pln"> popd </span><span class="pun">[-</span><span class="pln">n</span><span class="pun">]</span><span class="pun">[+</span><span class="pln">N </span><span class="pun">|</span><span class="pun">-</span><span class="pln">N</span><span class="pun">]</span>
  17. <span class="kwd">break</span><span class="pun">[</span><span class="pln">n</span><span class="pun">]</span><span class="kwd">printf</span><span class="pun">[-</span><span class="pln">v </span><span class="kwd">var</span><span class="pun">]</span><span class="pln"> format </span><span class="pun">[</span><span class="pln">arguments</span><span class="pun">]</span>
  18. <span class="pln">builtin </span><span class="pun">[</span><span class="pln">shell</span><span class="pun">-</span><span class="pln">builtin </span><span class="pun">[</span><span class="pln">arg </span><span class="pun">...]]</span><span class="pln"> pushd </span><span class="pun">[-</span><span class="pln">n</span><span class="pun">]</span><span class="pun">[+</span><span class="pln">N </span><span class="pun">|</span><span class="pun">-</span><span class="pln">N </span><span class="pun">|</span><span class="kwd">dir</span><span class="pun">]</span>
  19. <span class="kwd">caller</span><span class="pun">[</span><span class="kwd">expr</span><span class="pun">]</span><span class="kwd">pwd</span><span class="pun">[-</span><span class="pln">LP</span><span class="pun">]</span>
  20. <span class="kwd">case</span><span class="pln"> WORD </span><span class="kwd">in</span><span class="pun">[</span><span class="pln">PATTERN </span><span class="pun">[|</span><span class="pln"> PATTERN</span><span class="pun">]...)></span><span class="pln"> read </span><span class="pun">[-</span><span class="pln">ers</span><span class="pun">]</span><span class="pun">[-</span><span class="pln">a </span><span class="kwd">array</span><span class="pun">]</span><span class="pun">[-</span><span class="pln">d delim</span><span class="pun">]</span><span class="pun">[-></span>
  21. <span class="kwd">cd</span><span class="pun">[-</span><span class="pln">L</span><span class="pun">|[-</span><span class="pln">P </span><span class="pun">[-</span><span class="pln">e</span><span class="pun">]]</span><span class="pun">[-@]]</span><span class="pun">[</span><span class="kwd">dir</span><span class="pun">]</span><span class="pln"> readarray </span><span class="pun">[-</span><span class="pln">n count</span><span class="pun">]</span><span class="pun">[-</span><span class="pln">O origin</span><span class="pun">]</span><span class="pun">[-</span><span class="pln">s</span><span class="pun">></span>
  22. <span class="pln">command </span><span class="pun">[-</span><span class="pln">pVv</span><span class="pun">]</span><span class="pln"> command </span><span class="pun">[</span><span class="pln">arg </span><span class="pun">...]</span><span class="pln"> readonly </span><span class="pun">[-</span><span class="pln">aAf</span><span class="pun">]</span><span class="pun">[</span><span class="pln">name</span><span class="pun">[=</span><span class="pln">value</span><span class="pun">]</span><span class="pun">...]</span><span class="pln"> o</span><span class="pun">></span>
  23. <span class="pln">compgen </span><span class="pun">[-</span><span class="pln">abcdefgjksuv</span><span class="pun">]</span><span class="pun">[-</span><span class="pln">o option</span><span class="pun">]</span><span class="pun">[></span><span class="kwd">return</span><span class="pun">[</span><span class="pln">n</span><span class="pun">]</span>
  24. <span class="pln">complete </span><span class="pun">[-</span><span class="pln">abcdefgjksuv</span><span class="pun">]</span><span class="pun">[-</span><span class="kwd">pr</span><span class="pun">]</span><span class="pun">[-</span><span class="pln">DE</span><span class="pun">]</span><span class="pun">></span><span class="pln"> select NAME </span><span class="pun">[</span><span class="kwd">in</span><span class="pln"> WORDS </span><span class="pun">...</span><span class="pun">;]</span><span class="kwd">do</span><span class="pln"> COMM</span><span class="pun">></span>
  25. <span class="pln">compopt </span><span class="pun">[-</span><span class="pln">o</span><span class="pun">|+</span><span class="pln">o option</span><span class="pun">]</span><span class="pun">[-</span><span class="pln">DE</span><span class="pun">]</span><span class="pun">[</span><span class="pln">name </span><span class="pun">..></span><span class="kwd">set</span><span class="pun">[-</span><span class="pln">abefhkmnptuvxBCHP</span><span class="pun">]</span><span class="pun">[-</span><span class="pln">o option</span><span class="pun">-></span>
  26. <span class="kwd">continue</span><span class="pun">[</span><span class="pln">n</span><span class="pun">]</span><span class="pln"> shift </span><span class="pun">[</span><span class="pln">n</span><span class="pun">]</span>
  27. <span class="pln">coproc </span><span class="pun">[</span><span class="pln">NAME</span><span class="pun">]</span><span class="pln"> command </span><span class="pun">[</span><span class="pln">redirections</span><span class="pun">]</span><span class="pln"> shopt </span><span class="pun">[-</span><span class="pln">pqsu</span><span class="pun">]</span><span class="pun">[-</span><span class="pln">o</span><span class="pun">]</span><span class="pun">[</span><span class="pln">optname </span><span class="pun">...]</span>
  28. <span class="kwd">declare</span><span class="pun">[-</span><span class="pln">aAfFgilnrtux</span><span class="pun">]</span><span class="pun">[-</span><span class="pln">p</span><span class="pun">]</span><span class="pun">[</span><span class="pln">name</span><span class="pun">[=</span><span class="pln">v</span><span class="pun">></span><span class="pln"> source filename </span><span class="pun">[</span><span class="pln">arguments</span><span class="pun">]</span>
  29. <span class="pln">dirs </span><span class="pun">[-</span><span class="pln">clpv</span><span class="pun">]</span><span class="pun">[+</span><span class="pln">N</span><span class="pun">]</span><span class="pun">[-</span><span class="pln">N</span><span class="pun">]</span><span class="pln"> suspend </span><span class="pun">[-</span><span class="pln">f</span><span class="pun">]</span>
  30. <span class="pln">disown </span><span class="pun">[-</span><span class="pln">h</span><span class="pun">]</span><span class="pun">[-</span><span class="pln">ar</span><span class="pun">]</span><span class="pun">[</span><span class="pln">jobspec </span><span class="pun">...</span><span class="pun">|</span><span class="pln"> pid </span><span class="pun">></span><span class="kwd">test</span><span class="pun">[</span><span class="kwd">expr</span><span class="pun">]</span>
  31. <span class="kwd">echo</span><span class="pun">[-</span><span class="pln">neE</span><span class="pun">]</span><span class="pun">[</span><span class="pln">arg </span><span class="pun">...]</span><span class="kwd">time</span><span class="pun">[-</span><span class="pln">p</span><span class="pun">]</span><span class="pln"> pipeline</span>
  32. <span class="pln">enable </span><span class="pun">[-</span><span class="pln">a</span><span class="pun">]</span><span class="pun">[-</span><span class="pln">dnps</span><span class="pun">]</span><span class="pun">[-</span><span class="pln">f filename</span><span class="pun">]</span><span class="pun">[</span><span class="pln">na</span><span class="pun">></span><span class="pln"> times</span>
  33. <span class="kwd">eval</span><span class="pun">[</span><span class="pln">arg </span><span class="pun">...]</span><span class="pln"> trap </span><span class="pun">[-</span><span class="kwd">lp</span><span class="pun">]</span><span class="pun">[[</span><span class="pln">arg</span><span class="pun">]</span><span class="pln"> signal_spec </span><span class="pun">...]</span>
  34. <span class="kwd">exec</span><span class="pun">[-</span><span class="pln">cl</span><span class="pun">]</span><span class="pun">[-</span><span class="pln">a name</span><span class="pun">]</span><span class="pun">[</span><span class="pln">command </span><span class="pun">[</span><span class="pln">argume</span><span class="pun">></span><span class="kwd">true</span>
  35. <span class="kwd">exit</span><span class="pun">[</span><span class="pln">n</span><span class="pun">]</span><span class="pln"> type </span><span class="pun">[-</span><span class="pln">afptP</span><span class="pun">]</span><span class="pln"> name </span><span class="pun">[</span><span class="pln">name </span><span class="pun">...]</span>
  36. <span class="kwd">export</span><span class="pun">[-</span><span class="pln">fn</span><span class="pun">]</span><span class="pun">[</span><span class="pln">name</span><span class="pun">[=</span><span class="pln">value</span><span class="pun">]</span><span class="pun">...]</span><span class="kwd">or</span><span class="pln"> ex</span><span class="pun">></span><span class="pln"> typeset </span><span class="pun">[-</span><span class="pln">aAfFgilnrtux</span><span class="pun">]</span><span class="pun">[-</span><span class="pln">p</span><span class="pun">]</span><span class="pln"> name</span><span class="pun">[=</span><span class="pln">v</span><span class="pun">></span>
  37. <span class="kwd">false</span><span class="pln"> ulimit </span><span class="pun">[-</span><span class="typ">SHabcdefiklmnpqrstuvxPT</span><span class="pun">]</span><span class="pun">[</span><span class="pln">l</span><span class="pun">></span>
  38. <span class="pln">fc </span><span class="pun">[-</span><span class="pln">e ename</span><span class="pun">]</span><span class="pun">[-</span><span class="pln">lnr</span><span class="pun">]</span><span class="pun">[</span><span class="pln">first</span><span class="pun">]</span><span class="pun">[</span><span class="kwd">last</span><span class="pun">]</span><span class="pln"> o</span><span class="pun">></span><span class="pln"> umask </span><span class="pun">[-</span><span class="pln">p</span><span class="pun">]</span><span class="pun">[-</span><span class="pln">S</span><span class="pun">]</span><span class="pun">[</span><span class="pln">mode</span><span class="pun">]</span>
  39. <span class="pln">fg </span><span class="pun">[</span><span class="pln">job_spec</span><span class="pun">]</span><span class="pln"> unalias </span><span class="pun">[-</span><span class="pln">a</span><span class="pun">]</span><span class="pln"> name </span><span class="pun">[</span><span class="pln">name </span><span class="pun">...]</span>
  40. <span class="kwd">for</span><span class="pln"> NAME </span><span class="pun">[</span><span class="kwd">in</span><span class="pln"> WORDS </span><span class="pun">...</span><span class="pun">]</span><span class="pun">;</span><span class="kwd">do</span><span class="pln"> COMMAND</span><span class="pun">></span><span class="kwd">unset</span><span class="pun">[-</span><span class="pln">f</span><span class="pun">]</span><span class="pun">[-</span><span class="pln">v</span><span class="pun">]</span><span class="pun">[-</span><span class="pln">n</span><span class="pun">]</span><span class="pun">[</span><span class="pln">name </span><span class="pun">...]</span>
  41. <span class="kwd">for</span><span class="pun">((</span><span class="pln"> exp1</span><span class="pun">;</span><span class="pln"> exp2</span><span class="pun">;</span><span class="pln"> exp3 </span><span class="pun">));</span><span class="kwd">do</span><span class="pln"> COMMAN</span><span class="pun">></span><span class="kwd">until</span><span class="pln"> COMMANDS</span><span class="pun">;</span><span class="kwd">do</span><span class="pln"> COMMANDS</span><span class="pun">;</span><span class="kwd">done</span>
  42. <span class="kwd">function</span><span class="pln"> name </span><span class="pun">{</span><span class="pln"> COMMANDS </span><span class="pun">;</span><span class="pun">}</span><span class="kwd">or</span><span class="pln"> name </span><span class="pun">></span><span class="pln"> variables </span><span class="pun">-</span><span class="typ">Names</span><span class="kwd">and</span><span class="pln"> meanings of so</span><span class="pun">></span>
  43. <span class="pln">getopts optstring name </span><span class="pun">[</span><span class="pln">arg</span><span class="pun">]</span><span class="pln"> wait </span><span class="pun">[-</span><span class="pln">n</span><span class="pun">]</span><span class="pun">[</span><span class="kwd">id</span><span class="pun">...]</span>
  44. <span class="pln">hash </span><span class="pun">[-</span><span class="pln">lr</span><span class="pun">]</span><span class="pun">[-</span><span class="pln">p pathname</span><span class="pun">]</span><span class="pun">[-</span><span class="pln">dt</span><span class="pun">]</span><span class="pun">[</span><span class="pln">name </span><span class="pun">></span><span class="kwd">while</span><span class="pln"> COMMANDS</span><span class="pun">;</span><span class="kwd">do</span><span class="pln"> COMMANDS</span><span class="pun">;</span><span class="kwd">done</span>
  45. <span class="pln">help </span><span class="pun">[-</span><span class="pln">dms</span><span class="pun">]</span><span class="pun">[</span><span class="pln">pattern </span><span class="pun">...]</span><span class="pun">{</span><span class="pln"> COMMANDS </span><span class="pun">;</span><span class="pun">}</span>

 

stat -c

stat 命令用于显示文件的大小、所有者、用户组、索引节点号、权限、修改和访问时间等重要的统计信息。这是一个非常有用的命令,可以显示比 ls -l 更多的细节。

  1. <span class="pln">$ stat </span><span class="pun">.</span><span class="pln">bashrc</span>
  2. <span class="typ">File</span><span class="pun">:</span><span class="pun">.</span><span class="pln">bashrc</span>
  3. <span class="typ">Size</span><span class="pun">:</span><span class="lit">4048</span><span class="typ">Blocks</span><span class="pun">:</span><span class="lit">8</span><span class="pln"> IO </span><span class="typ">Block</span><span class="pun">:</span><span class="lit">4096</span><span class="pln"> regular </span><span class="kwd">file</span>
  4. <span class="typ">Device</span><span class="pun">:</span><span class="lit">806h</span><span class="pun">/</span><span class="lit">2054d</span><span class="typ">Inode</span><span class="pun">:</span><span class="lit">421481</span><span class="typ">Links</span><span class="pun">:</span><span class="lit">1</span>
  5. <span class="typ">Access</span><span class="pun">:</span><span class="pun">(</span><span class="lit">0644</span><span class="pun">/-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">r</span><span class="pun">--</span><span class="pln">r</span><span class="pun">--)</span><span class="typ">Uid</span><span class="pun">:</span><span class="pun">(</span><span class="lit">1000</span><span class="pun">/</span><span class="pln"> shs</span><span class="pun">)</span><span class="typ">Gid</span><span class="pun">:</span><span class="pun">(</span><span class="lit">1000</span><span class="pun">/</span><span class="pln"> shs</span><span class="pun">)</span>
  6. <span class="typ">Access</span><span class="pun">:</span><span class="lit">2017</span><span class="pun">-</span><span class="lit">08</span><span class="pun">-</span><span class="lit">23</span><span class="lit">15</span><span class="pun">:</span><span class="lit">13</span><span class="pun">:</span><span class="lit">41.781809933</span><span class="pun">-</span><span class="lit">0400</span>
  7. <span class="typ">Modify</span><span class="pun">:</span><span class="lit">2017</span><span class="pun">-</span><span class="lit">06</span><span class="pun">-</span><span class="lit">21</span><span class="lit">17</span><span class="pun">:</span><span class="lit">37</span><span class="pun">:</span><span class="lit">11.875157790</span><span class="pun">-</span><span class="lit">0400</span>
  8. <span class="typ">Change</span><span class="pun">:</span><span class="lit">2017</span><span class="pun">-</span><span class="lit">06</span><span class="pun">-</span><span class="lit">21</span><span class="lit">17</span><span class="pun">:</span><span class="lit">37</span><span class="pun">:</span><span class="lit">11.899157791</span><span class="pun">-</span><span class="lit">0400</span>
  9. <span class="typ">Birth</span><span class="pun">:</span><span class="pun">-</span>

使用 -c 选项,您可以指定要查看的字段。例如,如果您只想查看一个文件或一系列文件的文件名和访问权限,则可以这样做:

  1. <span class="pln">$ stat </span><span class="pun">-</span><span class="pln">c </span><span class="str">'%n %a'</span><span class="pun">.</span><span class="pln">bashrc</span>
  2. <span class="pun">.</span><span class="pln">bashrc </span><span class="lit">644</span>

在此命令中, %n 表示每个文件的名称,而 %a 表示访问权限。%u 表示数字类型的 UID,而 %u 表示用户名。

  1. <span class="pln">$ stat </span><span class="pun">-</span><span class="pln">c </span><span class="str">'%n %a'</span><span class="pln"> bin</span><span class="com">/*</span>
  2. <span class="com">bin/loop 700</span>
  3. <span class="com">bin/move2nohup 700</span>
  4. <span class="com">bin/nohup.out 600</span>
  5. <span class="com">bin/show_release 700</span>
  6. <span class="com">$ stat -c '%n %a %U' bin/*</span>
  7. <span class="com">bin/loop 700

相关推荐