Linux查找命令与find命令详解

本文发布时间: 2019-Mar-22
一、文件查找之locate命令locate :非实时的,查找时根据全系统文件数据库进行的,模糊查找,update 手动生成文件数据库速度快依赖于updatedb数据库#手动更新locate数据库[root@lovelace scripts]# updatedb#使用locate查找文件 (速度好快的说)[root@lovelace scripts]# locate *.py/home/scripts/factorial.py/home/scripts/input.py/usr/lib/python2.4/fileinput.py/usr/lib/python2.4/fileinput.pyc/usr/lib/python2.4/fileinput.pyo/usr/lib/python2.4/site-packages/dogtail/rawinput.py/usr/lib/python2.4/site-packages/dogtail/rawinput.pyc/usr/lib/python2.4/site-packages/dogtail/rawinput.pyo/usr/lib/python2.4/test/pyclbr_input.py/usr/lib/python2.4/test/pyclbr_input.pyc/usr/lib/python2.4/test/pyclbr_input.pyo/usr/lib/python2.4/test/test_fileinput.py/usr/lib/python2.4/test/test_fileinput.pyc/usr/lib/python2.4/test/test_fileinput.pyo二、文件查找之find命令find:实时精确支持众多查找规则遍历指定目录中的所有文件完成查找,速度慢find 查找路径 查找标准 查找到以后的处理运作查找路径:默认为当前目录查找标准:默认为指定路径下的所有文件查找到以后的处理操作:默认为显示匹配标准:-name 'filename':对文件名作精确匹配文件名通配:* 任意长度的任意字符? 任意的单个字符[] 选项内的字符-iname 'filename':文件名匹配时不区分大小写 -regex pattern 基于正则表达式进行文件名匹配-user username :根据属主查找-group groupname :根据属组查找-gid gid:根据gid查找-uid uid::根据udi查找-nouser 查找没有属主的文件-nogroup 没有属组的文件example: find /tmp -nouser[root@lovelace scripts]# find /tmp -name test/tmp/sources/httpd-2.2.17/srclib/apr-util/test/tmp/sources/httpd-2.2.17/srclib/apr/test/tmp/sources/httpd-2.2.17/modules/test/tmp/sources/httpd-2.2.17/test/tmp/test根据文件系统类型来查找-typef:普通文件d: 目录c: 字符b: 块设备l: 链接p: 管道s: 套接字example:find /etc -type d#查找/tmp目录下名字为test 而且文件格式为目录的[root@lovelace scripts]# find /tmp -type d -a -name test/tmp/sources/httpd-2.2.17/srclib/apr-util/test/tmp/sources/httpd-2.2.17/srclib/apr/test/tmp/sources/httpd-2.2.17/modules/test/tmp/sources/httpd-2.2.17/test/tmp/test根据文件大小查找-size[+|-]#k 没有+和-就代表是精确匹配[+|-]#m[+|-]#Gexample:find /tmp -size +10M 查找/etc目录下大于10M的文件组合条件:这个需要了解摩根定律-a:与 默认-o:或-not :非example:find /tmp -not -user user1 -o -not -type d#查找/tmp目录下文件格式为目录,而且大小在26k到32k之间的目录[root@lovelace scripts]# find /tmp -type d -a -size +16k -a -size -32k/tmp/sources/httpd-2.2.17/docs/manual/mod/tmp/sources/php-5.2.13/ext/reflection/tests/tmp/sources/php-5.2.13/ext/date/tests/tmp/sources/php-5.2.13/ext/spl/tests/tmp/sources/php-5.2.13/tests/classes/tmp/sources/php-5.2.13/Zend/tests根据文件时间戳来查找:以天为单位的:改变时间:-mtime修改时间:-ctime查看时间:-atime[+|-]# 默认时间戳为5天-5:5天内访问过+5:至少5天没访问过了5:离现在刚好5天访问过以分钟为单位的:-mmin:-cmin:-amin:find /tmp -amin –5 5分钟内被访问过的文件至少多久没有被访问的,且文件大小超过多少的,执行删除操作find /tmp -atime +30 -a –size +100M -exec 'rm -rf *' \;#查找home目录下至少5天没被访问过的文件[root@lovelace scripts]# find /home/ -ctime -5/home/scripts/home/scripts/list/home/scripts/for/home/scripts/for/dir.sh/home/scripts/for/three.sh/home/scripts/for/checkbash.sh/home/scripts/for/sorsum.sh/home/scripts/while/home/scripts/while/readpasswd.sh/home/scripts/while/catwhile.sh/home/scripts/case/home/scripts/case/showmenu.sh/home/scripts/case/showmenu/home/scripts/case/2showmenu.sh/home/scripts/if/home/scripts/if/grade.sh/home/scripts/51cto/home/scripts/51cto/info.tt/home/scripts/51cto/info.sh/home/scripts/51cto/1.sh/home/scripts/51cto/argument.sh/home/scripts/51cto/sum.sh根据权限查找:-perm mode :精确匹配-perm -mode :每一位权限都必须精确匹配 文件权限能完全包含此mode的均符合标准-perm /mode :9位权限中有任何一位符合条件的example:find /tmp -perm –001 查找其他用户有写权限的文件find的动作:-print 默认-ls:类似 ls -l的形式显示文件的每一个信息-ok command {} \; 会每次执行进行询问操作,需要用户确认-exec command {} \; 不会惊醒询问操作引用原来的额名字,使用{}example:find /tmp -perm -020 -exec mv {} {}.new \;#找出home中大小为16k到32k之间的文件,然后传递给exec 并显示出来[root@lovelace scripts]# find /home/ -size +16k -a -size -32k -exec ls -lh {} \;-rw-r--r-- 1 root root 20K 05-03 03:04 /home/nick/etc/gconf/gconf.xml.defaults/%gconf-tree-li.xml-rw-r--r-- 1 root root 20K 05-03 03:04 /home/nick/etc/gconf/gconf.xml.defaults/%gconf-tree-ug.xml-rw-r--r-- 1 root root 25K 05-03 03:04 /home/nick/etc/gconf/schemas/drivemount.schemas-rw-r--r-- 1 root root 20K 05-03 03:04 /home/nick/etc/gconf/schemas/gnome-volume-control.schemas-rw-r--r-- 1 root root 21K 05-03 03:04 /home/nick/etc/gconf/schemas/system_smb.schemas-rw-r--r-- 1 root root 22K 05-03 03:04 /home/nick/etc/gconf/schemas/desktop_gnome_thumbnailers.schemas-rw-r--r-- 1 root root 17K 05-03 03:04 /home/nick/etc/gconf/schemas/apps_gnome_settings_daemon_default_editor.schemasfind和xargs命令xargs: 作用是将参数列表转换成小块分段传递给其他命令,以避免参数列表过长的问题。相较于-exec command 而言,此命令功能更强悍。和find合用的时候,一般是通过管道传递给xargsfind /tmp –size +100M | xargs ‘rm –rf '三、其他查找命令which: 定位一个命令的完整路径,有可能会显示出命令的别名#使用which命令查找ls命令的完整路径[root@lovelace scripts]# which lsalias ls='ls --color=tty'/bin/ls#使用ldd命令查看ls所依赖的库文件(这里需要用到ls的完整路径)[root@lovelace scripts]# ldd /bin/lslinux-gate.so.1 => (0x00cf8000)librt.so.1 => /lib/librt.so.1 (0x00d8d000)libacl.so.1 => /lib/libacl.so.1 (0x00d62000)libselinux.so.1 => /lib/libselinux.so.1 (0x00de0000)libc.so.6 => /lib/libc.so.6 (0x00110000)libpthread.so.0 => /lib/libpthread.so.0 (0x00d71000)/lib/ld-linux.so.2 (0x00baf000)libattr.so.1 => /lib/libattr.so.1 (0x003a9000)libdl.so.2 => /lib/libdl.so.2 (0x00d55000)libsepol.so.1 => /lib/libsepol.so.1 (0x00d98000)whereis:与which类似,而且会额外的给出给出该命令的man页的完整路径#使用whereis查看命令的完整路径和相应的man文件[root@lovelace scripts]# whereis lsls: /bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz#man 查看对应的文件[root@lovelace scripts]# man 1 ls#man 查看对应的文件[root@lovelace scripts]#[root@lovelace scripts]# man 1p lswhatis:将会在whatis数据库中查询file,当你想确认系统命令和重要的配置文件的时候,这个命令就非常重要了,可以当做一个简单的man命令。[root@lovelace scripts]# whatis lsls (1) - list directory contentsls (1p) - list directory contents总结:之前面对which、whatis、whereis的时候,总是犯糊涂,这几个命令的作用老是给搞混,所以特此留记,以防止在搞混,另外,find命令对我们的运维工作至关重要,尤其是针对服务器被入侵之后,文件是否被篡改有着非同一般的功效。


(以上内容不代表本站观点。)
---------------------------------
本网站以及域名有仲裁协议。
本網站以及域名有仲裁協議。

2024-Mar-04 02:10pm
栏目列表