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

  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>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
禁止转载,如需转载请通过简信或评论联系作者。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,919评论 6 502
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,567评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,316评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,294评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,318评论 6 390
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,245评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,120评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,964评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,376评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,592评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,764评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,460评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,070评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,697评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,846评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,819评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,665评论 2 354

推荐阅读更多精彩内容