Linux基本命令练习
- 在你的家目录中,创建若干空文件。创建 6 个空文件 song1.mp3~song6.mp3;创建 6 个空文件
snap1.jpg~snap6.jpg;创建 6 个空文件 film1.avi~film6.avi
[root@localhost ~]# mkdir song{1..6}.mp3
[root@localhost ~]# mkdir snap{1..6}.jpg film{1..6}.avi
-
将 mp3 文件移动到"音乐"目录,将 jpg 文件移动到"图片"目录,将 avi 文件移动到"视 频"目录
[root@localhost ~]# mkdir 音乐 图片 视频 [root@localhost ~]# mv *.avi 视频 [root@localhost ~]# mv *.jpg 图片 [root@localhost ~]# mv *.mp3 音乐
-
在你的家目录中创建三个空目录,分别为 friends,family 和 work。
[root@localhost ~]# mkdir {friends,family,work}
-
将刚才创建的 mp3,jpg 和 avi 文件,1、2 号复制到 friends 目录中,3、4 号复制到 family 目录
中,5、6 号复制到 work 目录中。
[root@localhost ~]# cp 图片/snap{1..2}.jpg 视频/film{1..2}.avi 音乐/song{1..2}.mp3 friends/
[root@localhost ~]# cp 图片/snap{3,4}.jpg 视频/film{3,4}.avi 音乐/song{3,4}.mp3 family/
[root@localhost ~]# cp 图片/snap{5,6}.jpg 视频/film{5,6}.avi 音乐/song{5,6}.mp3 work/
-
回到你的家目录,尝试使用 rmdir 命令同时删除 family 和 friends 目录。
rmdir 不能删除,只能删除空目录
-
尝试使用 rm 命令同时删除 family 和 friends 目录。
[root@localhost ~]# rm -rf family/ friends/
-
删除 work 目录下的内容,但不要删除 work 目录本身。
[root@localhost ~]# cd work/ [root@localhost work]# rm -f *
-
使用 rmdir 命令删除空目录 work
[root@localhost ~]# rmdir work/
-
创建 12 个空文件,命名规则为 tv_seasonX_episodeY.ogg,X 的取值范围为 1~2,Y 的 取值范围
为 1~6。[root@localhost ~]# touch tv_season{1..2}_episode{1..6}.ogg
-
创建 8 个空文件,命名规则为 mystery_chapterX.odf,X 的取值范围为 1~8。
[root@localhost ~]# touch mystery_chapter{1..8}.odf
-
在家目录的“视频”目录中创建两个子目录 season1 和 season2
[root@localhost ~]# mkdir 视频/season1 视频/season2
-
移动刚才创建的 tv_season1 开头的文件到“视频/season1”目录中。tv_season2 开 头的文件到“视
频/season2”目录中。[root@localhost ~]# mv tv_season1* 视频/season1 tv_season2* 视频/season2
-
使用一条命令在家目录的“文档”目录中创建 my_bastseller 目录,并在 my_bastseller 目录中创建
chapters 目录[root@localhost ~]# mkdir my_bastseller/chapters -p
-
使用一条命令在刚才创建的 my_bastseller 目录中创建 editor,plot_change,vacation 三个子目录。
[root@localhost my_bastseller]# mkdir {editor,plot_change,vacation}
-
切换到 chapters 目录中,并将家目录中所有 mystery_chapter 开头的文件复制到其 中。
[root@localhost my_bastseller]# cp /root/my_bastseller/mystery_chapter* chapters/
-
接上一题,将刚复制过来的 mystery_chapter 文件中的前两个文件移动到 editor 目录 中,使用相
对路径。[root@localhost chapters]# mv mystery_chapter{1,2}.odf ../editor/
-
将复制到 chapters 目录中的 mystery_chapter 文件中的 7、8 号文件移动到 vacation 目录中,用
一条命令完成。[root@localhost my_bastseller]# mv chapters/mystery_chapter{7,8}.odf vacation/
-
统计/boot目录大小,以M为单位显示
[root@localhost /]# du -h /boot
-
查看系统中 cpu 的信息。
[root@localhost /]# cat /proc/cpuinfo
-
使用分页查看/var/log/messages 文件
[root@localhost /]# more /var/log/messages
-
查看/var/log/messages 文件最后 10 条记录
[root@localhost ~]# tail /root/abc.cfg
二、vim编辑器练习
-
复制/etc/ssh/sshd_config文件到你的家目录里面,并改名为test_config
[root@localhost ~]# cp /etc/ssh/sshd_config test_config
-
使用vim打开test_config文件
[root@localhost ~]# vim test_config
-
在第二行中输入:#this is a vim test
vim test_config o键 2 # this is a vim test
-
将1-6行中的sshd替换成SSHD
:1,6 s/sshd/SSHD/g
-
将所有的ssh替换成SSH
:% s/ssh/SSH/g
-
将文件另存为test2_config
:w test2_config
-
不保存退出文件
:q!
-
将/boot/grub2/grub.cfg复制到/root目录下,并改名为abc.cfg
[root@localhost ~]# cp /boot/grub2/grub.cfg /root/abc.cfg
-
使用vim编辑abc.cfg
[root@localhost ~]# vim /root/abc.cfg
-
设置显示行号
:set nu
-
将光标定位到第20行
20G
-
复制5行内容,并将其粘贴到文本末尾
复制5行:5yy 到文本末尾:G 粘贴:p
-
光标快速跳转到第30行,删除当前行
30G dd #删除光标所在的当前行
-
删除当前光标出下3行内容
3dd #删除3行内容
-
复制当前光标处一下5行内容,将其粘贴到文本最开始位置
5yy #复制5行内容 gg #将光标移动到文件首行 p #粘贴内容
-
查找文本中的kernel关键字
/kernel
-
移动光标到文本末尾,自下而上查找grub.d关键字
?kernrl
-
取消行号
:set nonu
-
保存退出
:wq :x