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

我记得我第一次使用 Linux 的时候,我还习惯于 Windows 的图形界面,我真的很讨厌 Linux 终端。那时候我觉得命令难以记忆,不能正确使用它们。随着时间推移,我意识到了 Linux 终端的优美、灵活和可用性,说实话,我没有一天不使用它。今天,我很高兴和刚开始接触 Linux 的人一起来分享一些有用的技巧和提示,希望能帮助他们更好的向 Linux 过渡,并帮助他们学到一些新的东西(希望如此)。

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

10 个 Linux 命令行技巧

这篇文章希望向你展示一些不需要很高的技术就可以像一个高手一样使用 Linux 终端的有用技巧。你只需要一个 Linux 终端和一些自由时间来体会这些命令。

 

1. 找到正确的命令

执行正确的命令对你的系统来说非常重要。然而在 Linux 中有如此多的、难以记忆的各种的命令行。那么怎样才能找到你需要的正确命令呢?答案是 apropos。你只需要运行:

  1. <span class="com">#</span><span class="pln"> apropos </span><span class="pun"><</span><span class="pln">description</span><span class="pun">></span>

其中你要用真正描述你要查找的命令的语句代替 “description”。这里有一个例子:

  1. <span class="com">#</span><span class="pln"> apropos </span><span class="str">"list directory"</span>
  2. <span class="kwd">dir</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>
  3. <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>
  4. <span class="pln">ntfsls </span><span class="pun">(</span><span class="lit">8</span><span class="pun">)</span><span class="pun">-</span><span class="kwd">list</span><span class="pln"> directory contents on an NTFS filesystem</span>
  5. <span class="pln">vdir </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>

左边你看到的是命令,右边是它们的描述。

 

2. 执行之前的命令

很多时候你需要一遍又一遍执行相同的命令。尽管你可以重复按你键盘上的向上光标键,但你也可以用 history 命令替代。这个命令会列出自从你上次启动终端以来所有输入过的命令:

  1. <span class="com">#</span><span class="pln"> history</span>
  2. <span class="lit">1</span><span class="kwd">fdisk</span><span class="pun">-</span><span class="pln">l</span>
  3. <span class="lit">2</span><span class="kwd">apt-get</span><span class="pln"> install gnome</span><span class="pun">-</span><span class="pln">paint</span>
  4. <span class="lit">3</span><span class="kwd">hostname</span><span class="pln"> tecmint</span><span class="pun">.</span><span class="pln">com</span>
  5. <span class="lit">4</span><span class="kwd">hostnamectl</span><span class="pln"> tecmint</span><span class="pun">.</span><span class="pln">com</span>
  6. <span class="lit">5</span><span class="kwd">man</span><span class="kwd">hostnamectl</span>
  7. <span class="lit">6</span><span class="kwd">hostnamectl</span><span class="pun">--</span><span class="kwd">set</span><span class="pun">-</span><span class="kwd">hostname</span><span class="pln"> tecmint</span><span class="pun">.</span><span class="pln">com</span>
  8. <span class="lit">7</span><span class="kwd">hostnamectl</span><span class="pun">-</span><span class="kwd">set</span><span class="pun">-</span><span class="kwd">hostname</span><span class="pln"> tecmint</span><span class="pun">.</span><span class="pln">com</span>
  9. <span class="lit">8</span><span class="kwd">hostnamectl</span><span class="kwd">set</span><span class="pun">-</span><span class="kwd">hostname</span><span class="pln"> tecmint</span><span class="pun">.</span><span class="pln">com</span>
  10. <span class="lit">9</span><span class="kwd">mount</span><span class="pun">-</span><span class="pln">t </span><span class="str">"ntfs"</span><span class="pun">-</span><span class="pln">o</span>
  11. <span class="lit">10</span><span class="kwd">fdisk</span><span class="pun">-</span><span class="pln">l</span>
  12. <span class="lit">11</span><span class="kwd">mount</span><span class="pun">-</span><span class="pln">t ntfs</span><span class="pun">-</span><span class="lit">3g</span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda5 </span><span class="pun">/</span><span class="pln">mnt</span>
  13. <span class="lit">12</span><span class="kwd">mount</span><span class="pun">-</span><span class="pln">t rw ntfs</span><span class="pun">-</span><span class="lit">3g</span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda5 </span><span class="pun">/</span><span class="pln">mnt</span>
  14. <span class="lit">13</span><span class="kwd">mount</span><span class="pun">-</span><span class="pln">t </span><span class="pun">-</span><span class="pln">rw ntfs</span><span class="pun">-</span><span class="lit">3g</span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda5 </span><span class="pun">/</span><span class="pln">mnt</span>
  15. <span class="lit">14</span><span class="kwd">mount</span><span class="pun">-</span><span class="pln">t ntfs</span><span class="pun">-</span><span class="lit">3g</span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda5 </span><span class="pun">/</span><span class="pln">mnt</span>
  16. <span class="lit">15</span><span class="kwd">mount</span><span class="kwd">man</span>
  17. <span class="lit">16</span><span class="kwd">man</span><span class="kwd">mount</span>
  18. <span class="lit">17</span><span class="kwd">mount</span><span class="pun">-</span><span class="pln">t </span><span class="pun">-</span><span class="pln">o ntfs</span><span class="pun">-</span><span class="lit">3g</span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda5 </span><span class="pun">/</span><span class="pln">mnt</span>
  19. <span class="lit">18</span><span class="kwd">mount</span><span class="pun">-</span><span class="pln">o ntfs</span><span class="pun">-</span><span class="lit">3g</span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda5 </span><span class="pun">/</span><span class="pln">mnt</span>
  20. <span class="lit">19</span><span class="kwd">mount</span><span class="pun">-</span><span class="pln">ro ntfs</span><span class="pun">-</span><span class="lit">3g</span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda5 </span><span class="pun">/</span><span class="pln">mnt</span>
  21. <span class="lit">20</span><span class="kwd">cd</span><span class="pun">/</span><span class="pln">mnt</span>
  22. <span class="pun">...</span>

正如你上面看到的,你会得到一个你运行过的命令的列表。每一行中有一个数字表示你在第几行输入了命令。你可以通过以下方法重新调用该命令:

  1. <span class="pun">!#</span>

其中要用命令的实际编号代替 #。为了更好的理解,请看下面的例子:

  1. <span class="pun">!</span><span class="lit">501</span>

等价于:

  1. <span class="com">#</span><span class="pln"> history</span>

 

3. 使用 midnight 命令

如果你不习惯使用类似 cd、cp、mv、rm 等命令,你可以使用 midnight 命令。它是一个简单的可视化 shell,你可以在上面使用鼠标:

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

Midnight 命令

借助 F1 到 F12 键,你可以轻易地执行不同任务。只需要在底部选择对应的命令。要选择文件或者目录,按下 “Insert” 键。

简而言之 midnight 就是所谓的 “mc”。要安装 mc,只需要运行:

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">apt-get</span><span class="pln"> install mc </span><span class="pun">[</span><span class="typ">On</span><span class="typ">Debian</span><span class="pln"> based systems</span><span class="pun">]</span>

  1. <span class="com">#</span><span class="kwd">yum</span><span class="pln"> install mc </span><span class="pun">[</span><span class="typ">On</span><span class="typ">Fedora</span><span class="pln"> based systems</span><span class="pun">]</span>

下面是一个使用 midnight 命令器的简单例子。通过输入以下命令打开 mc:

  1. <span class="com">#</span><span class="pln"> mc</span>

现在使用 TAB 键选择不同的窗口 - 左和右。我有一个想要移动到 “Software” 目录的 LibreOffice 文件:

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

Midnight 命令移动文件

按 F6 按钮移动文件到新的目录。MC 会请求你确认:

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

移动文件到��目录

确认了之后,文件就会被移动到新的目标目录。

 

4. 在指定时间关闭计算机

有时候你需要在下班几个小时后再关闭计算机。你可以通过使用下面的命令在指定时间关闭你的计算机:

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">shutdown</span><span class="lit">21</span><span class="pun">:</span><span class="lit">00</span>

这会告诉你在你指定的时间关闭计算机。你也可以告诉系统在指定分钟后关闭:

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">shutdown</span><span class="pun">+</span><span class="lit">15</span>

这表示计算机会在 15 分钟后关闭。

 

5. 显示已知用户的信息

你可以使用一个简单的命令列出你 Linux 系统的用户以及一些关于它们的基本信息。

  1. <span class="com">#</span><span class="pln"> lslogins</span>

这会输出下面的结果:

  1. <span class="pln">UID USER PWD</span><span class="pun">-</span><span class="pln">LOCK PWD</span><span class="pun">-</span><span class="pln">DENY LAST</span><span class="pun">-</span><span class="pln">LOGIN GECOS</span>
  2. <span class="lit">0</span><span class="pln"> root </span><span class="lit">0</span><span class="lit">0</span><span class="typ">Apr29</span><span class="pun">/</span><span class="lit">11</span><span class="pun">:</span><span class="lit">35</span><span class="pln"> root</span>
  3. <span class="lit">1</span><span class="pln"> bin </span><span class="lit">0</span><span class="lit">1</span><span class="pln"> bin</span>
  4. <span class="lit">2</span><span class="pln"> daemon </span><span class="lit">0</span><span class="lit">1</span><span class="pln"> daemon</span>
  5. <span class="lit">3</span><span class="pln"> adm </span><span class="lit">0</span><span class="lit">1</span><span class="pln"> adm</span>
  6. <span class="lit">4</span><span class="kwd">lp</span><span class="lit">0</span><span class="lit">1</span><span class="kwd">lp</span>
  7. <span class="lit">5</span><span class="kwd">sync</span><span class="lit">0</span><span class="lit">1</span><span class="kwd">sync</span>
  8. <span class="lit">6</span><span class="kwd">shutdown</span><span class="lit">0</span><span class="lit">1</span><span class="typ">Jul19</span><span class="pun">/</span><span class="lit">10</span><span class="pun">:</span><span class="lit">04</span><span class="kwd">shutdown</span>
  9. <span class="lit">7</span><span class="kwd">halt</span><span class="lit">0</span><span class="lit">1</span><span class="kwd">halt</span>
  10. <span class="lit">8</span><span class="pln"> mail </span><span class="lit">0</span><span class="lit">1</span><span class="pln"> mail</span>
  11. <span class="lit">10</span><span class="pln"> uucp </span><span class="lit">0</span><span class="lit">1</span><span class="pln"> uucp</span>
  12. <span class="lit">11</span><span class="kwd">operator</span><span class="lit">0</span><span class="lit">1</span><span class="kwd">operator</span>
  13. <span class="lit">12</span><span class="pln"> games </span><span class="lit">0</span><span class="lit">1</span><span class="pln"> games</span>
  14. <span class="lit">13</span><span class="pln"> gopher </span><span class="lit">0</span><span class="lit">1</span><span class="pln"> gopher</span>
  15. <span class="lit">14</span><span class="pln"> ftp </span><span class="lit">0</span><span class="lit">1</span><span class="pln"> FTP </span><span class="typ">User</span>
  16. <span class="lit">23</span><span class="pln"> squid </span><span class="lit">0</span><span class="lit">1</span>
  17. <span class="lit">25</span><span class="pln"> named </span><span class="lit">0</span><span class="lit">1</span><span class="typ">Named</span>
  18. <span class="lit">27</span><span class="pln"> mysql </span><span class="lit">0</span><span class="lit">1</span><span class="typ">MySQL</span><span class="typ">Server</span>
  19. <span class="lit">47</span><span class="pln"> mailnull </span><span class="lit">0</span><span class="lit">1</span>
  20. <span class="lit">48</span><span class="pln"> apache </span><span class="lit">0</span><span class="lit">1</span><span class="typ">Apache</span>
  21. <span class="pun">...</span>

 

6. 查找文件

查找文件有时候并不像你想象的那么简单。一个搜索文件的好例子是:

  1. <span class="com">#</span><span class="kwd">find</span><span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="pln">user </span><span class="pun">-</span><span class="pln">type f</span>

这个命令会搜索 /home/user 目录下的所有文件。find 命令真的很强大,你可以传递更多选项给它使得你的搜索更加详细。如果你想搜索超过特定大小的文件,可以使用:

  1. <span class="com">#</span><span class="kwd">find</span><span class="pun">.</span><span class="pun">-</span><span class="pln">type f </span><span class="pun">-</span><span class="kwd">size</span><span class="lit">10M</span>

上面的命令会搜索当前目录中所有大于 10M 的文件。确保不要在你 Linux 系统的根目录运行该命令,因为这可能导致你的机器 I/O 瓶颈。

我最经常和 find 命令一起使用的选项之一是 “exec”,这允许你对 find 命令的结果运行一些操作。

例如,假如我们想查找一个目录中的所有文件并更改权限。可以通过以下简单命令完成:

  1. <span class="com">#</span><span class="kwd">find</span><span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="pln">user</span><span class="pun">/</span><span class="pln">files</span><span class="pun">/</span><span class="pun">-</span><span class="pln">type f </span><span class="pun">-</span><span class="kwd">exec</span><span class="kwd">chmod</span><span class="lit">644</span><span class="pun">{}</span><span class="pln"> \;</span>

上面的命令会递归搜索指定目录内的所有文件,并对找到的文件执行 chmod 命令。推荐你阅读

我肯定你会发现这个命令更多的使用方法。

 

7. 用一个命令创建目录树

你很可能知道可以使用 mkdir 命令创建新的目录。因此如果你想创建一个新的目录,你可能会运行:

  1. <span class="com">#</span><span class="kwd">mkdir</span><span class="pln"> new_folder</span>

但如果你想在该目录下创建 5 个子目录呢?运行 5 次 mkdir 命令并非是一个好的选择。相反你可以类似下面这样使用 -p 选项:

  1. <span class="com">#</span><span class="kwd">mkdir</span><span class="pun">-</span><span class="pln">p new_folder</span><span class="pun">/{</span><span class="pln">folder_1</span><span class="pun">,</span><span class="pln">folder_2</span><span class="pun">,</span><span class="pln">folder_3</span><span class="pun">,</span><span class="pln">folder_4</span><span class="pun">,</span><span class="pln">folder_5</span><span class="pun">}</span>

最后你会在 new_folder 中有 5 个目录:

  1. <span class="com">#</span><span class="kwd">ls</span><span class="pln"> new_folder</span><span class="pun">/</span>
  2. <span class="pln">folder_1 folder_2 folder_3 folder_4 folder_5</span>

 

8. 复制文件到多个目录

通常使用 cp 命令进行文件复制。复制文件通常看起来类似:

  1. <span class="com">#</span><span class="kwd">cp</span><span class="pun">/</span><span class="pln">path</span><span class="pun">-</span><span class="pln">to</span><span class="pun">-</span><span class="kwd">file</span><span class="pun">/</span><span class="pln">my_file</span><span class="pun">.</span><span class="pln">txt </span><span class="pun">/</span><span class="pln">path</span><span class="pun">-</span><span class="pln">to</span><span class="pun">-</span><span class="kwd">new</span><span class="pun">-</span><span class="pln">directory</span><span class="pun">/</span>

现在假设你需要复制该文件到多个目录:

  1. <span class="com">#</span><span class="kwd">cp</span><span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="pln">user</span><span class="pun">/</span><span class="pln">my_file</span><span class="pun">.</span><span class="pln">txt </span><span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="pln">user</span><span class="pun">/</span><span class="lit">1</span>
  2. <span class="com">#</span><span class="kwd">cp</span><span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="pln">user</span><span class="pun">/</span><span class="pln">my_file</span><span class="pun">.</span><span class="pln">txt </span><span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="pln">user</span><span class="pun">/</span><span class="lit">2</span>
  3. <span class="com">#</span><span class="kwd">cp</span><span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="pln">user</span><span class="pun">/</span><span class="pln">my_file</span><span class="pun">.</span><span class="pln">txt </span><span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="pln">user</span><span class="pun">/</span><span class="lit">3</span>

这有点荒唐。相反,你可以用简单的一行命令解决问题:

  1. <span class="com">#</span><span class="kwd">echo</span><span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="pln">user</span><span class="pun">/</span><span class="lit">1</span><span class="str">/ /</span><span class="pln">home</span><span class="pun">/</span><span class="pln">user</span><span class="pun">/</span><span class="lit">2</span><span class="str">/ /</span><span class="pln">home</span><span class="pun">/</span><span class="pln">user</span><span class="pun">/</span><span class="lit">3</span><span class="pun">/</span><span class="pun">|</span><span class="kwd">xargs</span><span class="pun">-</span><span class="pln">n </span><span class="lit">1</span><span class="kwd">cp</span><span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="pln">user</span><span class="pun">/</span><span class="pln">my_file</span><span class="pun">.</span><span class="pln">txt</span>

 

9. 删除大文件

有时候文件可能会变得很大。我看过由于缺乏管理技能一个日志文件就超过 250G 的例子。用 rm 命令可能不足以删除该文件,因为有大量的数据需要移除。应该避免这个很“笨重”的操作。相反,你可以使用一个简单的方法解决这个问题:

  1. <span class="com">#</span><span class="pun">></span><span class="str">/path-to-file/</span><span class="pln">huge_file</span><span class="pun">.</span><span class="pln">log</span>

当然你需要根据你实际情况替换路径和文件名。上面的命令写一个空输出到该文件。用更简单的话说它会清空文件而不会导致你的系统产生大的 I/O 消耗。

 

10. 在多个 Linux 服务器上运行相同命令

最近我们的一个读者在 LinuxSay 论坛提问说如何通过 ssh 在多个 Linux 服务器上执行一个命令。他机器的 IP 地址是:

  1. <span class="lit">10.0</span><span class="pun">.</span><span class="lit">0.1</span>
  2. <span class="lit">10.0</span><span class="pun">.</span><span class="lit">0.2</span>
  3. <span class="lit">10.0</span><span class="pun">.</span><span class="lit">0.3</span>
  4. <span class="lit">10.0</span><span class="pun">.</span><span class="lit">0.4</span>
  5. <span class="lit">10.0</span><span class="pun">.</span><span class="lit">0.5</span>

这里有一个简单的解决方法。将服务器的 IP 地址写到文件 list.txt 中,像上面那样一行一个。然后运行:

  1. <span class="com">#</span><span class="kwd">for</span><span class="kwd">in</span><span class="pln"> $i</span><span class="pun">(</span><span class="kwd">cat</span><span class="kwd">list</span><span class="pun">.</span><span class="pln">txt</span><span class="pun">);</span><span class="kwd">do</span><span class="kwd">ssh</span><span class="pln"> user@$i </span><span class="str">'bash command'</span><span class="pun">;</span><span class="kwd">done</span>

上面的命令中你需要用实际登录的用户替换 “user”,用你希望执行的实际命令替换 “bash command”。这个方法非常适用于通过使用 SSH 密钥进行无密码验证,因为这样你不需要每次都为用户输入密码。

注意取决于你 Linux 系统的设置,你可能还需要传递一些额外的参数给 SSH 命令。

 

总结

上面的例子都很简单,我希望它们能帮助你发现 Linux 的优美之处,你如何能简单实现在其它操作系统上需要更多时间的不同操作。


via: http://www.tecmint.com/10-useful-linux-command-line-tricks-for-newbies/

作者:Marin Todorov 译者:ictlyh 校对:wxy

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

相关推荐