您的位置:首页 > 运维架构 > Linux

linux-vim编辑器,文件查找,locate,find,xargs

2017-02-19 22:33 423 查看
vim编辑器

文本编辑器,字处理器
ASCII

nano, sed

vi: Visual Interface
vim: VI iMproved

全屏编辑器,模式化编辑器

vim模式:
编辑模式(命令模式)
输入模式
末行模式

模式转换:
编辑-->输入:
i: 在当前光标所在字符的前面,转为输入模式;
a: 在当前光标所在字符的后面,转为输入模式;
o: 在当前光标所在行的下方,新建一行,并转为输入模式;

I:在当前光标所在行的行首,转换为输入模式
A:在当前光标所在行的行尾,转换为输入模式
O:在当前光标所在行的上方,新建一行,并转为输入模式;

输入-->编辑:
ESC

编辑-->末行:


末行-->编辑:
ESC, ESC

一、打开文件
# vim /path/to/somefile
vim +# :打开文件,并定位于第#行
vim +:打开文件,定位至最后一行
vim +/PATTERN : 打开文件,定位至第一次被PATTERN匹配到的行的行首

默认处于编辑模式

二、关闭文件
1、末行模式关闭文件
:q 退出
:wq 保存并退出
:q! 不保存并退出
:w 保存
:w! 强行保存
:wq --> :x
2、编辑模式下退出
ZZ: 保存并退出

三、移动光标(编辑模式)
1、逐字符移动:
h: 左
l: 右
j: 下
k: 上
#h: 移动#个字符;

2、以单词为单位移动
w: 移至下一个单词的词首
e: 跳至当前或下一个单词的词尾
b: 跳至当前或前一个单词的词首

#w:

3、行内跳转:
0: 绝对行首
^: 行首的第一个非空白字符
$: 绝对行尾

4、行间跳转
#G:跳转至第#行;
G:最后一行

末行模式下,直接给出行号即可

四、翻屏
Ctrl+f: 向下翻一屏
Ctrl+b: 向上翻一屏

Ctrl+d: 向下翻半屏
Ctrl+u: 向上翻半屏

五、删除单个字符
x: 删除光标所在处的单个字符
#x: 删除光标所在处及向后的共#个字符

六、删除命令: d
d命令跟跳转命令组合使用;
#dw, #de, #db

dd: 删除当前光标所在行
#dd: 删除包括当前光标所在行在内的#行;

末行模式下:
StartADD,EndADDd
.: 表示当前行
$: 最后一行
+#: 向下的#行

七、粘贴命令 p
p: 如果删除或复制为整行内容,则粘贴至光标所在行的下方,如果复制或删除的内容为非整行,则粘贴至光标所在字符的后面;
P: 如果删除或复制为整行内容,则粘贴至光标所在行的上方,如果复制或删除的内容为非整行,则粘贴至光标所在字符的前面;

八、复制命令 y
用法同d命令

九、修改:先删除内容,再转换为输入模式
c: 用法同d命令

十、替换:r
R: 替换模式
常用于单个字符替换

十一、撤消编辑操作 u
u:撤消前一次的编辑操作
连续u命令可撤消此前的n次编辑操作
#u: 直接撤消最近#次编辑操作

撤消最近一次撤消操作:Ctrl+r

十二、重复前一次编辑操作
.

十三、可视化模式
v: 按字符选取
V:按矩形选取

十四、查找
/PATTERN
?PATTERN
n
N

十五、查找并替换
在末行模式下使用s命令
ADDR1,ADDR2s@PATTERN@string@gi
1,$
%:表示全文

练习:将/etc/yum.repos.d/server.repo文件中的ftp://instructor.example.com/pub替换为http://172.16.0.1/yum

%s/ftp:\/\/instructor\.example\.com\/pub/http:\/\/172.16.0.1\/yum/g
%s@ftp://instructor\.example\.com/pub@http://172.16.0.1/yum@g

文件内容如下:
# repos on instructor for classroom use

# Main rhel5 server
[base]
name=Instructor Server Repository
baseurl=ftp://172.16.0.1/pub/Server
gpgcheck=0

# This one is needed for xen packages
[VT]
name=Instructor VT Repository
baseurl=ftp://172.16.0.1/pub/VT
gpgcheck=0

# This one is needed for clustering packages
[Cluster]
name=Instructor Cluster Repository
baseurl=ftp://172.16.0.1/pub/Cluster
gpgcheck=0

# This one is needed for cluster storage (GFS, iSCSI target, etc...) packages
[ClusterStorage]
name=Instructor ClusterStorage Repository
baseurl=ftp://172.16.0.1/pub/ClusterStorage
gpgcheck=0

十六、使用vim编辑多个文件
vim FILE1 FILE2 FILE3
:next 切换至下一个文件
:prev 切换至前一个文件
:last 切换至最后一个文件
:first 切换至第一个文件

退出
:qa 全部退出

十七、分屏显示一个文件
Ctrl+w, s: 水平拆分窗口
Ctrl+w, v: 垂直拆分窗口

在窗口间切换光标:
Ctrl+w, ARROW

:qa 关闭所有窗口

十八、分窗口编辑多个文件
vim -o : 水平分割显示
vim -O : 垂直分割显示

十九、将当前文件中部分内容另存为另外一个文件
末行模式下使用w命令
:w
:ADDR1,ADDR2w /path/to/somewhere

二十、将另外一个文件的内容填充在当前文件中
:r /path/to/somefile

二十一、跟shell交互
:! COMMAND

二十二、高级话题
1、显示或取消显示行号
:set number
:set nu

:set nonu

2、显示忽略或区分字符大小写
:set ignorecase
:set ic

:set noic

3、设定自动缩进
:set autoindent
:set ai
:set noai

4、查找到的文本高亮显示或取消
:set hlsearch
:set nohlsearch

5、语法高亮
:syntax on
:syntax off

二十三、配置文件
/etc/vimrc
~/.vimrc

vimtutor

grep, egrep, fgrep: 文本查找

find/locate:文件查找
locate:
非实时,模糊匹配,查找是根据全系统文件数据库进行的;
# updatedb, 手动生成文件数据库
速度快

[root@localhost ~]# locate passwd
/etc/passwd
/etc/passwd-
/etc/passwd.OLD
/etc/pam.d/passwd
/etc/security/opasswd
/lib64/security/pam_passwdqc.so
/lib64/security/pam_unix_passwd.so
/var/lib/yum/yumdb/p/38b4293a7cb64447183291cbf4f0bc01800a2bf5-passwd-0.77-4.el6_2.2-x86_64
/var/lib/yum/yumdb/p/e5bf65957bcc575c67c15c0a4a2ca7711b409842-pam_passwdqc-1.0.5-6.el6-x86_64
/var/lib/yum/yumdb/p/38b4293a7cb64447183291cbf4f0bc01800a2bf5-passwd-0.77-4.el6_2.2-x86_64/checksum_data

find:
实时
精确
支持众多查找标准
遍历指定目录中的所有文件完成查找,速度慢;

find 查找路径 查找标准 查找到以后的处理运作
查找路径:默认为当前目录
查找标准:默认为指定路径下的所有文件
处理运作:默认为显示

匹配标准:
(文件名)
-name 'FILENAME':对文件名作精确匹配
文件名通配:
*:任意长度的任意字符
?
[]
-iname 'FILENAME': 文件名匹配时不区分大小写
-regex PATTERN:基于正则表达式进行文件名匹配

(文件属主/属组)
-user USERNAME: 根据属主查找
[root@localhost ~]# find /tmp/ -user gdm
/tmp/orbit-gdm
/tmp/orbit-gdm/linc-811-0-43a1e17eeece8
/tmp/orbit-gdm/linc-814-0-ffceb4777d
[root@localhost ~]#

-group GROUPNAME: 根据属组查找

-uid UID: 根据UID查找
-gid GID: 根据GID查找

-nouser:查找没有属主的文件,经常进行检查
-nogroup: 查找没有属组的文件

-type (文件类型)
f:普通文件
d:目录
c:字符设备
b:块设备
l:链接文件,符号链接
p:管道设备
s:套接字文件

-size [+|-] (文件大小)
#k
#M
#G
[root@localhost ~]# find /tmp/ -size +1k
/tmp/
/tmp/keyring-HXk4gf
/tmp/pulse-Y030iPht7UnR
/tmp/.ICE-unix
/tmp/keyring-C6bpQR
/tmp/.X11-unix
/tmp/keyring-V1nCMd
/tmp/orbit-gdm
/tmp/virtual-rhel.T2NxT0
/tmp/.esd-500

组合条件:
-a
-o
-not

/tmp目录,不是目录,并且还不能套接字类型的文件
/tmp/test目录下,属主不是user1,也不是user2的文件;
[root@localhost test]# find ./ -not -user user1
./
./b
./d
./c
[root@localhost test]#
[root@localhost test]# find ./ -not -user user2
./
./d
./a
./c
[root@localhost test]#
[root@localhost test]# find ./ -not -user user2 -a -not -user user1
./
./d
./c
[root@localhost test]#
[root@localhost test]# find ./ -not \( -user user2 -o -user user1 \)
./
./d
./c
[root@localhost test]#

(/修改/改变/访问 时间)
-mtime
-ctime
-atime
[+|-]#
-mmin
-cmin
-amin
[+|-]#
[root@localhost test]# find ./ -amin +5
./
./b
./d
./a
./c
[root@localhost test]# touch -a a
[root@localhost test]# find ./ -amin -5
./a
[root@localhost test]#

(文件权限)
-perm MODE:精确匹配
/MODE: 任意一位匹配即满足条件
-MODE: 文件权限能完全包含此MODE时才符合条件

-644
644: rw-r--r--
755: rwxr-xr-x
750: rwxr-x---
find ./ -perl -001
[root@localhost test]# find ./ -perm 644
./b
./d
./a
./c
[root@localhost test]# ll
total 0
-rw-r--r--. 1 user1 root 0 Nov 19 22:46 a
-rw-r--r--. 1 user2 root 0 Nov 19 22:46 b
-rw-r--r--. 1 root root 0 Nov 19 22:46 c
-rw-r--r--. 1 root root 0 Nov 19 22:46 d
[root@localhost test]#
[root@localhost test]# chmod 006 b
[root@localhost test]# find ./ -perm /640
./
./d
./a
./c
[root@localhost test]#
[root@localhost test]# touch e
[root@localhost test]# chmod 750 e
[root@localhost test]# ll
total 0
-rw-r--r--. 1 user1 root 0 Nov 19 22:46 a
-------rw-. 1 user2 root 0 Nov 19 22:46 b
-rw-r--r--. 1 root root 0 Nov 19 22:46 c
-rw-r--r--. 1 root root 0 Nov 19 22:46 d
-rwxr-x---. 1 root root 0 Nov 19 23:09 e
[root@localhost test]# find ./ -perm -644
./
./d
./a
./c
[root@localhost test]#

运作:
-print: 显示
-ls:类似ls -l的形式显示每一个文件的详细
-ok COMMAND {} \; 每一次操作都需要用户确认
-exec COMMAND {} \;
{}引用匹配到的内容

[root@localhost test]# find ./ -type f -ok chmod +x {} \;
< chmod ... ./b > ? y
< chmod ... ./e > ? y
< chmod ... ./d > ? y
< chmod ... ./a > ? y
< chmod ... ./c > ? y
[root@localhost test]# ll
total 0
-rwxr-xr-x. 1 user1 root 0 Nov 19 22:46 a
---x--xrwx. 1 user2 root 0 Nov 19 22:46 b
-rwxr-xr-x. 1 root root 0 Nov 19 22:46 c
-rwxr-xr-x. 1 root root 0 Nov 19 22:46 d
-rwxr-x--x. 1 root root 0 Nov 19 23:09 e
[root@localhost test]#
[root@localhost test]# find ./ -perm -020 -exec mv {} {}.new \;
[root@localhost test]# ll
total 0
-rwxr-xr-x. 1 user1 root 0 Nov 19 22:46 a
-rw-rw-rw-. 1 user2 root 0 Nov 19 22:46 b.new
-rwxr-xr-x. 1 root root 0 Nov 19 22:46 c
-rw-rw-r--. 1 root root 0 Nov 19 22:46 d.new
-rwxr-x--x. 1 root root 0 Nov 19 23:09 e
[root@localhost test]#

1、查找/var目录下属主为root并且属组为mail的所有文件;
[root@localhost test]# find /var -user root -a -group mail
/var/spool/mail
[root@localhost test]#

2、查找/usr目录下不属于root,bin,或student的文件;
find /usr -not -user root -a -not -user bin -a -not -user student
find /usr -not \( -user root -o -user bin -o -user student \)

3、查找/etc目录下最近一周内内容修改过且不属于root及student用户的文件;
find /etc -mtime -7 -not \ ( -user root -o -user student \)
find /etc -mtime -7 -not -user root -a -not -user student

4、查找当前系统上没有属主或属组且最近1天内曾被访问过的文件,并将其属主属组均修改为root;
find / \( -nouser -o -nogroup \) -a -atime -1 -exec chown root:root {} \;
[root@localhost test]# find / -nouser -o -nogroup -a -atime -1 -exec chown root {} \;
find: `/proc/6159/task/6159/fd/5': No such file or directory
find: `/proc/6159/task/6159/fd/5': No such file or directory
find: `/proc/6159/task/6159/fdinfo/5': No such file or directory
find: `/proc/6159/task/6159/fdinfo/5': No such file or directory
find: `/proc/6159/fd/5': No such file or directory
find: `/proc/6159/fd/5': No such file or directory
find: `/proc/6159/fdinfo/5': No such file or directory
find: `/proc/6159/fdinfo/5': No such file or directory
[root@localhost test]#

5、查找/etc目录下大于1M的文件,并将其文件名写入/tmp/etc.largefiles文件中;
find /etc -size +1M >> /tmp/etc.largefiles

6、查找/etc目录下所有用户都没有写权限的文件,显示出其详细信息;
find /etc -not -perm /222 -ls
[root@localhost test]# find /etc -size +1M
/etc/selinux/targeted/policy/policy.24
/etc/selinux/targeted/modules/active/policy.kern
/etc/gconf/gconf.xml.defaults/%gconf-tree.xml
[root@localhost test]# find /etc -size +1M >> /tmp/etc.largefiles
[root@localhost test]# cat /tmp/etc.largefiles
/etc/selinux/targeted/policy/policy.24
/etc/selinux/targeted/modules/active/policy.kern
/etc/gconf/gconf.xml.defaults/%gconf-tree.xml
[root@localhost test]#

练习
[root@localhost ~]# bash adduser.sh --del
delete user1 finished
delete user2 finished
delete user3 finished
delete user4 finished
delete user5 finished
delete user6 finished
delete user7 finished
delete user8 finished
delete user9 finished
delete user10 finished
[root@localhost ~]# cat adduser.sh
#!/bin/bash

if [ $# -eq 0 ];then
echo "usage:adduser --add|--del"
exit 7
fi

if [ $1 == '--add' ]; then
for i in {1..10};do
if id user$i &> /dev/null; then
echo "user$i exist."
else
useradd user$i
echo "user$i" | passwd --stdin user$i &> /dev/null
fi
done
elif [ $1 == '--del' ]; then
for i in {1..10};do
if id user$i &> /dev/null; then
userdel -r user$i
echo "delete user$i finished"
else
echo "no user$i"
fi
done
else
echo "usage:adduser --add|--del"
exit 8
fi
[root@localhost ~]#

练习
[root@localhost ~]# bash +x adduser2.sh user1,user2,hell
add useruser1 finished
add useruser2 finished
add userhell finished

[root@localhost ~]# cat adduser2.sh
#!/bin/bash

for i in `echo $1 | sed 's/,/ /g'`;do
if id $i &> /dev/null;then
echo "$i exists"
else
useradd $i
echo $i | passwd --stdin $i &> /dev/null
echo "add $i finished"
fi
done
[root@localhost ~]#

[root@localhost ~]# bash adduser2.sh --add abc,bcd
add abc finished
add bcd finished
[root@localhost ~]# bash adduser2.sh --del abc,bcd
delete abc finished
delete bcd finished
[root@localhost ~]# cat adduser2.sh
#!/bin/bash

练习
if [ $1 == '--add' ];then
for i in `echo $2 | sed 's/,/ /g'`;do
if id $i &> /dev/null;then
echo "$i exists"
else
useradd $i
echo $i | passwd --stdin $i &> /dev/null
echo "add $i finished"
fi
done
elif [ $1 == '--del' ];then
for i in `echo $2 | sed 's/,/ /g'`;do
if id $i &> /dev/null;then
userdel -r $i
echo "delete $i finished"
else
echo "$i not exists"
fi
done
fi
[root@localhost ~]#

xargs:从标准输入接收并执行命令,比exec功能更强大

[root@localhost test]# find /etc -size +1M | xargs echo >> /tmp/etc.largefiles

[root@localhost test]# cat /tmp/etc.largefiles
/etc/selinux/targeted/policy/policy.24
/etc/selinux/targeted/modules/active/policy.kern
/etc/gconf/gconf.xml.defaults/%gconf-tree.xml
/etc/selinux/targeted/policy/policy.24
/etc/selinux/targeted/modules/active/policy.kern
/etc/gconf/gconf.xml.defaults/%gconf-tree.xml
{} /etc/selinux/targeted/policy/policy.24 /etc/selinux/targeted/modules/active/policy.kern /etc/gconf/gconf.xml.defaults/%gconf-tree.xml
/etc/selinux/targeted/policy/policy.24 /etc/selinux/targeted/modules/active/policy.kern /etc/gconf/gconf.xml.defaults/%gconf-tree.xml
[root@localhost test]#
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息