当前位置: 首页 > Linux

Linux文件搜索常用命令-详解

时间:2023-04-06 02:18:59 Linux

which-whereis-locate-grepfind用于查找文件的命令一般有以下命令:which查看可执行文件的位置whereis查看可执行文件的位置andrelatedfileslocate配合数据库Cache,快速查看文件所在位置grepfiltermatch,是一个文件搜索工具find,用于查找相关文件示例:[root@xuegod63~]#whichcd/usr/bin/cd[root@xuegod63~]#whereiscdcd:/usr/bin/cd/usr/share/man/man1/cd.1.gz/usr/share/man/man1p/cd.1p.gz[root@xuegod63~]#whereislsls:/usr/bin/ls/usr/share/man/man1/ls.1.gz/usr/share/man/man1p/ls.1p.glocatelocate命令类似于find-name功能,这是另一个写法,但是比find搜索快很多,因为find命令是搜索特定目录文件,而locate是搜索一个数据库/var/lib/mlocate/mlocate.db,里面存放了所有本地文件信息;这个数据库由Linux自动创建并自动更新维护。相关配置信息在/etc/updatedb.conf,定时任务信息在/etc/cron.daily/mlocate[root@xuegod63~]#yum-yinstallmlocate[root@xuegod63mnt]#touch/opt/雪神。txt[root@xuegod63mnt]#locatexuegod.txt#found没有找到[root@xuegod63mnt]#updatedb#如果搜索当天的文件,需要手动更新数据库updatedb[root@xuegod63mnt]#locatexuegodgrepFind使用功能:过滤,可以使用正则表达式搜索文本并打印出结果。参数:-v取反-i忽略大小写^#以##$开始以#^$结束空行-n添加行到过滤内容第l或意思[root@xuegod63~]#ps-aux|grepsshd|grep-vgreproot10890.00.21059964088?ss20:190:00/usr/sbin/sshd–D[root@xuegod63~]#cat/etc/passwd|grep^a#以a开始[root@xuegod63~]#grepbash$/etc/passwd#以bash结束[root@xuegod63~]#grep"nologin\|root"/etc/passwd|wc-l注意:\代表转义符[root@xuegod63~]#egrep"nologin|root"/etc/passwd|wc-l#查看包括nologin或root在内的行注:egrep是grep的加强版,find命令使用格式:findpathname-options[-print]命令字路径名option输出参数:pathname:pathname:路径find命令搜索到的目录,不输入则代表当前目录。例如,使用.表示当前目录,用/表示系统根目录。find命令选项:-name按文件名'name'查找文件-perm按文件权限查找文件。666777etc-user根据文件的所有者查找文件-group根据文件所属的组查找文件-mtime-n/+n根据文件被更改的时间查找文件-n表示文件更改时间在n天之内+n表示文件更改时间是从现在起n天前-type查找某种类型的文件b-块设备文件d-目录c-字符设备文件p-管道filel-符号链接filef-commonfile-sizen查找指定文件大小file-exec在匹配的文件上执行该参数给定的其他linux命令,对应的命令形式为'command{}\注意{}和\;之间的空格,{}表示查找内容的例子1:希望在根目录下找到1天之内修改过的文件[root@xuegod63~]#find/root/-mtime-1执行相应的commandforthesearchcontent-exec该选项参数后面可以自定义SHELL命令格式如下:例2:[root@xuegod63~]#touch{1,2,3}.back[root@xuegod63mnt]#寻找。-name"*.back"-execls-l{}\;示例3:[root@xuegod63~]#find.-name"*.back"-execmv{}/opt\;[root@xuegod63~]#ls/opt/1.back2.back3.backrhxuegod.txt例4:将找到的文件复制到指定目录目录[root@xuegod63mnt]#find/root-name"*.txt"-execcp{}/opt\;例5:xargs和find结合复制文件的命令-i意思是find传给xargs的结果被{}代替(懂了)[root@xuegod63~]#rm-rf/opt/*[root@xuegod63~]#找到。-名称“*.txt”|xargs-icp{}[root@xuegod63~]#ls/opt/例6:查找多类型文件比较字符的使用:-aandand-ooror+exceeds-below[root@xuegod63~]#toucha.pdfback.sh[root@xuegod63~]#find.-name"*.sh"-o-name"*.pdf"[root@xuegod63~]#find/etc-size+20k-a-size-50k|wc-l22[root@xuegod63~]#find/etc-size+20k|wc-l49例7:按权限查找:-perm[root@xuegod63~]#find/bin/-perm755#一个等于0755权限的文件或目录[root@xuegod63~]#find/bin/-perm-644#-表示至少644权限的文件或目录示例:查看系统中至少777权限的文件或目录创建部分测试文件:[root@xuegod63~]#mkdirccc[root@xuegod63~]#chmod777ccc[root@xuegod63~]#mkdirtest[root@xuegod63~]#chmod1777test[root@xuegod63~]#touchb.sh[root@xuegod63~]#chmod4777b.shsearch:[root@xuegod63~]]#find/root/-perm777[root@xuegod63~]#find/root/-perm1777[root@xuegod63~]#find/root/-perm4777示例:找出目录中权限不低于777的危险目录system[root@xuegod63~]#find/root/-perm-777#至少777权限查找777以下的危险文件[root@xuegod63~]#find/-typef-perm-777例八:搜索目录深度:-maxdepth1#只查找目录第一层的文件和目录,如:在bin目录下查找/权限等于644的文件[root@xuegod63~]#find/etc/-maxdepth1-perm644|更多[根@xuegod63~]#find/bin/-maxdepth1-perm755#/bin后面必须跟/[root@xuegod63~]#find/bin-maxdepth1-perm755#没有添加/不能满足我们的需求例9:查找全部系统中属于用户user1的文件,并将此文件放在/root/findresults目录下注意:/root/findresults需要提前创建root@xuegod63~]#mkdir/root/findresults[root@xuegod63~]#useradduser1[root@xuegod63~]#find/-useruser1-execcp-a{}/root/findresults/Parameter:-a#复制时,保留原文件所有属性,报错:find:'/proc/43475/task/43475/fd/6':找不到这样的文件或目录:'/proc/43475/task/43475/fdinfo/6':找不到这样的文件或目录:'/proc/43475/fd/6':没有这样的文件或目录查找:'/proc/43475/fdinfo/6':没有这样的文件或目录cp:不能使用目录“/home/user1”覆盖非目录“/root/findresults/user1”》在同一目录下,可以创建文件user1和文件夹user1吗?同目录下创建的文件名与目录名是否相同?No[root@xuegod63~]#touchabc[root@xuegod63~]#mkdirabcmkdir:Unabletocreatedirectory"abc":filealreadyexists解决方法:[root@xuegod63~]#find/-useruser1#find[root@xuegod63~]#ll/var/spool/mail/user1#查看这个文件[root@xuegod63~]#ll/home/user1发现/var/spool/mail/user1和/home/user1的名字相同.两者都要复制到/root/findresults/,而/var/spool/mail/user1先复制,所以不能复制/home/user1。[root@xuegod63~]#mv/var/spool/mail/user1[root@xuegod63~]#rm-rf/root/findresults/*/var/spool/mail/user1.mail[root@xuegod63~]#find/-usermk-execcp-a{}/root/findresults/\;[root@xuegod63~]#mv/var/spool/mail/user1.mail/var/spool/mail/user1#修改文章为Thisend想了解更多技术+