5 个在 Linux 中管理文件类型和系统时间的有用命令

对于想学习 Linux 的初学者来说要适应使用命令行或者终端可能非常困难。由于终端比图形用户界面程序更能帮助用户控制 Linux 系统,我们必须习惯在终端中运行命令。因此为了有效记忆 Linux 不同的命令,你应该每天使用终端并明白怎样将命令和不同选项以及参数一同使用。

5 个在 Linux 中管理文件类型和系统时间的有用命令

在 Linux 中管理文件类型和设置时间

请先查看我们 Linux 小技巧系列之前的文章:

  • 5 个有趣的 Linux 命令行技巧
  • 给新手的 10 个有用 Linux 命令行技巧

在这篇文章中,我们打算看看终端中 5 个和文件以及时间相关的提示和技巧。

 

Linux 中的文件类型

在 Linux 中,一切皆文件,你的设备、目录以及普通文件都认为是文件。

Linux 系统中文件有不同的类型:

  • 普通文件:可能包含命令、文档、音频文件、视频、图像,归档文件等。
  • 设备文件:系统用于访问你硬件组件。

这里有两种表示存储设备的设备文件:块文件,例如硬盘,它们以块读取数据;字符文件,以逐个字符读取数据。

  • 硬链接和软链接:用于在 Linux 文件系统的任意地方访问文件。
  • 命名管道和套接字:允许不同的进程之间进行交互。

1. 用 ‘file’ 命令确定文件类型

你可以像下面这样使用 file 命令确定文件的类型。下面的截图显示了用 file 命令确定不同文件类型的例子。

  1. <span class="pln">linuxidc@linuxidc </span><span class="pun">~/</span><span class="typ">Linux</span><span class="pun">-</span><span class="typ">Tricks</span><span class="pln"> $ </span><span class="kwd">dir</span>
  2. <span class="pln">BACKUP master</span><span class="pun">.</span><span class="pln">zip</span>
  3. <span class="pln">crossroads</span><span class="pun">-</span><span class="pln">stable</span><span class="pun">.</span><span class="kwd">tar</span><span class="pun">.</span><span class="pln">gz num</span><span class="pun">.</span><span class="pln">txt</span>
  4. <span class="pln">EDWARD</span><span class="pun">-</span><span class="pln">MAYA</span><span class="pun">-</span><span class="lit">2011</span><span class="pun">-</span><span class="lit">2012</span><span class="pun">-</span><span class="pln">NEW</span><span class="pun">-</span><span class="pln">REMIX</span><span class="pun">.</span><span class="pln">mp3 reggea</span><span class="pun">.</span><span class="pln">xspf</span>
  5. <span class="typ">Linux</span><span class="pun">-</span><span class="typ">Security</span><span class="pun">-</span><span class="typ">Optimization</span><span class="pun">-</span><span class="typ">Book</span><span class="pun">.</span><span class="pln">gif tmp</span><span class="pun">-</span><span class="kwd">link</span>
  6. <span class="pln">linuxidc@linuxidc </span><span class="pun">~</span><span class="str">/Linux-Tricks $ file BACKUP/</span>
  7. <span class="pln">BACKUP</span><span class="pun">/:</span><span class="pln"> directory </span>
  8. <span class="pln">linuxidc@linuxidc </span><span class="pun">~/</span><span class="typ">Linux</span><span class="pun">-</span><span class="typ">Tricks</span><span class="pln"> $ </span><span class="kwd">file</span><span class="pln"> master</span><span class="pun">.</span><span class="pln">zip </span>
  9. <span class="pln">master</span><span class="pun">.</span><span class="pln">zip</span><span class="pun">:</span><span class="typ">Zip</span><span class="pln"> archive data</span><span class="pun">,</span><span class="pln"> at least v1</span><span class="pun">.</span><span class="lit">0</span><span class="pln"> to extract</span>
  10. <span class="pln">linuxidc@linuxidc </span><span class="pun">~/</span><span class="typ">Linux</span><span class="pun">-</span><span class="typ">Tricks</span><span class="pln"> $ </span><span class="kwd">file</span><span class="pln"> crossroads</span><span class="pun">-</span><span class="pln">stable</span><span class="pun">.</span><span class="kwd">tar</span><span class="pun">.</span><span class="pln">gz</span>
  11. <span class="pln">crossroads</span><span class="pun">-</span><span class="pln">stable</span><span class="pun">.</span><span class="kwd">tar</span><span class="pun">.</span><span class="pln">gz</span><span class="pun">:</span><span class="pln"> gzip compressed data</span><span class="pun">,</span><span class="kwd">from</span><span class="typ">Unix</span><span class="pun">,</span><span class="kwd">last</span><span class="pln"> modified</span><span class="pun">:</span><span class="typ">Tue</span><span class="typ">Apr</span><span class="lit">5</span><span class="lit">15</span><span class="pun">:</span><span class="lit">15</span><span class="pun">:</span><span class="lit">20</span><span class="lit">2011</span>
  12. <span class="pln">linuxidc@linuxidc </span><span class="pun">~/</span><span class="typ">Linux</span><span class="pun">-</span><span class="typ">Tricks</span><span class="pln"> $ </span><span class="kwd">file</span><span class="typ">Linux</span><span class="pun">-</span><span class="typ">Security</span><span class="pun">-</span><span class="typ">Optimization</span><span class="pun">-</span><span class="typ">Book</span><span class="pun">.</span><span class="pln">gif </span>
  13. <span class="typ">Linux</span><span class="pun">-</span><span class="typ">Security</span><span class="pun">-</span><span class="typ">Optimization</span><span class="pun">-</span><span class="typ">Book</span><span class="pun">.</span><span class="pln">gif</span><span class="pun">:</span><span class="pln"> GIF image data</span><span class="pun">,</span><span class="pln"> version </span><span class="lit">89a</span><span class="pun">,</span><span class="lit">200</span><span class="pln"> x </span><span class="lit">259</span>
  14. <span class="pln">linuxidc@linuxidc </span><span class="pun">~/</span><span class="typ">Linux</span><span class="pun">-</span><span class="typ">Tricks</span><span class="pln"> $ </span><span class="kwd">file</span><span class="pln"> EDWARD</span><span class="pun">-</span><span class="pln">MAYA</span><span class="pun">-</span><span class="lit">2011</span><span class="pun">-</span><span class="lit">2012</span><span class="pun">-</span><span class="pln">NEW</span><span class="pun">-</span><span class="pln">REMIX</span><span class="pun">.</span><span class="pln">mp3 </span>
  15. <span class="pln">EDWARD</span><span class="pun">-</span><span class="pln">MAYA</span><span class="pun">-</span><span class="lit">2011</span><span class="pun">-</span><span class="lit">2012</span><span class="pun">-</span><span class="pln">NEW</span><span class="pun">-</span><span class="pln">REMIX</span><span class="pun">.</span><span class="pln">mp3</span><span class="pun">:</span><span class="typ">Audio</span><span class="kwd">file</span><span class="kwd">with</span><span class="pln"> ID3 version </span><span class="lit">2.3</span><span class="pun">.</span><span class="lit">0</span><span class="pun">,</span><span class="pln"> contains</span><span class="pun">:</span><span class="pln"> MPEG ADTS</span><span class="pun">,</span><span class="pln"> layer III</span><span class="pun">,</span><span class="pln"> v1</span><span class="pun">,</span><span class="lit">192</span><span class="pln"> kbps</span><span class="pun">,</span><span class="lit">44.1</span><span class="pln"> kHz</span><span class="pun">,</span><span class="typ">JntStereo</span>
  16. <span class="pln">linuxidc@linuxidc </span><span class="pun">~</span><span class="str">/Linux-Tricks $ file /</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda1</span>
  17. <span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda1</span><span class="pun">:</span><span class="pln"> block special </span>
  18. <span class="pln">linuxidc@linuxidc </span><span class="pun">~</span><span class="str">/Linux-Tricks $ file /</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">tty1</span>
  19. <span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">tty1</span><span class="pun">:</span><span class="pln"> character special </span>

2. 用 ‘ls’ 和 ‘dir’ 命令确定文件类型

确定文件类型的另一种方式是用 ls 和 dir 命令显示一长串结果。

用 ls -l 确定一个文件的类型。

当你查看文件权限时,第一个字符显示了文件类型,其它字符显示文件权限。

  1. <span class="pln">linuxidc@linuxidc </span><span class="pun">~/</span><span class="typ">Linux</span><span class="pun">-</span><span class="typ">Tricks</span><span class="pln"> $ </span><span class="kwd">ls</span><span class="pun">-</span><span class="pln">l</span>
  2. <span class="pln">total </span><span class="lit">6908</span>
  3. <span class="pln">drwxr</span><span class="pun">-</span><span class="pln">xr</span><span class="pun">-</span><span class="pln">x </span><span class="lit">2</span><span class="pln"> linuxidc linuxidc </span><span class="lit">4096</span><span class="typ">Sep</span><span class="lit">9</span><span class="lit">11</span><span class="pun">:</span><span class="lit">46</span><span class="pln"> BACKUP</span>
  4. <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="lit">1</span><span class="pln"> linuxidc linuxidc </span><span class="lit">1075620</span><span class="typ">Sep</span><span class="lit">9</span><span class="lit">11</span><span class="pun">:</span><span class="lit">47</span><span class="pln"> crossroads</span><span class="pun">-</span><span class="pln">stable</span><span class="pun">.</span><span class="kwd">tar</span><span class="pun">.</span><span class="pln">gz</span>
  5. <span class="pun">-</span><span class="pln">rwxr</span><span class="pun">-----</span><span class="lit">1</span><span class="pln"> linuxidc linuxidc </span><span class="lit">5916085</span><span class="typ">Sep</span><span class="lit">9</span><span class="lit">11</span><span class="pun">:</span><span class="lit">49</span><span class="pln"> EDWARD</span><span class="pun">-</span><span class="pln">MAYA</span><span class="pun">-</span><span class="lit">2011</span><span class="pun">-</span><span class="lit">2012</span><span class="pun">-</span><span class="pln">NEW</span><span class="pun">-</span><span class="pln">REMIX</span><span class="pun">.</span><span class="pln">mp3</span>
  6. <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="lit">1</span><span class="pln"> linuxidc linuxidc </span><span class="lit">42122</span><span class="typ">Sep</span><span class="lit">9</span><span class="lit">11</span><span class="pun">:</span><span class="lit">49</span><span class="typ">Linux</span><span class="pun">-</span><span class="typ">Security</span><span class="pun">-</span><span class="typ">Optimization</span><span class="pun">-</span><span class="typ">Book</span><span class="pun">.</span><span class="pln">gif</span>
  7. <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="lit">1</span><span class="pln"> linuxidc linuxidc </span><span class="lit">17627</span><span class="typ">Sep</span><span class="lit">9</span><span class="lit">11</span><span class="pun">:</span><span class="lit">46</span><span class="pln"> master</span><span class="pun">.</span><span class="pln">zip</span>
  8. <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="lit">1</span><span class="pln"> linuxidc linuxidc </span><span class="lit">5</span><span class="typ">Sep</span><span class="lit">9</span><span class="lit">11</span><span class="pun">:</span><span class="lit">48</span><span class="pln"> num</span><span class="pun">.</span><span class="pln">txt</span>
  9. <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="lit">1</span><span class="pln"> linuxidc linuxidc </span><span class="lit">0</span><span class="typ">Sep</span><span class="lit">9</span><span class="lit">11</span><span class="pun">:</span><span class="lit">46</span><span class="pln"> reggea</span><span class="pun">.</span><span class="pln">xspf</span>
  10. <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="lit">1</span><span class="pln"> linuxidc linuxidc </span><span class="lit">5</span><span class="typ">Sep</span><span class="lit">9</span><span class="lit">11</span><span class="pun">:</span><span class="lit">47</span><span class="pln"> tmp</span><span class="pun">-</span><span class="kwd">link</span>

使用 ls -l 确定块和字符文件

  1. <span class="pln">linuxidc@linuxidc </span><span class="pun">~</span><span class="str">/Linux-Tricks $ ls -l /</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda1</span>
  2. <span class="pln">brw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">----</span><span class="lit">1</span><span class="pln"> root disk </span><span class="lit">8</span><span class="pun">,</span><span class="lit">1</span><span class="typ">Sep</span><span class="lit">9</span><span class="lit">10</span><span class="pun">:</span><span class="lit">53</span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda1</span>
  3. <span class="pln">linuxidc@linuxidc </span><span class="pun">~</span><span class="str">/Linux-Tricks $ ls -l /</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">tty1</span>
  4. <span class="pln">crw</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">----</span><span class="lit">1</span><span class="pln"> root </span><span class="kwd">tty</span><span class="lit">4</span><span class="pun">,</span><span class="lit">1</span><span class="typ">Sep</span><span class="lit">9</span><span class="lit">10</span><span class="pun">:</span><span class="lit">54</span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">tty1</span>

使用 dir -l 确定一个文件的类型。

  1. <span class="pln">linuxidc@linuxidc </span><span class="pun">~/</span><span class="typ">Linux</span><span class="pun">-</span><span class="typ">Tricks</span><span class="pln"> $ </span><span class="kwd">dir</span><span class="pun">-</span><span class="pln">l</span>
  2. <span class="pln">total </span><span class="lit">6908</span>
  3. <span class="pln">drwxr</span><span class="pun">-</span><span class="pln">xr</span><span class="pun">-</span><span class="pln">x </span><span class="lit">2</span><span class="pln"> linuxidc linuxidc </span><span class="lit">4096</span><span class="typ">Sep</span><span class="lit">9</span><span class="lit">11</span><span class="pun">:</span><span class="lit">46</span><span class="pln"> BACKUP</span>
  4. <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="lit">1</span><span class="pln"> linuxidc linuxidc </span><span class="lit">1075620</span><span class="typ">Sep</span><span class="lit">9</span><span class="lit">11</span><span class="pun">:</span><span class="lit">47</span><span class="pln"> crossroads</span><span class="pun">-</span><span class="pln">stable</span><span class="pun">.</span><span class="kwd">tar</span><span class="pun">.</span><span class="pln">gz</span>
  5. <span class="pun">-</span><span class="pln">rwxr</span><span class="pun">-----</span><span class="lit">1</span><span class="pln"> linuxidc linuxidc </span><span class="lit">5916085</span><span class="typ">Sep</span><span class="lit">9</span><span class="lit">11</span><span class="pun">:</span><span class="lit">49</span><span class="pln"> EDWARD</span><span class="pun">-</span><span class="pln">MAYA</span><span class="pun">-</span><span class="lit">2011</span><span class="pun">-</span><span class="lit">2012</span><span class="pun">-</span><span class="pln">NEW</span><span class="pun">-</span><span class="pln">REMIX</span><span class="pun">.</span><span class="pln">mp3</span>
  6. <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="lit">1</span><span class="pln"> linuxidc linuxidc </span><span class="lit">42122</span><span class="typ">Sep</span><span class="lit">9</span><span class="lit">11</span><span class="pun">:</span><span class="lit">49</span><span class="typ">Linux</span><span class="pun">-</span><span class="typ">Security</span><span class="pun">-</span><span class="typ">Optimization</span><span class="pun">-</span><span class="typ">Book</span><span class="pun">.</span><span class="pln">gif</span>
  7. <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="lit">1</span><span class="pln"> linuxidc linuxidc </span><span class="lit">17627</span><span class="typ">Sep</span><span class="lit">9</span><span class="lit">11</span><span class="pun">:</span><span class="lit">46</span><span class="pln"> master</span><span class="pun">.</span><span class="pln">zip</span>
  8. <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="lit">1</span><span class="pln"> linuxidc linuxidc </span><span class="lit">5</span><span class="typ">Sep</span><span class="lit">9</span><span class="lit">11</span><span class="pun">:</span><span class="lit">48</span><span class="pln"> num</span><span class="pun">.</span><span class="pln">txt</span>
  9. <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="lit">1</span><span class="pln"> linuxidc linuxidc </span><span class="lit">0</span><span class="typ">Sep</span><span class="lit">9</span><span class="lit">11</span><span class="pun">:</span><span class="lit">46</span><span class="pln"> reggea</span><span class="pun">.</span><span class="pln">xspf</span>
  10. <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="lit">1</span><span class="pln"> linuxidc linuxidc </span><span class="lit">5</span><span class="typ">Sep</span><span class="lit">9</span><span class="lit">11</span><span class="pun">:</span><span class="lit">47</span><span class="pln"> tmp</span><span class="pun">-</span><span class="kwd">link</span>

 

3. 统计指定类型文件的数目

下面我们来看看在一个目录中用 ls,grep 和 wc 命令统计指定类型文件数目的技巧。命令之间的交互通过命名管道完成。

  • grep – 用户根据给定模式或正则表达式进行搜索的命令。
  • wc – 用于统计行、字和字符的命令。

统计普通文件的数目

在 Linux 中,普通文件用符号 - 表示。

  1. <span class="pln">linuxidc@linuxidc </span><span class="pun">~/</span><span class="typ">Linux</span><span class="pun">-</span><span class="typ">Tricks</span><span class="pln"> $ </span><span class="kwd">ls</span><span class="pun">-</span><span class="pln">l </span><span class="pun">|</span><span class="kwd">grep</span><span class="pun">^-</span><span class="pun">|</span><span class="kwd">wc</span><span class="pun">-</span><span class="pln">l</span>
  2. <span class="lit">7</span>

统计目录的数目

在 Linux 中,目录用符号 d 表示。

  1. <span class="pln">linuxidc@linuxidc </span><span class="pun">~/</span><span class="typ">Linux</span><span class="pun">-</span><span class="typ">Tricks</span><span class="pln"> $ </span><span class="kwd">ls</span><span class="pun">-</span><span class="pln">l </span><span class="pun">|</span><span class="kwd">grep</span><span class="pun">^</span><span class="pln">d </span><span class="pun">|</span><span class="kwd">wc</span><span class="pun">-</span><span class="pln">l</span>
  2. <span class="lit">1</span>

统计符号链接和硬链接的数目

在 Linux 中,符号链接和硬链接用符号 l 表示。

  1. <span class="pln">linuxidc@linuxidc </span><span class="pun">~/</span><span class="typ">Linux</span><span class="pun">-</span><span class="typ">Tricks</span><span class="pln"> $ </span><span class="kwd">ls</span><span class="pun">-</span><span class="pln">l </span><span class="pun">|</span><span class="kwd">grep</span><span class="pun">^</span><span class="pln">l </span><span class="pun">|</span><span class="kwd">wc</span><span class="pun">-</span><span class="pln">l</span>
  2. <span class="lit">0</span>

统计块文件和字符文件的数目

在 Linux 中,块和字符文件用符号 bc 表示。

  1. <span class="pln">linuxidc@linuxidc </span><span class="pun">~</span><span class="str">/Linux-Tricks $ ls -l /</span><span class="pln">dev </span><span class="pun">|</span><span class="kwd">grep</span><span class="pun">^</span><span class="pln">b </span><span class="pun">|</span><span class="kwd">wc</span><span class="pun">-</span><span class="pln">l</span>
  2. <span class="lit">37</span>
  3. <span class="pln">linuxidc@linuxidc </span><span class="pun">~</span><span class="str">/Linux-Tricks $ ls -l /</span><span class="pln">dev </span><span class="pun">|</span><span class="kwd">grep</span><span class="pun">^</span><span class="pln">c </span><span class="pun">|</span><span class="kwd">wc</span><span class="pun">-</span><span class="pln">l</span>
  4. <span class="lit">159</span>

 

4. 在 Linux 系统中查找文件

下面我们来看看在 Linux 系统中查找文件一些命令,它们包括 locate、find、whatis 和 which 命令。

用 locate 命令查找文件

在下面的输出中,我想要定位系统中的 Samba 服务器配置文件

  1. <span class="pln">linuxidc@linuxidc </span><span class="pun">~/</span><span class="typ">Linux</span><span class="pun">-</span><span class="typ">Tricks</span><span class="pln"> $ </span><span class="kwd">locate</span><span class="pln"> samba</span><span class="pun">.</span><span class="pln">conf</span>
  2. <span class="pun">/</span><span class="pln">usr</span><span class="pun">/</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="pln">samba</span><span class="pun">.</span><span class="pln">conf</span>
  3. <span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">dpkg</span><span class="pun">/</span><span class="kwd">info</span><span class="pun">/</span><span class="pln">samba</span><span class="pun">.</span><span class="pln">conffiles</span>

用 find 命令查找文件

想要学习如何在 Linux 中使用 find 命令,你���以阅读我们以下的文章,里面列出了 find 命令的 30 多个例子和使用方法。

用 whatis 命令定位命令

whatis 命令通常用于定位命令,它很特殊,因为它给出关于一个命令的信息,它还能查找配置文件和命令的帮助手册条目。

  1. <span class="pln">linuxidc@linuxidc </span><span class="pun">~/</span><span class="typ">Linux</span><span class="pun">-</span><span class="typ">Tricks</span><span class="pln"> $ </span><span class="kwd">whatis</span><span class="kwd">bash</span>
  2. <span class="kwd">bash</span><span class="pun">(</span><span class="lit">1</span><span class="pun">)</span><span class="pun">-</span><span class="pln"> GNU </span><span class="typ">Bourne</span><span class="pun">-</span><span class="typ">Again</span><span class="typ">SHell</span>
  3. <span class="pln">linuxidc@linuxidc </span><span class="pun">~/</span><span class="typ">Linux</span><span class="pun">-</span><span class="typ">Tricks</span><span class="pln"> $ </span><span class="kwd">whatis</span><span class="kwd">find</span>
  4. <span class="kwd">find</span><span class="pun">(</span><span class="lit">1</span><span class="pun">)</span><span class="pun">-</span><span class="pln"> search </span><span class="kwd">for</span><span class="pln"> files </span><span class="kwd">in</span><span class="pln"> a directory hierarchy</span>
  5. <span class="pln">linuxidc@linuxidc </span><span class="pun">~/</span><span class="typ">Linux</span><span class="pun">-</span><span class="typ">Tricks</span><span class="pln"> $ </span><span class="kwd">whatis</span><span class="kwd">ls</span>
  6. <span class="kwd">ls</span><span class="pun">(</span><span class="lit">1</span><span class="pun">)</span><span class="pun">-</span><span class="kwd">list</span><span class="pln"> directory contents</span>

用 which 命令定位命令

which 命令用于定位文件系统中的命令。

  1. <span class="pln">linuxidc@linuxidc </span><span class="pun">~/</span><span class="typ">Linux</span><span class="pun">-</span><span class="typ">Tricks</span><span class="pln"> $ which </span><span class="kwd">mkdir</span>
  2. <span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="kwd">mkdir</span>
  3. <span class="pln">linuxidc@linuxidc </span><span class="pun">~/</span><span class="typ">Linux</span><span class="pun">-</span><span class="typ">Tricks</span><span class="pln"> $ which </span><span class="kwd">bash</span>
  4. <span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="kwd">bash</span>
  5. <span class="pln">linuxidc@linuxidc </span><span class="pun">~/</span><span class="typ">Linux</span><span class="pun">-</span><span class="typ">Tricks</span><span class="pln"> $ which </span><span class="kwd">find</span>
  6. <span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="kwd">find</span>
  7. <span class="pln">linuxidc@linuxidc </span><span class="pun">~/</span><span class="typ">Linux</span><span class="pun">-</span><span class="typ">Tricks</span><span class="pln"> $ $ which </span><span class="kwd">ls</span>
  8. <span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="kwd">ls</span>

 

5.处理 Linux 系统的时间

在联网环境中,保持你 Linux 系统时间准确是一个好的习惯。Linux 系统中有很多服务要求时间正确才能在联网条件下正常工作。

让我们来看看你可以用来管理你机器时间的命令。在 Linux 中,有两种方式管理时间:系统时间和硬件时间。

系统时间由系统时钟管理,硬件时间由硬件时钟管理。

要查看你的系统时间、日期和时区,像下面这样使用 date 命令。

  1. <span class="pln">linuxidc@linuxidc </span><span class="pun">~/</span><span class="typ">Linux</span><span class="pun">-</span><span class="typ">Tricks</span><span class="pln"> $ </span><span class="kwd">date</span>
  2. <span class="typ">Wed</span><span class="typ">Sep</span><span class="lit">9</span><span class="lit">12</span><span class="pun">:</span><span class="lit">25</span><span class="pun">:</span><span class="lit">40</span><span class="pln"> IST </span><span class="lit">2015</span>

像下面这样用 date -s 或 date -set=“STRING” 设置系统时间。

  1. <span class="pln">linuxidc@linuxidc </span><span class="pun">~/</span><span class="typ">Linux</span><span class="pun">-</span><span class="typ">Tricks</span><span class="pln"> $ </span><span class="kwd">sudo</span><span class="kwd">date</span><span class="pun">-</span><span class="pln">s </span><span class="str">"12:27:00"</span>
  2. <span class="typ">Wed</span><span class="typ">Sep</span><span class="lit">9</span><span class="lit">12</span><span class="pun">:</span><span class="lit">27</span><span class="pun">:</span><span class="lit">00</span><span class="pln"> IST </span><span class="lit">2015</span>
  3. <span class="pln">linuxidc@linuxidc </span><span class="pun">~/</span><span class="typ">Linux</span><span class="pun">-</span><span class="typ">Tricks</span><span class="pln"> $ </span><span class="kwd">sudo</span><span class="kwd">date</span><span class="pun">--</span><span class="kwd">set</span><span class="pun">=</span><span class="str">"12:27:00"</span>
  4. <span class="typ">Wed</span><span class="typ">Sep</span><span class="lit">9</span><span class="lit">12</span><span class="pun">:</span><span class="lit">27</span><span class="pun">:</span><span class="lit">00</span><span class="pln"> IST </span><span class="lit">2015</span>

你也可以像下面这样设置时间和日期。

  1. <span class="pln">linuxidc@linuxidc </span><span class="pun">~/</span><span class="typ">Linux</span><span class="pun">-</span><span class="typ">Tricks</span><span class="pln"> $ </span><span class="kwd">sudo</span><span class="kwd">date</span><span class="lit">090912302015</span>
  2. <span class="typ">Wed</span><span class="typ">Sep</span><span class="lit">9</span><span class="lit">12</span><span class="pun">:</span><span class="lit">30</span><span class="pun">:</span><span class="lit">00</span><span class="pln"> IST </span><span class="lit">2015</span>

使用 cal 命令从日历中查看当前日期。

  1. <span class="pln">linuxidc@linuxidc </span><span class="pun">~/</span><span class="typ">Linux</span><span class="pun">-</span><span class="typ">Tricks</span><span class="pln"> $ cal</span>
  2. <span class="typ">September</span><span class="lit">2015</span>
  3. <span class="typ">Su</span><span class="typ">Mo</span><span class="typ">Tu</span><span class="typ">We</span><span class="typ">Th</span><span class="typ">Fr</span><span class="typ">Sa</span>
  4. <span class="lit">1</span><span class="lit">2</span><span class="lit">3</span><span class="lit">4</span><span class="lit">5</span>
  5. <span class="lit">6</span><span class="lit">7</span><span class="lit">8</span><span class="lit">9</span><span class="lit">10</span><span class="lit">11</span><span class="lit">12</span>
  6. <span class="lit">13</span><span class="lit">14</span><span class="lit">15</span><span class="lit">16</span><span class="lit">17</span><span class="lit">18</span><span class="lit">19</span>
  7. <span class="lit">20</span><span class="lit">21</span><span class="lit">22</span><span class="lit">23</span><span class="lit">24</span><span class="lit">25</span><span class="lit">26</span>
  8. <span class="lit">27</span><span class="lit">28</span><span class="lit">29</span><span class="lit">30</span>

使用 hwclock 命令查看硬件时钟时间。

  1. <span class="pln">linuxidc@linuxidc </span><span class="pun">~/</span><span class="typ">Linux</span><span class="pun">-</span><span class="typ">Tricks</span><span class="pln"> $ </span><span class="kwd">sudo</span><span class="kwd">hwclock</span>
  2. <span class="typ">Wednesday</span><span class="lit">09</span><span class="typ">September</span><span class="lit">2015</span><span class="lit">06</span><span class="pun">:</span><span class="lit">02</span><span class="pun">:</span><span class="lit">58</span><span class="pln"> PM IST </span><span class="pun">-</span><span class="lit">0.200081</span><span class="pln"> seconds</span>

要设置硬件时钟时间,像下面这样使用 hwclock –set –date=“STRING” 命令。

  1. <span class="pln">linuxidc@linuxidc </span><span class="pun">~</span><span class="str">/Linux-Tricks $ sudo hwclock --set --date="09/</span><span class="lit">09</span><span class="pun">/</span><span class="lit">2015</span><span class="lit">12</span><span class="pun">:</span><span class="lit">33</span><span class="pun">:</span><span class="lit">00</span><span class="str">"</span>
  2. <span class="str">linuxidc@linuxidc ~/Linux-Tricks $ sudo hwclock</span>
  3. <span class="str">Wednesday 09 September 2015 12:33:11 PM IST -0.891163 seconds</span>

系统时间是由硬件时钟时间在启动时设置的,系统关闭时,硬件时间被重置为系统时间。

因此你查看系统时间和硬件时间时,它们是一样的,除非你更改了系统时间。当你的 CMOS 电量不足时,硬件时间可能不正确。

你也可以像下面这样使用硬件时钟的时间设置系统时间。

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">hwclock</span><span class="pun">--</span><span class="pln">hctosys</span>

也可以像下面这样用系统时钟时间设置硬件时钟时间。

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">hwclock</span><span class="pun">--</span><span class="pln">systohc</span>

要查看你的 Linux 系统已经运行了多长时间,可以使用 uptime 命令。

  1. <span class="pln">linuxidc@linuxidc </span><span class="pun">~/</span><span class="typ">Linux</span><span class="pun">-</span><span class="typ">Tricks</span><span class="pln"> $ </span><span class="kwd">uptime</span>
  2. <span class="lit">12</span><span class="pun">:</span><span class="lit">36</span><span class="pun">:</span><span class="lit">27</span><span class="pln"> up </span><span class="lit">1</span><span class="pun">:</span><span class="lit">43</span><span class="pun">,</span><span class="lit">2</span><span class="kwd">users</span><span class="pun">,</span><span class="pln"> load average</span><span class="pun">:</span><span class="lit">1.39</span><span class="pun">,</span><span class="lit">1.34</span><span class="pun">,</span><span class="lit">1.45</span>
  3. <span class="pln">linuxidc@linuxidc </span><span class="pun">~/</span><span class="typ">Linux</span><span class="pun">-</span><span class="typ">Tricks</span><span class="pln"> $ </span><span class="kwd">uptime</span><span class="pun">-</span><span class="pln">p</span>
  4. <span class="pln">up </span><span class="lit">1</span><span class="pln"> hour</span><span class="pun">,</span><span class="lit">43</span><span class="pln"> minutes</span>
  5. <span class="pln">linuxidc@linuxidc </span><span class="pun">~/</span><span class="typ">Linux</span><span class="pun">-</span><span class="typ">Tricks</span><span class="pln"> $ </span><span class="kwd">uptime</span><span class="pun">-</span><span class="pln">s</span>
  6. <span class="lit">2015</span><span class="pun">-</span><span class="lit">09</span><span class="pun">-</span><span class="lit">09</span><span class="lit">10</span><span class="pun">:</span><span class="lit">52</span><span class="pun">:</span><span class="lit">47</span>

 

总结

对于初学者来说理解 Linux 中的文件类型是一个好的尝试,同时时间管理也非常重要,尤其是在需要可靠有效地管理服务的服务器上。希望这篇指南能对你有所帮助。如果你有任何反馈,别忘了给我们写评论。和我们保持联系。


作者:Aaron Kili 译者:ictlyh 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出

相关推荐