Linux笔记之浅析linux文件查找——find、grep命令

Linux文件查找

1、grep: 搜索内容

用法一:grep ‘目标字符’ 文件名称

[root@host ~]# grep 'root' /etc/passwd  #从/etc/passwd文件中搜索root字段
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

用法二:命令 | grep ‘目标字符’

[root@host ~]# ps aux	#显示当前所有进程的状态(process status)
[root@host ~]# ps aux | grep 'nginx' #查看nginx进程
root        944  0.0  0.1  46228  1204 ?        Ss   09:15   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx       946  0.0  0.1  46612  1948 ?        S    09:15   0:00 nginx: worker process
root       1255  0.0  0.0 112824   988 pts/0    R+   09:21   0:00 grep --color=auto nginx

用法三:选项-v(invert-match)取反操作

[root@host ~]# ps aux | grep 'nginx' | grep -v 'grep' #剔除grep进程
root        944  0.0  0.1  46228  1204 ?        Ss   09:15   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx       946  0.0  0.1  46612  1948 ?        S    09:15   0:00 nginx: worker process

2、which: 查找命令位置

语法:which 命令

[root@host ~]# which cd
/usr/bin/cd
[root@host ~]# which ls
alias ls='ls --color=auto'	#alias命令用来设置指令的别名
        /usr/bin/ls
[root@host ~]# which which
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
        /usr/bin/alias
        /usr/bin/which

3、find: 查找文件和目录

语法:find 路径 (-!) 条件 跟条件相关的操作符 [-exec 动作]
注意:省略路径时默认查找当前路径。

-! 表示取反操作,查找不符合条件的所有文件和目录

条件: 按名称查找、按文件大小查找。

3.1 按文件名 -name

[root@host ~]# find / -name “file2” #从根目录下查找以file2命令的所有文件和目录
/root/file2
/var/tmp/file2
[root@host ~]# find /etc -name "passwd"	#查找以passwd命名的文件和目录
/etc/passwd
/etc/pam.d/passwd
[root@host ~]# find /var -iname "Abc" # -i:忽略大小写

搭配通配符*使用。

[root@host ~]# find /etc -name "*.txt" #从/etc目录下查找所有以.txt结尾的文件和目录

3.2 按文件大小 -size

可以使用选项将多个条件连接起来。

选项:-a --and,-o --or。

[root@host ~]# find /etc -size +5M		#大于5M
[root@host ~]# find /etc -size 5M		#等于5M
[root@host ~]# find /etc -size -5M      #小于5M
[root@host ~]# find / -size +3M -a -size -5M  #大于3M并且小于5M
[root@host ~]# find / -size -1M -o -size +80M #小于1M或者大于80M
[root@host ~]# find / -size -3M -a -name "*.txt" #小于3M而且后缀是.txt

3.3 取反操作 -!

[root@host ~]# find / -name 'test5' #从根目录查找以test5命名的所有文件和目录
[root@host ~]# find / -! -name "test5" # 查找不以test5命名的所有文件和目录

3.4 查找后的操作命令

语法:-exec 命令 {} \;

{} 代指前面查找到的所有文件,位置视具体命令而定。

\; 命令结束符,无实际意义。

#将查找的所有以ifcfg开头的文件复制到tmp目录下
[root@host ~]# find /etc -name "ifcfg*" -exec cp {} /tmp \;
[root@host ~]# touch /home/test{1..20}.txt	#创建文件test1-20
[root@host ~]# find /home/ -name test* -exec rm -rf {} \;	#查找删除文件test1-20

4、grep和find的异同

grep和find的功能都是查找,不过它们的查找精度和查找对象并不相同。

1、查找精度:find是精确查找,在按名称查找时仅查找完全匹配该名称的文件和目录;

​ grep是模糊查找,除了名称完全匹配的,还会查找包含目标字符的内容。

2、查找对象:find的查找对象是文件和目录;

​ grep的查找对象除了基本的文件内容,还有任意命令输出的内容。

[root@host oyy]# find ./ -name "yy*"
./yy.txt
./dir/yy.txt
./yy
[root@host oyy]# ll | grep "yy"
-rw-r--r--. 1 root root  4 1211 10:10 oyy.txt
drwxr-xr-x. 2 root root  6 1211 11:58 yy
-rw-r--r--. 1 root root 20 125 20:07 yy.txt
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值