ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

【linux】循序渐进学运维-基础命令篇-查找类命令

2021-04-13 23:51:12  阅读:162  来源: 互联网

标签:学运 文件 zmgaosh -- 命令 查找 linux txt root


本文为循序渐进学运维系列的基础命令篇,文章已收录gitee. 在Gitee上,点击直达 有运维技术的全部技术栈和文档,免费学习。 打造运维行业最全的知识体系,欢迎star

点赞再看,注意收藏,习惯养成,决胜未来


文章目录


Linux操作系统中关于查找的命令有很多,今天我们着重讨论以下几个命令的使用:


which 查看可执行文件的位置
whereis 查看可执行文件的位置及相关文件
locate 配合数据库缓存,快速查看文件位置
grep 过滤匹配,它是一个文件搜索工具
find 查找相关文件

1. which 和whereis 的用法:

关于which和whereis的用法是最简单的,主要是用来查找可执行文件位置及相关文件-我们来看下案例:

案例:

[root@zmgaosh ~]# which cd/usr/bin/cd[root@zmgaosh ~]# whereis cdcd: /usr/bin/cd /usr/share/man/man1/cd.1.gz[root@zmgaosh ~]# whereis lsls: /usr/bin/ls /usr/share/man/man1/ls.1.gz

2. locate的用法

1) locate介绍

locate命令和find -name 功能差不多,是它的另外一种写法,但是这个要比find搜索快的多,因为find命令查找的是具体目录文件,而locate搜索的是一个数据库/var/lib/mlocate/mlocate.db,这个数据库中存有本地所有的文件信息;

这个数据库是Linux自动创建并每天自动更新维护。

/usr/bin/updatedb   主要用来更新数据库,通过crontab自动完成的

/usr/bin/locate         查询文件位置

/etc/updatedb.conf   updatedb的配置文件

/var/lib/mlocate/mlocate.db  存放文件信息的文件

2)locate 选项:

-b, --basename         match only the base name of path names
  -c, --count            只输出找到的数量
  -d, --database DBPATH  使用DBPATH指定的数据库,而不是默认数据库 /var/lib/mlocate/mlocate.db
  -e, --existing         only print entries for currently existing files
  -L, --follow           follow trailing symbolic links when checking file existence (default)
  -h, --help             显示帮助
  -i, --ignore-case      忽略大小写
  -l, --limit, -n LIMIT  limit output (or counting) to LIMIT entries
  -m, --mmap             ignored, for backward compatibility
  -P, --nofollow, -H     don't follow trailing symbolic links when checking file existence
  -0, --null             separate entries with NUL on output
  -S, --statistics       don't search for entries, print statistics about eachused database
  -q, --quiet            安静模式,不会显示任何错误讯息
  -r, --regexp REGEXP    使用基本正则表达式
      --regex            使用扩展正则表达式
  -s, --stdio            ignored, for backward compatibility
  -V, --version          显示版本信息
  -w, --wholename        match whole path name (default)

3) locate 的安装

[root@zmgaosh ~]# yum install mlocate && updatedb[root@zmgaosh ~]# ls         查看本地目录,有zmedu.txt文件a.sh  a.txt  file  test  zmedu.txt[root@zmgaosh ~]# locate zmedu.txt    #查找zmedu.txt文件/root/zmedu.txt[root@zmgaosh ~]# touch zmeduv2.txt   #创建zmeduv2.txt[root@zmgaosh ~]# locate zmeduv2.txt  #查找v2但无法找到[root@zmgaosh ~]# updatedb            #更新[root@zmgaosh ~]# locate zmeduv2.txt  #可以看到已经找到了/root/zmeduv2.txt[root@zmgaosh ~]#

由上面的案例充分说明, 新增的文件无法locate,只有使用update后才能够快速查到。

我们来查看下updatedb的配置文件

updatedb的配置文件

[root@zmgaosh ~]# cat /etc/updatedb.conf PRUNE_BIND_MOUNTS = "yes"   #是否限制搜索PRUNEFS = "9p afs anon_inodefs auto autofs bdev binfmt_misc cgroup cifs coda configfs cpuset debugfs devpts ecryptfs exofs fuse fuse.sshfs fusectl gfs gfs2 gpfs hugetlbfs inotifyfs iso9660 jffs2 lustre mqueue ncpfs nfs nfs4 nfsd pipefs proc ramfs rootfs rpc_pipefs securityfs selinuxfs sfs sockfs sysfs tmpfs ubifs udf usbfs fuse.glusterfs ceph fuse.ceph"    #对哪些后缀的文件排除检索PRUNENAMES = ".git .hg .svn"PRUNEPATHS = "/afs /media /mnt /net /sfs /tmp /udev /var/cache/ccache /var/lib/yum/yumdb /var/spool/cups /var/spool/squid /var/tmp /var/lib/ceph"  #检索的路径,即列出的路径下的文件和子文件夹均跳过不进行检索

grep的使用

1)grep的作用

过滤,它能够使用正则表达式来搜索文本,并把结果打印出来

2)常用参数:

-v  取反
-i  忽略大小写
^#	以#开头#$	以#结尾^$	空行
-n 对过滤的内容加上行号|  或者的意思

3)举例:

[root@zmgaosh ~]# ps -aux |grep sshd |grep -v greproot      4700  0.2  0.1 157640  6348 ?        Ss   13:55   0:51 sshd: root@pts/0,pts/1
root      9315  0.0  0.1 112920  4312 ?        Ss   6月17   0:00 /usr/sbin/sshd -D

这里的 grep -v grep ,就是指的不显示grep的查询语句。

如果不用grep -v grep的结果如下:

[root@zmgaosh ~]# ps -aux |grep sshd root      4700  0.2  0.1 157640  6348 ?        Ss   13:55   0:51 sshd: root@pts/0,pts/1
root      9146  0.0  0.0 112732   968 pts/0    S+   21:03   0:00 grep --color=auto sshd
root      9315  0.0  0.1 112920  4312 ?        Ss   6月17   0:00 /usr/sbin/sshd -D

一般情况下运维排查目前是否有后门账号的时候可以使用grep

[root@zmgaosh ~]# grep /bin/bash /etc/passwdroot:x:0:0:root:/root:/bin/bash

我们要查询目前的nologin用户一共有多少的时候

[root@zmgaosh ~]# grep "nologin" /etc/passwd |wc -l

find命令的使用(重点 )

1)格式

格式:find pathname -options [-print]
命令字 路径名称 选项 输出

2) find命令常用参数

find命令选项:
-name 	按照文件名查找文件。  “名称”
-perm 	按照文件权限来查找文件。
-user 	按照文件属主来查找文件
-group 	按照文件所属的组来查找文件
-mtime  -n  / +n 	按照文件的更改时间来查找文件,
			 - n	表示文件更改时间距现在n天以内
			 + n	表示文件更改时间距现在n天以前
-type 	查找某一类型的文件
			b - 块设备文件
			d - 目录
			c - 字符设备文件
			p - 管道文件
			l- 符号链接文件
			f - 普通文件
-size n  查找符合指定的文件大小的文件
-exec	   对匹配的文件执行该参数所给出的其他linux命令, 相应命令的形式为' 命令 {} \;,注意{ }和 \;之间的空格,{}代表查到的内容

3)范例

查找当前目录下所有的txt文件
[root@zmgaosh ~]# find . -name "*.txt"./zmedu.txt
./a.txt
./zmeduv2.txt

按照更改时间或访问时间等查找文件

mtime: 文件最后一次修改的时间
atime: 最后一次访问时间
ctime: 文件的最后一次变化时间,也就是修改时间

举例: 查找root目录下更改时间在1天之内的且被修改过的文件

[root@zmgaosh ~]# find /root/ -mtime -1/root/
/root/.viminfo
/root/a.sh
/root/zmedu.txt
/root/.mysql_history
/root/a.txt
/root/test
/root/.bash_history
/root/zmeduv2.txt
/root/file[root@zmgaosh ~]#

查找并执行相应的命令

使用exec选项

[root@zmgaosh ~]# touch {1,2,3}.bak   #创建三个文件[root@zmgaosh ~]# ls1.bak  3.bak  a.txt  test       zmeduv2.txt
2.bak  a.sh   file   zmedu.txt[root@zmgaosh ~]# find . -name "*.bak" -exec rm {} \;  #查找以.bak结尾的文件并删除[root@zmgaosh ~]# lsa.sh  a.txt  file  test  zmedu.txt  zmeduv2.txt

查找一个目录下的制定文件并拷贝到另外一个目录
[root@zmgaosh ~]# lsa.sh  a.txt  file  test  zmedu.txt  zmeduv2.txt[root@zmgaosh ~]# find . -name "*.txt" -exec cp {} /opt \;[root@zmgaosh ~]# ls /opt/a.txt  zmedu.txt  zmeduv2.txt

查找多个类型文件
比较符的使用:
	-a  and 并且
	-o  or  或者
	+       超过
    -       低于

查找/etc/下文件大小为大于20K 小于50K的文件,并统计个数
[root@zmgaosh ~]# find /etc -size +20k -a -size -50k |wc -l17

按权限查找 -perm

查找/etc权限为755的文件或者目录
[root@zmgaosh ~]# find /etc -perm 755  |wc -l232

总结:

查找命令中,find和grep是用的最多的命令,在面试中也经常出现

后面我会单独写一篇文章,主要笔试题经常出现的find和grep试题

标签:学运,文件,zmgaosh,--,命令,查找,linux,txt,root
来源: https://blog.51cto.com/xinsz08/2704530

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有