linux基础命令03
1.grep
grep:文本查找 -i:不区分大小写 -v:显示没有被匹配的行
-o:显示匹配的行 -E:正则表达式 -q:静默模式
#过滤
#查找hello
[root@clq ~]# grep 'hello' haha
hello world
hello world you
#不区分大小写查找
[root@clq ~]# grep -i 'hello' haha
hello world
hello world you
HELLO
HELLO
#显示被匹配的行
[root@clq ~]# grep -o 'hello world' haha
hello world
hello world
#正则表达式查找
[root@clq ~]# grep -E 'hell|HELL' haha
hello world
hello world you
HELLO
HELLO
#查找本内容以及后俩行
[root@clq ~]# grep -A 2 'hello' haha
hello world
hello world you
HELLO
HELLO
#查找本内容以及前一行
[root@clq ~]# grep -B 1 'HELL' haha
hello world you
HELLO
HELLO
2.find
语法:find / -name 文件
-iname:不区分大小写 - nouser:查找没有属主的文件
-nogroup:查找没有属组的文件
-mtime:修改时间
-fls :查找到的所有文件的长格式信息保存至指定文件中
-ok :对查找到的每个文件执行COMMAND,每次操作都需要用
-exec :对查找到的每个文件执行COMMAND,操作不需要确认
#查找没有属主的文件
[root@clq ~]# ll
-rw-r--r--. 1 1314 1314 40 Apr 12 09:09 haha
[root@clq ~]# find / -nouser
find: ‘/proc/107625’: No such file or directory
find: ‘/proc/107646’: No such file or directory
find: ‘/proc/107647/task/107647/fd/7’: No such file or directory
find: ‘/proc/107647/task/107647/fdinfo/7’: No such file or directory
find: ‘/proc/107647/fd/8’: No such file or directory
find: ‘/proc/107647/fdinfo/8’: No such file or directory
/root/haha
#修改时间
[root@clq ~]# find /mnt/ -mtime +2
/mnt/
/mnt/abcd
/mnt/abcd/c
/mnt/abcd/c/hehe
/mnt/abcd/a
/mnt/abcd/b
/mnt/abcd/b/haha
/mnt/abcd/d
/mnt/abc
#组合使用
[root@clq ~]# find / -type d -size -5 -iname oppo #f:文件 d:目录
/root/oppo
#
[root@clq mnt]# find -type d -fls oppo
[root@clq mnt]# cat oppo
50791665 0 drwxr-xr-x 3 root root 41 Apr 12 10:08 .
51180968 0 drwxr-xr-x 6 root root 42 Mar 24 19:25 ./abcd
2807771 0 drwxr-xr-x 3 root root 18 Mar 24 19:21 ./abcd/c
17586741 0 drwxr-xr-x 2 root root 6 Mar 24 19:21 ./abcd/c/hehe
34275514 0 drwxr-xr-x 2 root root 6 Mar 24 19:21 ./abcd/a
51391554 0 drwxr-xr-x 3 root root 18 Mar 24 19:21 ./abcd/b
2807781 0 drwxr-xr-x 2 root root 6 Mar 24 19:21 ./abcd/b/haha
17586744 0 drwxr-xr-x 2 root root 6 Mar 24 19:21 ./abcd/d
[root@clq mnt]# ls
file1 file2 file3 file4 file5 oppo
[root@clq mnt]# find ./ -name file3 -exec ls -l {} \;
-rw-r--r--. 1 root root 0 Apr 12 11:50 ./file3
相对安全一点
-rw-r--r--. 1 root root 0 Apr 12 11:50 ./file3
[root@clq mnt]# find ./ -name file3 -ok ls -lh {} \;
< ls ... ./file3 > ? y
-rw-r--r--. 1 root root 0 Apr 12 11:50 ./file3
3.文件层级系统(FHS)
/(根目录)
/bin(普通执行命令)
/sbin(管理执行命令)
/boot(启动)(相关的文件)
/dev(驱动)
/etc(配置)
/home(普通用户)
/root(管理员家目录超级管理员)
run(运行)
tmp(临时目录)
/usr(应用程序)/local #第三方软件安装目录
/var(日志,邮件mail)
media(媒体/挂u盘)
/proc /sys (伪文件)
srv(服务相关)
/lib /静态库/动态库
4.重定向/管道
>:输出 <:输入
>>:追加输出 <<追加输入
2>:错误输出
2>>:错误追加输出
&>覆盖重定向标准输出或错误输出至同一个文件
&>>追加重定向标准输出或错误输出至同一个文件
0(键盘)1(显示器)2(显示器)3(文件)
标准输入--stdin
[root@clq ~]# echo '123456' |passwd --stdin oppo
Changing password for user oppo.
passwd: all authentication tokens updated successfully.
#管道(piping):可以将多个命令组合起来,一次性完成复杂的任务
#查看root的第一条信息
#grep管道组合使用
[root@clq mnt]# cat /etc/passwd |grep root|head -1
root:x:0:0:root:/root:/bin/bash
[root@clq mnt]# cat /etc/passwd |grep root |cut -d: -f1
root
operator
#tee管道:三通管道交给其它程序处理保存副本
[root@clq mnt]# touch 999 #创建一个文件夹999
[root@clq mnt]# cat /etc/passwd |tee 999 |head -2
###查看/etc/passwd的开头第2行并且保存到副本999
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@clq mnt]# cat 999
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
,,,
#xargs(参数传递)(特殊cp和rm)
[root@clq mnt]# ls #文件
file1 file2 file3 file4 files.txt
[root@clq mnt]# cat files.txt
/mnt/file1 #文件路径
/mnt/file4
#删除文件路径就能删除指定文件
#通过文件路径利用管道命令删除不了!!
[root@clq mnt]# cat files.txt |rm -rfv
[root@clq mnt]# ls
file1 file2 file3 file4 files.txt
俩个方法删除:
[root@clq mnt]# cat files.txt |rm -rfv * #只能删全部
[root@clq mnt]# cat files.txt |xargs rm -rfv #删指定文件
5.文本编辑vi/vim
安装vim
[root@clq ~]# dnf -y install vim
模式:
模式:1.insert(插入)<esc+i,l,a,o
2.(末行模式命令模式)<esc+shift+:
功能操作:
进入文本第5行
[root@clq ~]# vim +5 file01
hjkl:左下上右
末行模式关闭文件:q!:不保存退出
wq!:强行保存并退出
x:强行保存并退出
X:给文本加一个密码
模行模式下:yy复制
p:粘贴
dd:删除 ndd:删n行
D:删除光标的内容
s:删一个指定的字
v:可视化
u:恢复
set nu:显示列表数字
set nonu:取消列表数字
/:查看
gg:页首 GG:页尾 n(gg GG)移动n个字符
h(左)j(下)k(上)l(右)n(hjkl)移动n个字符
:w 保存一样的别名文本
:n s///g ^删掉
vimdiff:查看文本差异
[root@clq ~]# vimdiff file01 file02