跳至内容

实验室 3: 常用系统工具

目标

完成本实验室后,您将能够

  • 使用大多数 Linux 系统上常见的系统工具

完成本实验室的预计时间:70 分钟

Linux 系统上的常用系统工具

本实验室的练习涵盖了用户和管理员都需要熟悉的一些基本系统工具的使用。大多数命令用于导航和操作文件系统。文件系统由文件和目录组成。

这些练习将涵盖以下工具的使用:–pwdcdlsrmmvftpcptouchmkdirfilecatfindlocate

练习 1

cd

cd 命令代表“更改目录”。您将从这些实验室开始,切换到文件系统上的其他目录。

使用 cd

  1. 以 root 用户身份登录到计算机

  2. 从当前目录更改到 /etc 目录。

    [root@localhost root]# cd /etc
    
  3. 注意,您的提示符已从“[root@localhost root]#”更改为:“[root@localhost etc]#”。

  4. 更改到 /usr/local/ 目录

    [root@localhost etc]# cd /usr/local
    
    [root@localhost local]#
    

    问题

    您的提示符发生了什么变化?

  5. 更改回 root 的主目录

    [root@localhost local]# cd /root
    
  6. 再次更改到 /usr/local/ 目录。输入

    [root@localhost root]# cd /usr/local
    
  7. 要更改到 local 目录的父目录,请输入“cd ..”。

    [root@localhost local]# cd ..
    

    问题

    /usr/local/ 目录的父目录是什么?

  8. 要快速更改回 root 的主目录,请输入“cd”,不带任何参数。

    [root@localhost usr]# cd
    
    [root@localhost root]#
    

练习 2

pwd

pwd 命令代表“当前工作目录”。它显示您在文件系统中的位置。

使用 pwd

  1. 要找出您的当前工作目录,请输入

    [root@localhost root]# pwd
    /root
    
  2. 使用“cd”命令将您的目录更改到 /usr/local/ 目录

    [root@localhost root]# cd /usr/local
    
  3. 使用 pwd 找出您的当前工作目录

    [root@localhost local]# pwd
    /usr/local
    
  4. 返回到 root 的主目录

    [root@localhost root]#  cd
    

练习 3

mkdir

mkdir 命令用于创建目录。您将创建两个名为“folder1”和“folder2”的目录。

使用 mkdir

  1. 类型

    [root@localhost root]# mkdir folder1
    
  2. 创建一个名为 folder2 的第二个目录。

    [root@localhost root]# mkdir   folder2
    
  3. 现在将您的工作目录更改为上面创建的“folder1”目录。

    [root@localhost root]# cd folder1
    
  4. 显示您当前的工作目录。

    [root@localhost folder1]# pwd
    /root/folder1
    

    问题

    不离开当前目录,更改到“folder2”目录。执行此操作的命令是什么?

  5. 返回到根目录的主目录。

练习 4

touch

touch 命令可用于创建普通文件。您将在上面创建的文件夹中创建“file11、file12、file21 和 file22”。

要使用 touch

  1. 将目录更改为,即 cd 到 folder1 并创建“file11”:

    [root@localhost folder1]# touch file11
    
  2. 仍在 folder1 中时,创建“file12”:

    [root@localhost folder1]# touch file12
    
  3. 现在返回到根目录的主目录。

  4. cd 到 folder2 并创建“file21”和“file22”。

    [root@localhost folder2]# Touch file21 file22
    
  5. 返回到根目录的主目录。

练习 5

ls

ls 命令代表列表。它列出目录的内容。

要使用 ls

  1. 在根目录的主目录中键入 ls

    [root@localhost root]# ls
    

    问题

    列出目录的内容。

  2. 更改到“folder1”目录。

  3. 列出“folder1”目录的内容。键入 ls

    [root@localhost folder1]# ls
    file11  file12
    
  4. 更改到“folder2”目录并在此处列出其内容。

  5. 更改回您的主目录并列出“所有”隐藏文件和文件夹。

    [root@localhost folder2]# cd
    
    [root@localhost root]# ls   –a
    ..  .bash_history  .bash_logout  .bash_profile  .bashrc  folder1  folder2  .gtkrc  .kde   screenrc
    
  6. 要获取主目录中所有文件和文件夹的长列表或详细列表,请键入

    [root@localhost root]# ls –al
    total 44
    
    drwx------    5 root    root        4096 May  8 10:15 .
    
    drwxr-xr-x    8 root     root         4096 May  8 09:44 ..
    
    -rw-------    1 root    root          43 May  8 09:48 .bash_history
    
    -rw-r--r--    1 root    root          24 May  8 09:44 .bash_logout
    
    -rw-r--r--    1 root    root         191 May  8 09:44 .bash_profile
    
    -rw-r--r--    1 root    root         124 May  8 09:44 .bashrc
    
    drwxrwxr-x    2 root    root        4096 May  8 10:17 folder1
    
    drwxrwxr-x    2 root    root        4096 May  8 10:18 folder2
    
    ………………………..
    

练习 6

mv

mv 命令代表移动。它重命名文件或目录。它还可以移动文件。

要使用 mv

  1. 将目录更改到“folder1”目录并列出其内容。

    [root@localhost root]# cd   folder1
    [root@localhost folder1] ls
    
    file11  file12
    
  2. 您将分别将“folder1”目录中的 file11 和 file12 重命名为 temp_file11 和 temp_file12。

    [root@localhost folder1]# mv file11 temp_file11
    
  3. 再次列出 folder1 的内容。

    [root@localhost folder1]# ls
    

    问题

    记下内容。

  4. 将 file12 重命名为 temp_file12。

    [root@localhost folder1]# mv file12 temp_file12
    
  5. 不更改目录,分别将“folder2”中的 file21 和 file22 重命名为 temp_file21 和 temp_file22。

    [root@localhost folder1]# mv   /root/folder2/file21     /root/folder2/temp_file21
    
    [root@localhost folder1]# mv   /root/folder2/file22    /root/folder2/temp_file22
    
  6. 不更改当前目录,列出 folder2 的内容。

    问题

    执行此操作的命令是什么?另外,列出命令的输出?

练习 7

cp

cp 命令代表复制。它复制文件或目录。

  1. 将您的目录更改为“folder2”目录。

  2. 将“folder2”(temp_file21 和 temp_file22)的内容复制到“folder1”:

    [root@localhost folder2]# cp  temp_file21  temp_file22    ../folder1
    
  3. 列出 folder1 的内容。

    [root@localhost folder2]# ls  ../folder1
    temp_file11  temp_file12  temp_file21  temp_file22
    
  4. 列出 folder2 的内容。请注意,temp_file21 和 temp_file22 的原始副本保留在 folder2 中。

    [root@localhost folder2]# ls
    temp_file21  temp_file22
    

练习 8

file

file 实用程序用于确定文件或目录类型。

要使用 file

  1. 更改回您的主目录。

  2. 要查看“folder1”是文件还是目录;键入

    [root@localhost root]# file    folder1
    folder1: directory
    
  3. 更改到 folder1 目录。

  4. 使用 file 实用程序确定 temp_file11 的文件类型。

    [root@localhost folder1]# file     temp_file11
    temp_file11: empty
    
  5. 使用 file 实用程序查找 folder1 目录中所有文件的类型。列出此处。

  6. 将目录更改到 /etc 目录。

    [root@localhost folder1]# cd /etc
    
  7. 使用 file 实用程序查找“passwd”文件的类型。

    [root@localhost etc]# file passwd
    

    问题

    它是什么类型的文件?

练习 9

cat

cat 命令是连接的简写,表示它将文件串在一起。cat 命令还将在屏幕上显示整个文件的内容。您将 cat 与重定向符号“>”一起使用来创建文件。

要使用 cat 创建文件

  1. 将目录更改到 /root/folder1 目录。

  2. 创建一个名为“first.txt”的新文本文件。

    [root@localhost folder1]# cat > first.txt
    
  3. 在空白提示符下键入以下短语并按 Enter

    This is a line from first.txt !!
    
  4. 同时按 Ctrl+C

  5. 键入“cat first.txt”以读取您刚键入的文本。

    [root@localhost folder1]#  cat    first.txt
    This is a line from first.txt !!
    
  6. 使用 cat 创建另一个名为“second.txt”的文件。在文件中键入以下文本 -“This is a line from second.txt !!”。

    问题

    执行此操作的命令是什么?

要使用 cat 将文件连接在一起

  1. 您将连接“first.txt”和“second.txt”文件。键入

    [root@localhost folder1]#  cat     first.txt    second.txt
    

    问题

    您的输出是什么?

练习 10

ftp

ftp 是一个客户端程序,用于通过文件传输协议使用和连接到 FTP 服务。该程序允许用户将文件传输到远程网络站点或从远程网络站点传输文件。它是一个您可能需要经常使用的实用程序。

在本练习中,您将学习如何匿名登录到 FTP 服务器,并使用 ftp 客户端程序从服务器下载文件。

注意

您需要完成先前实验室中的练习才能在需要可用的 FTP 服务器在某个可访问位置运行的此特定练习中继续学习。

要使用 ftp

  1. 以 root 用户身份登录到您的机器。

  2. 将目录更改为“/usr/local/src/”目录。

  3. 在“/usr/local/src/”目录下创建一个名为“downloads”的新目录。

    问题

    执行此操作的命令是什么?

  4. 将目录更改为新创建的“downloads”目录。

    [root@localhost src]# cd  downloads
    
  5. 键入“ftp”以启动您的 ftp 客户端。

    [root@localhost downloads]#  ftp
    ftp>
    
  6. 要连接到 FTP 服务器,请键入

    ftp> open  < server-address>         (Obtain the <server-address> from your instructor)
    ………  
    
    220 localhost.localdomain FTP server (Version wu-2.6.2-5) ready.
    ………..
    
  7. 以匿名用户身份登录。在提示符下键入“anonymous”。

    Name (10.4.51.29:root):  anonymous
    
  8. 在密码提示符下键入任何电子邮件地址并按回车键。

    Password:         ***************
    
    230 Guest login ok, access restrictions apply.
    
    Remote system type is UNIX.
    
    Using binary mode to transfer files.
    
    ftp>
    
  9. 更改为二进制模式。键入

    ftp> binary
    

    问题

    二进制命令的输出是什么?“二进制模式”是什么?

  10. 列出 ftp 服务器上的当前目录。在 ftp 提示符 (ftp>) 下键入“ls”。

    ftp>  ls  
    227 Entering Passive Mode (10,0,4,5,16,103).
    125 Data connection already open; Transfer starting.
    11-23-43  10:23PM       <DIR>          images
    11-02-43  02:20PM       <DIR>          pub
    226 Transfer complete.
    
  11. 将目录更改为“pub”目录。键入

    ftp> cd  pub
    
  12. 使用“ls”命令列出“pub”目录中的文件和目录。

    问题

    现在那里有多少个文件和目录?

  13. 将名为“hello-2.1.1.tar.gz”的文件下载到您的本地目录。在提示符下键入“yes”。

    ftp>  mget     hello-2.1.1.tar.gz
    mget hello-2.1.1.tar.gz?    yes
    
    227 Entering Passive Mode (10,0,4,5,16,252).
    
    125 Data connection already open; Transfer starting.
    
    226 Transfer complete.
    
    389363 bytes received in 0.0745 secs (5.1e+03 Kbytes/sec)
    
  14. 注销 FTP 服务器并退出 ftp 客户端。键入

    ftp> bye
    
  15. 您将被送回本地 shell。

  16. 确保您仍位于本地机器上的本地机器的“downloads”目录中。

    问题

    列出 downloads 文件夹中的文件。

练习 11

使用重定向

您在 Linux 中使用的大多数实用程序和命令将其输出发送到屏幕。屏幕称为标准输出 (stdout)。重定向允许您将输出发送到其他地方 - 也许是文件。

在 Linux 系统上启动的每个程序都有三个打开的文件描述符:stdin (0)、stdout (1) 和 stderr (2)。您可以重定向或将它们管道到“管道”。重定向符号是“>、<”。

要使用重定向

  1. 确保您仍位于 folder1 目录中。

  2. 您将使用输出重定向将 ls(列表)命令的输出重定向到一个名为 myredirects 的文本文件。

    [root@localhost folder1]# ls  > myredirects
    
  3. 检查 folder1 目录中创建的新文件(myredirects)的内容。

    [root@localhost folder1] # cat     myredirects
    temp_file11  temp_file12  temp_file21  temp_file22 myredirects
    
  4. 现在您将将 file 命令的输出重定向到同一个文件。您想查找 folder1 目录中 temp_file11 的文件类型,并将输出发送到您的 myredirects 文件。

    [root@localhost folder1]#  file    temp_file11   >   myredirects
    
  5. 检查 myredirects 文件的内容。

    问题

    它已经改变了。发生了什么事?

  6. 如果您想阻止上面发生的事情,您将使用双重定向符号“>>”。这将追加(添加)新输出到文件,而不是替换它。试试看

    [root@localhost folder1]#  ls  >>  myredirects
    
  7. 现在使用 cat 再次检查文件 myredirects 的内容。

    问题

    在此处记下其内容。

使用重定向来抑制命令的输出

您将经常在 Linux 中使用此处介绍的概念,因此请特别注意它。这可能有点棘手。

有时您不希望用户看到命令的输出 - 也许是错误消息。这通常是因为奇怪的错误消息往往会吓唬普通用户。在本练习中,您将将命令的输出发送到空设备(/dev/null/)。空设备就像一个“垃圾桶”。您放入其中的任何内容都会永远消失。您还可以将常规命令输出发送(或重定向)到空设备“空设备”。

使用以下指南

|Redirector|<p></p><p>Function</p>|
| :- | :- |
|> file|Direct standard output to file|
|< file|Take standard input from file|
|Cmd1 | cmd2|Pipe; take standard out of cmd1 as standard input to cmd2|
|n> file|Direct file descriptor n to file|
|N< file|Set file as file descriptor n|
|>&n|Duplicate standard output to file descriptor n|
|<&n|Duplicate standard input from file descriptor n|
|&>file|Direct standard output and standard error to file|
  1. 确保您仍位于 folder1 目录中。对 temp_file11 使用 ls 命令的长列表选项。

    [root@localhost folder1]#  ls   –l   temp_file11
    -rw-r--r--    1 root     root            0 Jul 26 18:26 temp_file11
    
  2. 您将将相同命令(ls –l temp_file11)的输出重定向到空设备。

    [root@localhost folder1]#  ls   –l temp_file11  > /dev/null
    

    您不应该有任何输出。

  3. 现在,如果您不小心拼错了要查看其信息的的文件名称;您将得到

    [root@localhost folder1]# ls –l te_file1
    ls: te_file1: No such file or directory
    

    以上是 ls 命令被编程为提供这种类型的错误的结果。

  4. 运行与上面相同的命令,但使用文件名的错误拼写,并将其重定向到 /dev/null

    [root@localhost folder1]# ls   -l   te_file1  >  /dev/null
    
    ls: te_file1: No such file or directory
    

    问题

    这里发生了什么?为什么输出仍然显示在屏幕上(stdout)?

  5. 由于各种原因,您可能希望抑制错误消息,例如上面的错误消息。为此,请键入

    [root@localhost folder1]# ls –l te_file1 > /dev/null 2>&1
    

    您将不会得到任何输出。

    这次,标准输出和标准错误都被抑制。

    重定向的顺序很重要!

    重定向从命令行上的从左到右读取。

    重定向符号的左侧 - “>”:将标准输出(stdout)发送到 /dev/null。然后,最右侧的部分 - “2>&1”:将标准错误 (2) 复制到标准输出 (1)。

    因此,上面的命令可以理解为:将 stdout(1) 重定向到“/dev/null”,然后将 stderr (2) 复制到 stdout。

  6. 为了进一步证明重定向顺序的重要性;尝试

    [root@localhost folder1]# ls   –l    tem_file  2>&1   > order.txt
    

    使用 cat 命令检查“order.txt”文件的内容。

    最左侧的部分 - “2>&1” 将将标准错误复制到标准输出。然后,最右侧的部分 - “> order.txt” 将 stdout 重定向到文件 order.txt。

  7. 尝试上述步骤的这种变体

    [root@localhost folder1]# ls  –l   hgh_ghz  2>  order2.txt   > order2.txt
    

    问题

    检查“order2.txt”文件并解释发生了什么事?

  8. 要将标准输出和标准错误发送到单独的文件;键入

    [root@localhost folder1]# ls  –l  tep_f   > standard_out  2> standard_err
    

    问题

    创建了两个新文件。文件名称是什么?它们的内容是什么?

  9. 您可以类似地使用以下命令将 stdout 和 stderr 都重定向到同一个文件

    [root@localhost folder1]# ls  –l   te_fil   &>   standard_both
    

练习 12

rm

rm 命令用于删除文件或目录。您将使用 rm 删除您在前面练习中创建的一些文件。

要使用 rm

  1. 仍在“folder1”目录中时,删除 standard_err 文件。在确认提示符下键入“y”。

    [root@localhost folder1]# rm   standard_err
    rm: remove `standard_err'? y
    
  2. 删除“standard_out”文件。要防止在删除文件之前提示确认,请在 rm 命令中使用“–f”选项。

    [root@localhost folder1]# rm   -f   standard_out
    
  3. 更改回您的主目录 (/root) 并删除“folder2”目录。要使用 rm 删除文件夹,您需要使用“–r”开关。

    [root@localhost root]# rm  -r   folder2
    
    rm: descend into directory `folder2'? y
    
    rm: remove `folder2/temp_file21'? y
    
    rm: remove `folder2/temp_file22'? y
    
    rm: remove directory `folder2'? y
    

    问题

    您再次被提示确认要删除目录中的每个文件以及目录本身。您将使用哪个选项与 rm –r 命令来防止这种情况?

练习 13

学习 vi

vi 是一个文本编辑器。它可以用来编辑各种纯文本。它对于编辑程序特别有用。

vi 是一个庞大而强大的工具,它几乎可以做任何事情 - 包括为您制作咖啡或可可!

本练习不会尝试教会您 vi,而是会向您推荐一个更适合熟悉 vi 的工具。

请花些时间学习在线 vi(更准确地说是 vim)教程。只需按照说明进行操作即可。

学习 vi

  1. 登录系统后,输入

    [root@localhost root]# vimtutor

练习 14

搜索文件:(findlocate

本练习将讨论两个最常用的实用程序,用于在文件系统上搜索文件和目录。它们是 find 命令和 locate 命令。

find

find 实用程序已经存在很长时间了。它递归地扫描目录以查找与给定条件匹配的文件。

find 的一般语法是

find   [path]    [options]   [criterion]    [action]

如果您未指定任何目录或路径,find 将搜索当前目录。如果您未指定条件,则等效于“true”,因此将找到所有文件。find 实用程序具有许多选项,可以执行几乎任何类型的文件搜索。下面列出了一些选项、条件和操作。

OPTIONS:

-xdev: do not search on directories located on other filesystems;

-mindepth <n> descend at least <n> levels below the specified directory before

searching for files;

-maxdepth <n>: search for files located at most n levels below the specified directory;

-follow: follow symbolic links if they link to directories.

-daystart: when using tests related to time (see below), take the beginning of current day as a timestamp instead of the default (24 hours before current time).
CRITERION

-type <type>: search for a given type of file; <type> can be one of: f (regular file), d (directory),

l (symbolic link), s (socket), b (block mode file), c (character mode file) or

p (named pipe);

-name <pattern>: find files whose names match the given <pattern>;

-iname <pattern>: like -name, but ignore case;

-atime <n>, -amin <n>:find files which have last been accessed <n> days ago (-atime) or <n> minutes ago (-amin). You can also specify +<n> or -<n>, in which case the search will be done for files accessed respectively at most or at least <n> days/minutes ago;

-anewer <file>: find files which have been accessed more recently than file <file>;

-ctime <n>, -cmin <n>, -cnewer <file>: same as for -atime, -amin and -anewer, but applies to the last time when the contents of the file have been modified;

-regex <pattern>: same as -name, but pattern is treated as a regular expression;

-iregex <pattern>: same as -regex, but ignore case.
ACTION:

-print: just prints the name of each file on standard output. This is the default action;

-ls: prints on the standard output the equivalent of ls -ilds for each file found;

-exec <command>: execute command <command> on each file found. The command line <command> must end with a ;, which you must escape so that the shell does not interpret it; the file position is marked with {}.

-ok <command>: same as -exec but asks confirmation for each command.

使用 find

  1. 确保您位于主目录中。

  2. 您将使用 find 来显示当前目录(pwd)中的所有文件。输入

    [root@localhost root]# find
    
    
    ………..
    
    ./.bash_profile
    
    ./.bashrc
    
    ./.cshrc
    
    ./.tcshrc
    
    ./.viminfo
    
    ./folder1
    
    ./folder1/first.txt
    
    …………
    

    您的输出显示了在不使用任何选项的情况下使用 find 时默认行为。

    它递归地显示工作目录中的所有文件和目录(包括隐藏文件)。

  3. 现在使用 find 来查找 pwd 中的目录。输入

    [root@localhost root]# find   -type   d
    .
    ./folder1
    ./folder2
    ………
    

    问题

    从上面的命令“find –type d”中;“选项”是什么,“路径”是什么,“条件”是什么,最后“操作”是什么?

  4. 接下来,您将搜索系统上所有以“.txt”为后缀的文件

    [root@localhost root]# find    /   -maxdepth   3   -name   "*.txt"   -print
    /root/folder1/first.txt
    /root/folder1/second.txt
    /root/folder1/order.txt
    /root/folder1/order2.txt
    

    问题

    再次从上面的命令中,“选项”是什么,“路径”是什么,“条件”是什么,最后“操作”是什么?(提示:操作 = “- print”)

    搜索将仅在从“/”目录开始的 3 个目录深度中执行。

    上面命令中使用的星号是 Linux 中的“通配符”之一。

    在 Linux 中使用通配符称为“globbing”。

  5. 使用 find 命令查找 pwd 中所有“小于”200 千字节的文件。输入

    [root@localhost root]# find   .   –size    -200k
    
  6. 使用 find 命令查找 pwd 中所有“大于”10 千字节的文件,并显示其“文件类型”。输入

    [root@localhost root]#  find   .  –size  +10k   –exec    file     "{ }"      ";"
    

locate

有时 find 命令的语法可能很难使用;由于其广泛的搜索范围,它也可能很慢。locate 是一个替代命令。

locate 搜索先前创建的文件系统上所有文件的数据库。

它依赖于 updatedb 程序。

search usage:

locate [-qi] [-d <path>] [--database=<path>] <search string>...

locate [-r <regexp>] [--regexp=<regexp>]

database usage: locate [-qv] [-o <file>] [--output=<file>]

locate [-e <dir1,dir2,...>] [-f <fs_type1,...> ] [-l <level>]

[-c] <[-U <path>] [-u]>

general usage: locate [-Vh] [--version] [--help]

使用 locate

  1. 切换到 folder1 目录并创建空文件 temp1、temp2 和 temp3

    [root@localhost root]# cd   folder1;   touch temp1   temp2    temp3
    [root@localhost folder1]#
    

    上面命令中使用的分号 (;) 允许您在一行上发出多个命令!

  2. 使用 locate 搜索 pwd 中所有以“temp”为后缀的文件

    [root@localhost folder1]# locate  temp*
    /root/folder1/temp_file11
    /root/folder1/temp_file12
    /root/folder1/temp_file21
    /root/folder1/temp_file22
    

    请注意,您在步骤 1 中创建的三个文件没有找到。

  3. 您将使用 updatedb 强制更新数据库,使其能够识别所有新创建的文件。输入

    [root@localhost folder1]# updatedb
    
  4. 现在再次尝试搜索。输入

    [root@localhost folder1]# locate    temp
    

    问题

    这次发生了什么?

  5. Lab 3 完成。

作者:Wale Soyinka

贡献者:Steven Spencer, Ganna Zhyrnova