文件管理命令、vim文件编辑、常用的文本处理工具

1.文件管理命令

1)新建文件:    touch  + 文件名称
    新建目录:    mkdir  + 目录名称
2)删除文件:    rm  -f  + 文件名称  (-f 是force的缩写,表示强制删除不提示)
    删除目录:   rm  -f -r + 目录名称    (-r 表示递归,就是目录本身和里面的所有内容 )

注意: -a -b -c =-abc = -bac  
即:删除目录也可写为: rm -fr  +目录名称
3)复制:  cp  + 文件名称/目录名称 + 新文件名称/新目录名称   

注意:cp file1 file2  #将file1的内容复制到file2中,这样会覆盖原文件的内容

这里写图片描述

4)移动(重命名):  mv  + 文件名称/目录名称 + 新文件名称/新目录名称   

注意:  /* 表示所有   . 代表当前所在系统目录
例如: mv  westos/* .  #把目录westos中的所有内容移动到当前所在系统位置
mv file1 file2  #将file1移动到file2,即为将file1的内容复制给file2,这样仍然会覆盖原文件的内容

这里写图片描述

移动与复制的区别:
   移动:只是文件/目录的存放位置发生改变,始终只有一份文件/目录
   复制:即为文件/目录的备份,总共有两份文件/目录
5)查看文件内容:  cat + 文件名称

(6)显示目录中的文件名: ls  + 目录名称
              ls  -a  + 目录名     #-a表示显示所有文件包括以点开头的隐藏文件
              ls  -r  + 目录名     #-r表示递归显示目录中的所有内容

这里写图片描述
2.文件的编辑

(1)基本操作:
vim +文件名   进入浏览模式,
按"i"键  进入插入模式开始编写字符,
编写结束后按"ESC"键 退出插入模式,
最后按":wq" 保存退出,文件编写完成。
(2) gedit + 文件名称 + &   
#"&"的作用是让gedit命令在后台运行而不占用当前接口
(3) vim的三种模式
 1)命令模式:
它是vim打开文件后默认进入的模式,这个模式中不能插入字符,但是可以设定vim的工作方式

 1.vim工作方式设定
命令模式是vim打开文件后默认进入的模式,这个模式中不能插入字符,不显示鼠标即不能用 鼠标直接操作,但是可以设定vim的工作方式

:set   ##指定信息

:set nu        #添加行号                     
:set nonu      #删除行号
:set mouse=a   #显示鼠标,并允许鼠标直接操作
:set cursorline  #添加下划线      

注意:在vim中直接设定vim的工作方式是临时设定,也就是说当vim关闭,设定失效
那么如果要永久设定,则需要编写"/etc/vimrc".这个文件也是vim的配置文件     

 2.在命令模式下如何管理字符
x        #删除光标所在字符       
u        #恢复(undo的缩写) 
ctrl+r   #撤销恢复(redo的缩写)

y   ##复制
yw       #复制一个单词   
yl       #复制一个字母
yy       #复制一整行      
y5y      #复制5行(从上到下)

c   ##剪切
cw       #剪掉一个单词word    
cl       #剪掉一个字母
cc       #剪掉一整行      
c5c      #剪掉5行

注意:"用c做剪切时,会进入插入模式,如果要粘贴需要退出插入模式按<ESC>再按P"

p   ##粘贴

d   ##删除
dw      #删除一个单词 
dl      #删除一个字母
dd      #删除一整行                    
d5d     #删除5行

2)插入模式:   
 i     #在光标所在位置插入字符                 
 I     #在光标所在行的行首插入字符
 o     #光标所在行的下一行新行插入字符   
 O     #光标所在行的上一行新行插入字符
 s     #删除光标所在字符并插入字符     
 S     #删除光标所在行并插入字符
 a     #光标所在字符的下一个字符插入       
 A     #光标所在行的行尾插入字符

3)退出模式:
:q     #当vim进入文件没有对文件内容做任何操作可以按"q"退出
:q!    #当vim进入文件对文件内容有操作但不想保存退出
:wq    #正常保存退出               
:wq!   #强行保存退出,只针对于root用户或文件所有人生效
4)vim的可视化操作 
1.基本操作
在vim中是不能用鼠标选择区域操作的。如果要操作某个区域,需要进入到可视化模式
在命令模式下ctrl+v就可以进入到可视模式了,按"上|下|左|右"移动光标选择区域
按相应操作键就可以对选择区域进行操作

2.可视化模式下批量添加字符
把光标停留在想要加入字符所在的列的第一行,移动光标选择添加字符所在列
按"I"进入插入模式,写入要插入的字符,按"ESC"退出插入模式,字符批量添加成功

3.vim的字符替换 
:%s/原有字符/新字符/g          #全文替换
:1,5s/原有字符/新字符/g        #替换1到5行的字符

注意:g替换出现的所有关键字,如果不加"g"只替换出现的第一个

4.vim总字符的查找及光标的移动 

(1)字符的查找       
/关键字
n  向下匹配关键字             
N  向上匹配关键子

(2)光标的移动 
:数字      #光标快速移动到指定行
gg        #光标移动到文件的第一行
G         #光标移动到文件的最后一行

5.vim同时编辑多个文件 
:sp+文件名
ctrl + w       #光标进入上边文件窗口
ctrl + w       #光标进入下边文件窗口

6.vim的注意事项
(1)编辑文件时 当文件名称存在,那么进入文件直接编辑,当文件名称不存在,vim会在退出保存时自动建立文件,当vim命令后没有跟任何文件名称时,时无法实现":wq"退出保存的那么可以用
":wq filename"2)当vim异常退出时,系统会建立.file.swp这个缓存文件,这个文件中保存着没有":wq"动作保存的信息,当用vim对文件做编辑的时候会提示
O    #只读打开(小写字母也可)                                       
E    #直接忽略.file.swap信息编辑文件
R    #利用.file.swap进行为保存数据恢复     
D    #删除.file.swap
Q    #退出                                              
A    #终止

3.关于路径的命令

(1)路径分为
绝对路径:
    文件所在系统的真实位置,在系统的任何位置都可以使用通常以"/"开头

相对路径
    相对当前所在系统位置的一个文件名称的简写,他通常会自动添加"pwd"的值在名称前

(2).相关命令
pwd   #显示当前所在系统位置的路径(print work directory)
touch /mnt/file     #用绝对路径的方式在/mnt/中建立file文件
rm -fr /mnt/file    #用绝对路径的方式删除/mnt/中的file文件
mv hello /mnt       #把当前的hello文件移动到/mnt目录中
mv /mnt/hello .         #用绝对路径的表示方式把/mnt中的hello文件移动到当前位置

4.切换工作目录

cd 目录名称
cd ..       #进入当前目录的上层目录
cd -        #进入当前目录之前所在目录

这里写图片描述
这里写图片描述

5.通配符

(1).字符匹配    
*                 #匹配0到任意字符#匹配单个字符 

这里写图片描述

[[:alpha:]]       #匹配单个字母
[[:lower:]]       #匹配单个小写字母
[[:upper:]]       #匹配单个大写字母
[[:digit:]]       #匹配单个数字
[[:alnum:]]       #匹配单个数字或字母
[[:punct:]]       #匹配单个符号
[[:space:]]       #匹配单个空格
{}
{1..9}            #1-9
{a..f}            #a-f
{1,3,5}           #135
{a,c,e}           #ace
{1..3}{a..c}      #1a 2a 3a 2a 2b 2c 3a 3b 3c
[]
[a-C]             #a或者A或者b或者B或者c或者C
[a-c]             #a或者b或者c
[1-3]             #1或者2或者3
[145]             #1或者4或者5
[^abc]|[!abc]     #除了a并且除了b并且除了c

6.其他常用文本处理工具

head    ##显示指定文件的前多少行
head -n 1 /etc/passwd      #显示文件的第一行
head -n 3 /etc/passwd      #显示文件的前3行
head /etc/passwd           #默认显示文件的前10行

这里写图片描述
这里写图片描述

tail    ##显示指定文件的最后多少行
tail -n 1 /etc/passwd      #显示文件的最后以行
tail -n 2 /etc/passwd      #显示文件的最后2行
tail /etc/passwd           #显示文件的最后10行

这里写图片描述

less    ##分页浏览
less /etc/passwd
file    ##查看文件类型
file +文件名称

这里写图片描述

wc      ##统计文件的字数,字符数,字节数
wc -l  +文件名称    #统计行数           
wc -m  +文件名称    #统计字符数        
wc -c  +文件名称    #统计字节数          
wc -w  +文件名称    #统计字数

这里写图片描述
练习1:

在桌面上建立文件 westos 在桌面建立目录 linux 在/mnt下建文件 lee 复制/etc目录中的passwd
文件到/mnt目录中的usermessage文件中 复制/mnt中的所有文件到桌面的linux目录中
删除/mnt目录中的所有文件
重命名桌面上的linux目录为test文件

[root@localhost ~]# cd Desktop/
[root@localhost Desktop]# pwd
/root/Desktop
[root@localhost Desktop]# touch westos
[root@localhost Desktop]# mkdir linux
[root@localhost Desktop]# touch /mnt/lee
[root@localhost Desktop]# cp /etc/passwd /mnt/usermessage
[root@localhost Desktop]# cp /mnt/* /root/Desktop/linux
cp: omitting directory ‘/mnt/home’
[root@localhost Desktop]# cp -r /mnt/* /root/Desktop/linux
[root@localhost Desktop]# rm -rf /mnt/*
[root@localhost Desktop]# mv linux test

这里写图片描述
练习2:

(1)用一条命令建立 12 个文件 WESTOS_classX_linuxY(X 的数值范围为 1-2 , Y 的数值范围为1-6) 这些文件都包含在 root 用户桌面的 study 目录中

[root@localhost ~]# cd Desktop
[root@localhost Desktop]# pwd
/root/Desktop
[root@localhost Desktop]# mkdir study
[root@localhost Desktop]# cd study/
[root@localhost study]# ls
[root@localhost study]# touch WESTOS_class{1..2}_linux{1..6}
[root@localhost study]# ls
WESTOS_class1_linux1  WESTOS_class1_linux5  WESTOS_class2_linux3
WESTOS_class1_linux2  WESTOS_class1_linux6  WESTOS_class2_linux4
WESTOS_class1_linux3  WESTOS_class2_linux1  WESTOS_class2_linux5
WESTOS_class1_linux4  WESTOS_class2_linux2  WESTOS_class2_linux6

这里写图片描述

(2)用一条命令建立 8 个文件 redhat_versionX ( x 的范围为 1-8 ) redhat_virsionX
这些文件都包含在 /tmp 目录中的 VERSION 中

[root@localhost ~]# cd /tmp
[root@localhost tmp]# ls
ssh-6vqSzNsCij7E        systemd-private-FOTt9r  systemd-private-mOm4td
systemd-private-1cB5OT  systemd-private-hdxcfr  systemd-private-pJEs7Y
systemd-private-5HyWuU  systemd-private-Hm8qh8  systemd-private-TFEYR6
systemd-private-5X4zlR  systemd-private-imD9ym  systemd-private-uZ5uVl
systemd-private-CTa9F1  systemd-private-JhQgW9  systemd-private-VZl79x
systemd-private-DG6wGQ  systemd-private-KLrgSd  systemd-private-xQfkpI
systemd-private-djnqmO  systemd-private-lBk5qB  systemd-private-xUGBe7
[root@localhost tmp]# mkdir VERSION
[root@localhost tmp]# cd VERSION/
[root@localhost VERSION]# pwd
/tmp/VERSION
[root@localhost VERSION]# touch redhat_version{1..8}
[root@localhost VERSION]# ls
redhat_version1  redhat_version3  redhat_version5  redhat_version7
redhat_version2  redhat_version4  redhat_version6  redhat_version8

这里写图片描述

•管理刚才信建立的文件要求如下 用一条命令把 redhat_versionX 中的带有奇数的文件复制到桌面的 SINGLE 中

[root@localhost ~]# cd Desktop/
[root@localhost Desktop]# pwd
/root/Desktop
[root@localhost Desktop]# mkdir SINGLE
[root@localhost Desktop]# cd /tmp/VERSION/
[root@localhost VERSION]# ls
redhat_version1  redhat_version3  redhat_version5  redhat_version7
redhat_version2  redhat_version4  redhat_version6  redhat_version8
[root@localhost VERSION]# mv redhat_version{1,3,5,7} /root/Desktop/SINGLE[root@localhost VERSION]# ls
redhat_version2  redhat_version4  redhat_version6  redhat_version8
[root@localhost VERSION]# cd /root/Desktop/SINGLE
[root@localhost SINGLE]# ls
redhat_version1  redhat_version3  redhat_version5  redhat_version7

这里写图片描述

用一条命令把 redhat_versionX 中的带偶数数的文件复制到 /DOUBLE 中

[root@localhost ~]# mkdir /DOUBLE
[root@localhost ~]# cd /DOUBLE/
[root@localhost DOUBLE]# pwd
/DOUBLE
[root@localhost DOUBLE]# cd /tmp/VERSION/
[root@localhost VERSION]# ls
redhat_version2  redhat_version4  redhat_version6  redhat_version8
[root@localhost VERSION]# mv * /DOUBLE/
[root@localhost VERSION]# ls
[root@localhost VERSION]# cd /DOUBLE/
[root@localhost DOUBLE]# ls
redhat_version2  redhat_version4  redhat_version6  redhat_version8

这里写图片描述

用一条命令把 WESTOS_classX_linuxY 中 class1 的文件移动到当前用户桌面的 CLASS1 中
用一条命令把WESTOS_classX_linuxY 中 class2 的文件一动到当前用户桌面的 CLASS2 中

[root@localhost ~]# cd Desktop/
[root@localhost Desktop]# mkdir CLASS1
[root@localhost Desktop]# mkdir CLASS2
[root@localhost Desktop]# cd study/
[root@localhost study]# ls
WESTOS_class1_linux1  WESTOS_class1_linux5  WESTOS_class2_linux3
WESTOS_class1_linux2  WESTOS_class1_linux6  WESTOS_class2_linux4
WESTOS_class1_linux3  WESTOS_class2_linux1  WESTOS_class2_linux5
WESTOS_class1_linux4  WESTOS_class2_linux2  WESTOS_class2_linux6
[root@localhost study]# pwd
/root/Desktop/study
[root@localhost study]# mv WESTOS_class1_linux* ../CLASS1
[root@localhost study]# ls
WESTOS_class2_linux1  WESTOS_class2_linux3  WESTOS_class2_linux5
WESTOS_class2_linux2  WESTOS_class2_linux4  WESTOS_class2_linux6
[root@localhost study]# mv WESTOS_class2_linux* ../CLASS2
[root@localhost study]# ls
[root@localhost study]# cd ../CLASS1
[root@localhost CLASS1]# ls
WESTOS_class1_linux1  WESTOS_class1_linux3  WESTOS_class1_linux5
WESTOS_class1_linux2  WESTOS_class1_linux4  WESTOS_class1_linux6
[root@localhost CLASS1]# pwd
/root/Desktop/CLASS1
[root@localhost CLASS1]# cd ../CLASS2
[root@localhost CLASS2]# ls
WESTOS_class2_linux1  WESTOS_class2_linux3  WESTOS_class2_linux5
WESTOS_class2_linux2  WESTOS_class2_linux4  WESTOS_class2_linux6

这里写图片描述

•备份 /etc 目录中所有带有名字带有数字并且以 .conf 结尾的文件到桌面上的 confdir 中

[root@localhost ~]# cd Desktop
[root@localhost Desktop]# pwd
/root/Desktop
[root@localhost Desktop]# mkdir confdir
[root@localhost Desktop]# cp /etc/*[[:digit:]]*.conf  confdir
[root@localhost Desktop]# ls confdir/
e2fsck.conf  krb5.conf  mke2fs.conf  pbm2ppa.conf  pnm2ppa.conf

这里写图片描述

•删掉刚才建立或者备份的所有文件

[root@localhost Desktop]# ls
CLASS1  CLASS2  confdir  SINGLE  study  test  westos
[root@localhost Desktop]# rm -rf *
[root@localhost Desktop]# ls
[root@localhost Desktop]# rm -rf /tmp/VERSION/*
[root@localhost Desktop]# rm -rf /DOUBLE

这里写图片描述

猜你喜欢

转载自blog.csdn.net/lilygg/article/details/82388695