欢迎来到study-hard-forever的博客

操作系统实验之掌握基本SHELL命令(一)

实验一  掌握基本SHELL命令

【实验要求

  1. 所有练习保留题目要求,在题目要求后面直接贴上在虚拟机执行的命令及输出结果(可从虚拟机拷贝过来),或者是屏幕截图
  2. 保持排版整齐

提示:可练习开始前在虚拟机中新建一个当前系统的快照---“命令练习”,以备以后恢复快照。

 

【实验练习

1.完成下列文件和目录的基本操作:

(1)查看当前工作目录为用户的主目录。

直接ls即可显示当前目录

 

deepin@deepin-MJG:~/Desktop$ ls

'eclipse 快捷方式'   mysql-5.6.33-linux-glibc2.5-x86_64.tar.gz   test

deepin@deepin-MJG:~/Desktop$ cd   #退回到主目录

deepin@deepin-MJG:~$ ls

Bookmarks.html  Documents  eclipse            epidemic_management-0.0.1-SNAPSHOT.jar  Pictures

Desktop         Downloads  eclipse-workspace  Music   

(2)提示:mkdir

在当前家目录下创建一个新目录,目录名用自己名字的全拼,以下用yournamedir指代。另外,下面出现的yourname指代的姓名全拼定义的文件。

mkdir MJG

(3)提示:touch、cp

创建两个文件:yourname.my(编辑yourname.my文件,内容为自己的班级学号姓名等信息)、空文件yourname-2.my

将创建的文件拷贝到新目录yournamedir中,再将yourname-2.my更名为yourname-file.my

deepin@deepin-MJG:~/Desktop$ mkdir MJG

deepin@deepin-MJG:~/Desktop$ touch mjg.file

deepin@deepin-MJG:~/Desktop$ touch mjg.file2

deepin@deepin-MJG:~/Desktop$ cp mjg.file MJG  #复制文件

deepin@deepin-MJG:~/Desktop$ mv mjg.file2 MJG  #剪切(移动)文件

deepin@deepin-MJG:~/Desktop$ cd MJG

deepin@deepin-MJG:~/Desktop/MJG$ ls

mjg.file  mjg.file2

deepin@deepin-MJG:~/Desktop/MJG$ mv mjg.file2 mjg.filerename

deepin@deepin-MJG:~/Desktop/MJG$ ls

mjg.file  mjg.filerename

 

Test建立文件链接:

deepin@deepin-MJG:~/Desktop/MJG$ ls

mjg.file  mjg.filerename  test

deepin@deepin-MJG:~/Desktop/MJG$ ln mjg.file ~/test

deepin@deepin-MJG:~/Desktop/MJG$ ls -l mjg.file ~/test

-rw-r--r-- 2 deepin deepin 0 11月 26 08:27 /home/deepin/test

-rw-r--r-- 2 deepin deepin 0 11月 26 08:27 mjg.file

deepin@deepin-MJG:~/Desktop/MJG$ stat mjg.file

  文件:mjg.file

  大小:0          块:0          IO 块:4096   普通空文件

设备:806h/2054d Inode:659366      硬链接:2

权限:(0644/-rw-r--r--)  Uid:( 1000/  deepin)   Gid:( 1000/  deepin)

最近访问:2020-11-26 08:47:49.252928014 +0800

最近更改:2020-11-26 08:27:56.634175265 +0800

最近改动:2020-11-26 08:47:49.248928122 +0800

创建时间:-

deepin@deepin-MJG:~/Desktop/MJG$ stat ~/test

  文件:/home/deepin/test

  大小:0          块:0          IO 块:4096   普通空文件

设备:806h/2054d Inode:659366      硬链接:2

权限:(0644/-rw-r--r--)  Uid:( 1000/  deepin)   Gid:( 1000/  deepin)

最近访问:2020-11-26 08:47:49.252928014 +0800

最近更改:2020-11-26 08:27:56.634175265 +0800

最近改动:2020-11-26 08:47:49.248928122 +0800

创建时间:-

 

#修改mjg.file文件后:

deepin@deepin-MJG:~/Desktop/MJG$ stat ~/test

  文件:/home/deepin/test

  大小:6          块:8          IO 块:4096   普通文件

设备:806h/2054d Inode:659366      硬链接:2

权限:(0644/-rw-r--r--)  Uid:( 1000/  deepin)   Gid:( 1000/  deepin)

最近访问:2020-11-26 08:51:45.332715173 +0800

最近更改:2020-11-26 08:51:45.328715216 +0800

最近改动:2020-11-26 08:51:45.328715216 +0800

创建时间:-

deepin@deepin-MJG:~/Desktop/MJG$ stat mjg.file

  文件:mjg.file

  大小:6          块:8          IO 块:4096   普通文件

设备:806h/2054d Inode:659366      硬链接:2

权限:(0644/-rw-r--r--)  Uid:( 1000/  deepin)   Gid:( 1000/  deepin)

最近访问:2020-11-26 08:51:45.332715173 +0800

最近更改:2020-11-26 08:51:45.328715216 +0800

最近改动:2020-11-26 08:51:45.328715216 +0800

创建时间:-

deepin@deepin-MJG:~/Desktop/MJG$ ls -l mjg.file ~/test

-rw-r--r-- 2 deepin deepin 6 11月 26 08:51 /home/deepin/test

-rw-r--r-- 2 deepin deepin 6 11月 26 08:51 mjg.file

deepin@deepin-MJG:~/Desktop/MJG$ rm -f mjg.file

deepin@deepin-MJG:~/Desktop/MJG$ ls -l mjg.file ~/test

ls: 无法访问'mjg.file': 没有那个文件或目录

-rw-r--r-- 1 deepin deepin 6 11月 26 08:51 /home/deepin/test

deepin@deepin-MJG:~/Desktop/MJG$ cat /home/deepin/test

hello

 

(4)提示:ln建软链接

创建yourname.my的软链接yourname-soft.my

使用软链接文件yourname-soft.my显示yourname.my文件的内容,

如何能查看到文件yourname-soft.my 和yourname.my文件类型的区别?

deepin@deepin-MJG:~/Desktop/MJG$ cd test

deepin@deepin-MJG:~/Desktop/MJG/test$ ls

mjg.filerename

deepin@deepin-MJG:~/Desktop/MJG/test$ touch mjg.file

#当文件存在时无法建立软链接或者硬链接(不可用一个已知文件当做另一个已知文件的链接):

deepin@deepin-MJG:~/Desktop/MJG/test$ ln -s mjg.file mjg.filerename

ln: 无法创建符号链接'mjg.filerename': 文件已存在

deepin@deepin-MJG:~/Desktop/MJG/test$ ln -s mjg.filerename mjg.file

ln: 无法创建符号链接'mjg.file': 文件已存在

deepin@deepin-MJG:~/Desktop/MJG/test$ ln mjg.file mjg.filerename

ln: 无法创建硬链接'mjg.filerename': 文件已存在

#创建mjg.softfile的本地链接(快捷方式):

deepin@deepin-MJG:~/Desktop/MJG/test$ ln -s mjg.file mjg.softfile

deepin@deepin-MJG:~/Desktop/MJG/test$ ls -l mjg.file ~/mjg.softfile

ls: 无法访问'/home/deepin/mjg.softfile': 没有那个文件或目录

-rw-r--r-- 1 deepin deepin 0 11月 26 09:09 mjg.file

#创建mjg.softfile的软链接:

deepin@deepin-MJG:~/Desktop/MJG/test$ ln -s mjg.file ~/mjg.softfile

deepin@deepin-MJG:~/Desktop/MJG/test$ ls -l mjg.file ~/mjg.softfile

lrwxrwxrwx 1 deepin deepin 8 11月 26 09:14 /home/deepin/mjg.softfile -> mjg.file

-rw-r--r-- 1 deepin deepin 7 11月 26 09:13 mjg.file

deepin@deepin-MJG:~/Desktop/MJG/test$ ls -a

.  ..  mjg.file  mjg.filerename  mjg.softfile

#软链接Inode不一样:

deepin@deepin-MJG:~/Desktop/MJG/test$ stat mjg.file

  文件:mjg.file

  大小:7          块:8          IO 块:4096   普通文件

设备:806h/2054d Inode:787774      硬链接:1

权限:(0644/-rw-r--r--)  Uid:( 1000/  deepin)   Gid:( 1000/  deepin)

最近访问:2020-11-26 09:13:53.491020046 +0800

最近更改:2020-11-26 09:13:53.475020165 +0800

最近改动:2020-11-26 09:13:53.475020165 +0800

创建时间:-

deepin@deepin-MJG:~/Desktop/MJG/test$ stat /home/deepin/mjg.softfile

  文件:/home/deepin/mjg.softfile -> mjg.file

  大小:8          块:0          IO 块:4096   符号链接

设备:806h/2054d Inode:659384      硬链接:1

权限:(0777/lrwxrwxrwx)  Uid:( 1000/  deepin)   Gid:( 1000/  deepin)

最近访问:2020-11-26 09:15:11.250467485 +0800

最近更改:2020-11-26 09:14:24.418795154 +0800

最近改动:2020-11-26 09:14:24.418795154 +0800

创建时间:-

#ls -la查看文件详细信息

deepin@deepin-MJG:~/Desktop/MJG/test$ ls -la

总用量 16

drwxr-xr-x 2 deepin deepin 4096 11月 26 09:12 .

drwxr-xr-x 3 deepin deepin 4096 11月 26 08:56 ..

-rw-r--r-- 1 deepin deepin    7 11月 26 09:13 mjg.file

-rw-r--r-- 1 deepin deepin   21 11月 26 08:53 mjg.filerename

lrwxrwxrwx 1 deepin deepin    8 11月 26 09:12 mjg.softfile -> mjg.file

 

(5)提示:ln建硬链接

建立一个yourname.my文件的链接文件yourname-hard.my。

删除yourname.my后能浏览输出yourname-hard.my的内容吗?

deepin@deepin-MJG:~/Desktop/MJG/test$ ln mjg.filerename mjg.test

deepin@deepin-MJG:~/Desktop/MJG/test$ ls

mjg.file  mjg.filerename  mjg.softfile  mjg.test

删除后依然能浏览内容。

(6)提示:cat,head,tail,more,less

显示文件/etc/passwd内容的命令,体会各有什么不同。

cat 显示本文文件、连接文件内容

命令格式:cat  [选项]  [文件]

【例】 假设有file1、file2、file3三个文本文件,

将这三个文本文件连接起来输出到文本文件file4中。

[user@localhost ~]$ cat file1 file2 file3>file4

2.more 分页显示文本文件

命令格式:more  [选项]  [文件]

3.head 显示指定文件的前若干行

命令格式:head   -n  行数值  [文件]

默认缺省显示文件的前10行内容。

head  -5  /etc/passwd表示显示/etc/passwd文件的前5行。

4.tail 查看文件末尾数据

命令格式:tail  -n  行数值 [文件]

默认缺省显示文件的后10行内容

tail  -5  /etc/passwd表示显示/etc/passwd文件的后5行。

  1. grep  [选项]  查找条件  源文件

-i 查找时忽略大小写

-v 反转查找,输出与查找条件不相符的行

要查找的字符串以双引号括起来

“^……”表示以……开头,“……$”表示以……结尾

“^$”表示空行

 

deepin@deepin-MJG:~/Desktop/MJG/test$ ls

mjg.file  mjg.softfile  mjg.test

deepin@deepin-MJG:~/Desktop/MJG/test$ cat mjg.test

hello

my name is MJG

lalala

 

deepin@deepin-MJG:~/Desktop/MJG/test$ more mjg.test

hello

my name is MJG

lalala

 

deepin@deepin-MJG:~/Desktop/MJG/test$ head -1 mjg.test

hello

deepin@deepin-MJG:~/Desktop/MJG/test$ tail -2 mjg.test

lalala

 

deepin@deepin-MJG:~/Desktop/MJG/test$ tail mjg.test

hello

my name is MJG

lalala

 

deepin@deepin-MJG:~/Desktop/MJG/test$ grep 'hell' mjg.test

hello

deepin@deepin-MJG:~/Desktop/MJG/test$ grep 'my name' mjg.test

my name is MJG

deepin@deepin-MJG:~/Desktop/MJG/test$ grep 'llo$' mjg.test

hello

deepin@deepin-MJG:~/Desktop/MJG/test$ less mjg.test

deepin@deepin-MJG:~/Desktop/MJG/test$ grep -v "hell" mjg.test

my name is MJG

lalala

 

(7)提示:find,输出重定向>

查找/etc目录下以名字中包含“conf”的文件,保存查到的结果信息到/tmp/yourname-find中。

root@deepin-MJG:/home/deepin/Desktop/MJG/test# find /etc -name "*conf*" -type f >/home/deepin/Desktop/MJG/test/test.file

find输出重定向可以实现上述操作将内容保存到文件,也可以通过文件复制方式将查询到的文件复制到指定文件夹。

文件复制方式:

当权限不够时:

deepin@deepin-MJG:~/Desktop/MJG/test$ find /etc -name "*conf*" -type f

find: ‘/etc/vpnc’: 权限不够

find: ‘/etc/polkit-1/localauthority’: 权限不够

find: ‘/etc/chatscripts’: 权限不够

find: ‘/etc/ppp/peers’: 权限不够

find: ‘/etc/ipsec.d/private’: 权限不够

find: ‘/etc/cups/ssl’: 权限不够

find: ‘/etc/ssl/private’: 权限不够

deepin@deepin-MJG:~/Desktop/MJG/test$ su root

密码:

root@deepin-MJG:/home/deepin/Desktop/MJG/test# find /etc -name "*conf*" -type f

/etc/gtk-3.0/im-multipress.conf

/etc/ca-certificates.conf

/etc/libaudit.conf

/etc/resolv.conf

/etc/ucf.conf

...

将查询结果保存到指定位置:

root@deepin-MJG:/home/deepin/Desktop/MJG/test# find /etc -name "*conf*" -type f -exec  cp  '{}'  /tmp/myfind  \;

 

deepin@deepin-MJG:/tmp$ mkdir myfind

deepin@deepin-MJG:/tmp$ cd myfind

deepin@deepin-MJG:/tmp/myfind$ ls

000-default.conf                           fonts.conf                           php7.3.conf

00-disable-autospawn.conf                  fprintd.conf                         pie.conf

...

 

(8)提示:grep

在文件/etc/passwd中查找到所有包含字符串nologin的行,将找出的行内容保存到一个名为yourname-grep的文件。

deepin@deepin-MJG:~/Desktop/MJG/test$ grep /sbin/nologin /etc/passwd>/home/deepin/Desktop/MJG/test/test.file

 

 

2.完成下列文件打包(带压缩)

(1)打包归档当前家目录下的~/yournamedir,生成~/yournamedir.tar。

命令格式:tar  [参数]  打包文件名 文件

选项 说明

-c 产生.tar打包文件

-v 列出打包解包的详细过程

-f 指定打包文件的名称

-z 以.gz的格式压缩或解压打包文件

-j 以.bz2的格式压缩或解压打包文件

-x 从打包文件中还原文件

-C 解压到指定目录

-r 将文件追加到打包文档末尾

打包:

tar  -czvf  [存放路径]打包文件名.tar.gz  源文件或目录

tar  -cjvf  [存放路径]打包文件名.tar.bz2  源文件或目录

tar  cJvf   [存放路径]打包文件名.tar.xz  源文件或目录

解包:

tar  -xzvf  [存放路径]打包文件名.tar.gz   [-C  解压目录]

tar  -xjvf  [存放路径]打包文件名.tar.bz2  [-C  解压目录]

tar  xJvf  [存放路径]打包文件名.tar.xz   [-C  解压目录]

 

【 例】将当前目录下的file.c文件添加到文件myfile.tar中。

[user@localhost ~]$ tar  rvf  myfile.tar  file.c

【 例 】将当前用户目录下的目录文件myfile包括的所有文件用tar命令打包,然后用gzip命令压缩,打包压缩后的文件名为myfile.tar.gz。

[user@localhost ~]$ tar -zcvf myfile.tar.gz myfile/

【 例】查看myfile.tar.gz压缩文件的内容。

[user@localhost ~]$ tar -ztvf myfile.tar.gz

【 例】将文件打包然后用bzip2命令压缩。将当前用户目录/home/user下的目录文件myfile包括的所有文件用tar命令打包,并且用bzip2命令压缩,打包压缩后的文件名为myfile.tar.bz2。

[user@localhost ~]$tar -jcvf myfile.tar.bz2 myfile/

【 例】用tar命令解压经过bzip2压缩的tar文件。由于使用bzip2压缩,所以要加上j这个参数。将压缩文件myfile.tar.bz2解压到当前目录下。

[user@localhost ~]$tar -jxvf myfile.tar.bz2

 

 

Test打包(带压缩):root@deepin-MJG:/home/deepin/Desktop/MJG/test# tar czvf mjg.file.tar.gz mjg.file

mjg.file

root@deepin-MJG:/home/deepin/Desktop/MJG/test# ls

mjg.file  mjg.file.tar.gz  mjg.softfile  mjg.test  test.file

Test解包:

root@deepin-MJG:/home/deepin/Desktop/MJG/test# tar -xzvf mjg.file.tar.gz mjg.file

mjg.file

 

打包当前test目录:

deepin@deepin-MJG:~/Desktop/MJG$ tar -zcvf test.tar.gz test/

test/

test/mjg.test

test/mjg.file.tar.gz

test/mjg.softfile

test/test.file

test/mjg.file

test/mjg/

 

  1. 查看~/yournamedir.tar归档文件包里的内容。

查看test.tar.gz内的内容:

deepin@deepin-MJG:~/Desktop/MJG$ tar -ztvf test.tar.gz

drwxr-xr-x deepin/deepin     0 2020-11-26 19:44 test/

-rw-r--r-- deepin/deepin    29 2020-11-26 12:24 test/mjg.test

-rw-r--r-- root/root       139 2020-11-26 19:34 test/mjg.file.tar.gz

lrwxrwxrwx deepin/deepin     0 2020-11-26 09:12 test/mjg.softfile -> mjg.file

-rw-r--r-- deepin/deepin    31 2020-11-26 18:39 test/test.file

-rw-r--r-- deepin/deepin     7 2020-11-26 09:13 test/mjg.file

drwxr-xr-x root/root         0 2020-11-26 19:37 test/mjg/

 

(3)将归档文件~/yournamedir .tar解包出来,到指定文件夹yourname-extract

解包:

deepin@deepin-MJG:~/Desktop/MJG$ tar -xzvf test.tar.gz

test/

test/mjg.test

test/mjg.file.tar.gz

test/mjg.softfile

test/test.file

test/mjg.file

test/mjg/

 

3.权限相关操作

(1)修改你的用户的密码。

passwd  [选项] 用户名

 

deepin@deepin-MJG:~/Desktop/MJG$ passwd deepin

为 deepin 更改 STRESS 密码。

Current password:

新的 密码:

重新输入新的 密码:

passwd:已成功更新密码

 

(2)从你的普通用户切换到root用户。

deepin@deepin-MJG:~/Desktop/MJG$ su root

密码:

root@deepin-MJG:/home/deepin/Desktop/MJG#

 

(3)新建目录/home/yourname,并设置权限,提示:chown,chmod

1)将此目录的所有者设置为root,并设置所有者具有读写执行权限。

deepin@deepin-MJG:/home$ su root

密码:

root@deepin-MJG:/home# mkdir MJG

root@deepin-MJG:/home# chmod u+rwx MJG

root@deepin-MJG:/home# chmod goa-rwx MJG

2)除了所有者,任何用户对改文件只能只读。

root@deepin-MJG:/home# chmod goa+r MJG

 

 

4.建一个文件/root/ab,使用文字设定法对文件设置权限,用户所有者为读取、写入和执行权限,同组用户为读取和写入权限,而其他用户没有任何权限。

deepin@deepin-MJG:~/Desktop$ su root

密码:

root@deepin-MJG:/home/deepin/Desktop# mkdir ab

root@deepin-MJG:/home/deepin/Desktop# chmod u+rwx ab

root@deepin-MJG:/home/deepin/Desktop# chmod g+rw-x ab

root@deepin-MJG:/home/deepin/Desktop# chmod oa-rwx ab

  1. cat带行号浏览/etc/passwd的内容,并利用重定向将内容写入yourname.txt文件,执行vi,编辑该文件,保留行号为10到20行的内容,其他删除,然后保存该文件。

root@deepin-MJG:/home/deepin/Desktop# cat /etc/passwd>test.txt

root@deepin-MJG:/home/deepin/Desktop# vim test.txt

使用vi 命令也可以:

root@deepin-MJG:/home/deepin/Desktop# vi test.txt

Vim中要保留10-20行数据首先将光标定位到第一行1G,然后删除1-9行9dd,然后定位到12G(10-20行数据实际为11行,删除要从第12行开始删除),然后dg删除后面所有数据,最后:wq!保存退出。

 

 

【恢复】体验快照功能,通过把虚拟机恢复到之前的 “命令练习”快照,重新再练习一遍。

 

posted @ 2021-01-19 00:14  study-hard-forever  阅读(281)  评论(0编辑  收藏  举报