美文网首页
命令行基础 、 目录和文件管理

命令行基础 、 目录和文件管理

作者: 只为美好未来 | 来源:发表于2020-04-15 13:24 被阅读0次
  1. 案例1:命令行基础技巧
  2. 案例2:挂载并访问光盘设备
  3. 案例3:ls列表及文档创建
  4. 案例4:复制、删除、移动

1 案例1:命令行基础技巧

1.1 问题

本例要求掌握Linux命令行环境的基本操作,完成下列任务:

  1. 利用Tab键快速找出下列文件:/etc/sysconfig/network-scripts/ifcfg-*、/etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
  2. 练习以下快捷编辑操作:Ctrl + l、Ctrl + u、Ctrl + w;Ctrl + c、Esc + .

1.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:利用Tab键快速补全文档路径

1)找出现有的网络连接配置文件

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@server0 ~]# ls /etc/sysco<TAB>
  2. [root@server0 ~]# ls /etc/sysconfig/netw<TAB>
  3. [root@server0 ~]# ls /etc/sysconfig/network-s<TAB>
  4. [root@server0 ~]# ls /etc/sysconfig/network-scripts/ifc<TAB>
  5. [root@server0 ~]# ls /etc/sysconfig/network-scripts/ifcfg-<TAB><TAB>
  6. ifcfg-br0 ifcfg-br1 ifcfg-lo
  7. ifcfg-br0:253 ifcfg-eno16777736

</pre>

2)找出RHEL7校验软件包的密钥文件

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@server0 ~]# ls /etc/pki/rp<TAB>
  2. [root@server0 ~]# ls /etc/pki/rpm-gpg/RP<TAB>
  3. [root@server0 ~]# ls /etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-r<TAB>
  4. [root@server0 ~]# ls /etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
  5. /etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release

</pre>

步骤二:练习以下快捷编辑操作

1)清理编辑的命令行

快速清屏:Ctrl + l

从当前光标处删除到行首:Ctrl + u

从当前光标处往前删除一个单词:Ctrl + w

2)放弃编辑的命令行

中止当前命令行:Ctrl + c

3)参数复用

在当前光标处粘贴上一条命令行的最后一个参数:Esc + .

2 案例2:挂载并访问光盘设备

2.1 问题

本例要求学会mount挂载操作。主要完成下列任务:

  1. 连接光盘 /ISO/rhel-server-7.4-x86_64-dvd.iso
  2. 将光盘挂载到 /mnt 目录,检查 /mnt 目录内容
  3. 卸载光盘设备,再次检查目录内容

2.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:使用ls命令列出指定的文件

1)连接光盘 /ISO/rhel-server-7.4-x86_64-dvd.iso

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@server0 ~]# mount /dev/cdrom /mnt //挂载设备
  2. mount: /dev/sr0 写保护,将以只读方式挂载

</pre>

2)将光盘挂载到 /mnt 目录,检查 /mnt 目录内容

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@server0 ~]# ls /mnt //访问设备内容
  2. addons images Packages RPM-GPG-KEY-redhat-release
  3. EFI isolinux release-notes TRANS.TBL
  4. EULA LiveOS repodata
  5. GPL media.repo RPM-GPG-KEY-redhat-beta

</pre>

3)卸载光盘设备,再次检查目录内容

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@server0 ~]# umount /mnt/dvd //卸载设备
  2. mount: /dev/sr0 写保护,将以只读方式挂载
  3. [root@server0 ~]# ls /mnt/dvd //确认结果
  4. [root@server0 ~]#

</pre>

3 案例3:ls列表及文档创建

3.1 问题

本例要求学会列表查看目录内容、新建文档相关技能,并熟悉通配符机制的应用。主要完成下列任务:

  1. 使用ls命令列出指定的文件:/etc/目录下以re开头.conf结尾的文件、/dev/目录下编号是个位数的tty控制台设备
  2. 一条命令创建文件夹 /protected/project/tts10
  3. 使用 vim 创建文件 /etc/hostname,编写一行内容:svr7.tedu.cn

3.2 方案

对于通配符使用,需理解每个通配符的作用:

  • *:任意多个任意字符
  • ?:单个字符
  • [a-z]:多个字符或连续范围中的一个,若无则忽略
  • {a,min,xy}:多组不同的字符串,全匹配

vim是Linux系统上最常用的命令行交互式文本编辑器,主要工作在三种模式:命令模式、输入模式、末行模式。

通过vim打开一个文件时,默认处于命令模式;从命令模式按i键可以进入编辑状态,按Esc键返回命令模式;从命令模式输入冒号:可以进入末行模式,在末行模式下主要执行存盘、退出等基本操作。

3.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:使用ls命令列出指定的文件

1)列出/etc/目录下以re开头.conf结尾的文件

使用通配符 * 代替未知的字符串。

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@server0 ~]# ls /etc/re*.conf
  2. /etc/request-key.conf /etc/resolv.conf

</pre>

2)列出/dev/目录下编号是个位数的tty控制台设备

使用通配符 ? 代替单个未知的字符。

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@server0 ~]# ls /dev/tty?
  2. /dev/tty0 /dev/tty2 /dev/tty4 /dev/tty6 /dev/tty8
  3. /dev/tty1 /dev/tty3 /dev/tty5 /dev/tty7 /dev/tty9

</pre>

或者更严谨一些,使用 [0-9] 代替单个数字。

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@server0 ~]# ls /dev/tty[0-9]
  2. /dev/tty0 /dev/tty2 /dev/tty4 /dev/tty6 /dev/tty8
  3. /dev/tty1 /dev/tty3 /dev/tty5 /dev/tty7 /dev/tty9

</pre>

步骤二:新建文档

1)使用mkdir新建文件夹

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@server0 ~]# mkdir -p /protected/project/tts10
  2. [root@server0 ~]# ls -ld /protected/project/tts10/
  3. drwxr-xr-x. 2 root root 6 Aug 30 10:11 /protected/project/tts10/

</pre>

2)使用vim新建或修改文本文件

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@server0 ~]# vim /etc/hostname
  2. //按i键进入编辑模式
  3. //将文本内容修改为 svr7.tedu.cn
  4. //按Esc键返回命令模式
  5. //输入:wq保存修改并退出vim编辑器
  6. [root@server0 ~]# cat /etc/hostname
  7. svr7.tedu.cn

</pre>

4 案例4:复制、删除、移动

4.1 问题

本例要求学会对文档进行复制、删除、移动/改名相关操作,依次完成下列任务:

  • 在当前目录下创建一个子目录 dir1
  • 将文件夹 /boot/grub2/ 复制到目录dir1下
  • 将目录 /root/ 下以 .cfg 结尾的文件复制到dir1下
  • 将文件 /etc/redhat-release复制到 /root/ 下,同时改名为 version.txt
  • 将文件 /root/version.txt 移动到dir1目录下
  • 删除 dir1 目录下的 grub2 子目录

4.2 步骤

实现此案例需要按照如下步骤进行。

1)在当前目录下创建一个子目录 dir1

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@server0 ~]# mkdir dir1

</pre>

2)将文件夹 /boot/grub2/ 复制到目录dir1下

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@server0 ~]# cp -r /boot/grub2/ dir1/
  2. [root@server0 ~]# ls -ld dir1/* //检查复制结果
  3. drwxr-xr-x. 6 root root 104 Aug 30 10:27 dir1/grub2

</pre>

3)将目录 /root/ 下以 .cfg 结尾的文件复制到dir1下

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@server0 ~]# cp /root/*.cfg dir1/
  2. [root@server0 ~]# ls -ld dir1/* //检查复制结果
  3. -rw-------. 1 root root 16793 Aug 30 10:29 dir1/anaconda-ks.cfg
  4. drwxr-xr-x. 6 root root 104 Aug 30 10:27 dir1/grub2

</pre>

4)将文件 /etc/redhat-release复制到 /root/ 下,同时改名为 version.txt

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@server0 ~]# cp /etc/redhat-release /root/version.txt
  2. [root@server0 ~]# ls -ld /root/version.txt //检查复制结果
  3. -rw-r--r--. 1 root root 52 Aug 30 10:30 /root/version.txt

</pre>

5)将文件 /root/version.txt 移动到dir1目录下

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@server0 ~]# cp /root/version.txt dir1/
  2. [root@server0 ~]# ls -ld dir1/* //检查移动/改名结果
  3. -rw-------. 1 root root 16793 Aug 30 10:29 dir1/anaconda-ks.cfg
  4. drwxr-xr-x. 6 root root 104 Aug 30 10:27 dir1/grub2
  5. -rw-r--r--. 1 root root 52 Aug 30 10:31 dir1/version.txt

</pre>

6)删除 dir1 目录下的grub2子目录

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@server0 ~]# rm -rf dir1/grub2/
  2. [root@server0 ~]# ls -ld dir1/* //检查删除结果
  3. -rw-------. 1 root root 16793 Aug 30 10:29 dir1/anaconda-ks.cfg
  4. -rw-r--r--. 1 root root 52 Aug 30 10:31 dir1/version.txt

</pre>

相关文章

  • 命令行基础 、 目录和文件管理

    案例1:命令行基础技巧 案例2:挂载并访问光盘设备 案例3:ls列表及文档创建 案例4:复制、删除、移动 1 案例...

  • 命令行作业

    命令行的基础 常用命令行 图示1.ls 显示文件目录列表

  • Linux常用基础命令行

    Linux命令行基础 基本命令 查看当前完整路径 查看当前目录下文件 切换目录 创建文件 删除文件 重命名文件 创...

  • 2018-07-01

    Linux的文件管理和Bash的基础特性 一:Linux的文件管理类命令 (一)目录管理命令: 1、ls:用于显示...

  • Linux学习第三天

    通过命令行管理文件和文件夹 查看及切换目录(Windows中叫做文件夹)pwd查看工作目录(Print Worki...

  • 文件管理

    回顾:文件系统、bash 的基础特性,目录管理、文件查看、时间戳管理 文件管理:cp, mv, rm 复制命令: ...

  • 关于命令行

    『为什么要使用命令行』 个人认为命令行最大优点就是可以提高效率。 『文件和目录管理的基本命令』 ls [OPTIO...

  • 我的Linux手册

    基础安装 命令行Tips 进程及端口 查找与统计 文件操作 远程ssh 包管理 磁盘管理 用户管理 系统相关 防火...

  • Windows常用运行命令

    运行命令: cmd命令行文件和目录管理: windows键的快捷操作: 运行窗口的快捷命令: 查看当前系统版本号,...

  • Linux的简单学习记录(三)

    Linux的文件目录管理一、文件和目录管理 1.cd命令与pwd命令 进入文件目录与现实当前文件目录 2.mkdi...

网友评论

      本文标题:命令行基础 、 目录和文件管理

      本文链接:https://www.haomeiwen.com/subject/ggccvhtx.html