一、文件查找之locate命令

locate :
非实时的,查找时根据全系统文件数据库进行的,模糊查找,
update 手动生成文件数据库
速度快

依赖于updatedb数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#手动更新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

1
2
3
4
5
6
[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

根据文件类型来查找

-type
f:普通文件
d: 目录
c: 字符
b: 块设备
l: 链接
p: 管道
s: 套接字

example:find /etc -type d

1
2
3
4
5
6
7
#查找/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
[+|-]#G

example:find /tmp -size +10M 查找/etc目录下大于10M的文件

组合条件:这个需要了解摩根定律
-a:与 默认
-o:或
-not :非
example:find /tmp -not -user user1 -o -not -type d

1
2
3
4
5
6
7
8
#查找/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 *' \;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#查找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 \;

1
2
3
4
5
6
7
8
9
#找出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.schemas

find和xargs命令

xargs: 作用是将参数列表转换成小块分段传递给其他命令,以避免参数列表过长的问题。

相较于-exec command 而言,此命令功能更强悍。和find合用的时候,一般是通过管道传递给xargs

find /tmp –size +100M | xargs ‘rm –rf '

三、其他查找命令

which: 定位一个命令的完整路径,有可能会显示出命令的别名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#使用which命令查找ls命令的完整路径
[root@lovelace scripts]# which ls
alias ls='ls --color=tty'
/bin/ls
#使用ldd命令查看ls所依赖的库文件(这里需要用到ls的完整路径)
[root@lovelace scripts]# ldd /bin/ls
linux-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页的完整路径

1
2
3
4
5
6
7
8
#使用whereis查看命令的完整路径和相应的man文件
[root@lovelace scripts]# whereis ls
ls/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 ls

whatis:将会在whatis数据库中查询file,当你想确认系统命令和重要的配置文件的时候

,这个命令就非常重要了,可以当做一个简单的man命令。

1
2
3
[root@lovelace scripts]# whatis ls
ls                   (1)  - list directory contents
ls                   (1p)  - list directory contents

总结:之前面对which、whatis、whereis的时候,总是犯糊涂,这几个命令的作用老是给搞混,所以特此留记,以防止在搞混,另外,find命令对我们的运维工作至关重要,尤其是针对服务器被入侵之后,文件是否被篡改有着非同一般的功效。

转载于:https://www.cnblogs.com/wuhongkuan/p/4632235.html

Linux查找命令与find命令详解相关推荐

  1. linux 查找目录或文件 (详解)

    linux 查找目录或文件  (详解) 查找目录:find /(查找范围) -name '查找关键字' -type d 查找文件:find /(查找范围) -name 查找关键字 -print 如果需 ...

  2. linux 查找某个程序,Linux查找特定程序whereis实例详解

    Linux 查找特定程序 whereis whereis 命令主要用于查找程序文件,并提供这个文件的二进制可执行文件.源代码文件和使用手册存放位置. 1.查找命令程序 例如,查找 touch 命令 [ ...

  3. linux ipset 流量,linux中ipset命令的使用方法详解

    linux中ipset命令的使用方法详解 发布时间:2020-10-25 17:07:19 来源:脚本之家 阅读:97 作者:lijiaocn 栏目:服务器 ipset介绍 iptables是在lin ...

  4. Linux Bash命令关于程序调试详解

    转载:http://os.51cto.com/art/201006/207230.htm 参考:<Linux shell 脚本攻略>Page22-23 Linux bash程序在程序员的使 ...

  5. linux中grep命令 菜鸟教程,linux grep正则表达式与grep用法详解

    需要大家牢记:正则表达式与通配符不一样,它们表示的含义并不相同 正则表达式只是字符串的一种描述,只有和支持正则表达式的工具相结合才能进行字符串处理.本文以grep为例来讲解正则表达式. grep命令 ...

  6. linux mount命令参数及用法详解

    linux mount命令参数及用法详解 非原创,主要来自 http://www.360doc.com/content/13/0608/14/12600778_291501907.shtml. htt ...

  7. linux useradd(adduser)命令参数及用法详解(linux创建新用户命令)

    linux useradd(adduser)命令参数及用法详解(linux创建新用户命令) useradd可用来建立用户帐号.帐号建好之后,再用passwd设定帐号的密码.而可用userdel删除帐号 ...

  8. linux中group命令详解,linux groupmod命令参数及用法详解

    需要更改群组的识别码或名称时,可用groupmod指令来完成这项工作.接下来是小编为大家收集的linux groupmod命令参数及用法详解,希望能帮到大家. linux groupmod命令参数及用 ...

  9. linux的usermod命令参数,linux usermod命令参数及用法详解

    linuxusermod命令参数及用法详解,linux修改用户账号信息命令,usermod可用来修改用户帐号的各项设定.接下来是小编为大家收集的linux usermod命令参数及用法详解,欢迎大家阅 ...

  10. linux两台服务器传输,Linux两台服务器之间高速数据传输命令:scp应用详解

    Linux两台服务器之间高速数据传输命令:scp应用详解 Linux scp命令用于Linux之间复制文件和目录到另外一台,这个命令在多台服务器之间传输还是非常有用的,速度也是非常快的.比window ...

最新文章

  1. 95后程序员月薪2万背着电脑送外卖,送单途中改Bug
  2. 夜深深~帮别人做课程设计。。。
  3. Android给自定义按键添加广播和通过广播给当前焦点输入框赋值
  4. Xshell选中的同时把内容复制到剪贴板(还可以设置设置文本分隔符)
  5. 【PAT】1009. Product of Polynomials (25)
  6. 第4章 Python 数字图像处理(DIP) - 频率域滤波7 - 二维DFT和IDFT的一些性质 - 傅里叶频谱和相角
  7. BZOJ1045 HAOI2008糖果传递(贪心)
  8. 数据结构----二叉树叶子结点到根节点的高度计算
  9. 中国寒龙反网络病毒联盟核心小组:官方公告,近期本站将会发布各种编程技术视频教程,详情请点击我们的以下公告!...
  10. python编程入门到实践笔记习题_Python编程从入门到实践笔记——列表简介
  11. LightOJ 1013 LCS+记忆化搜索
  12. 运算符优先级(图表)
  13. lvm基本知识与常用命令
  14. 汇编指令-adr与ldr伪汇编区别(8)
  15. C++模板实现的通用工厂方法模式
  16. JavaWeb面试题
  17. 管理系统中计算机应用 tps,全国1月自学考试管理系统中计算机应用试题(5)
  18. 火车头怎么采集php的,火车采集器采集入库教程
  19. linux 原路返回路由,linux – 根据服务将返回流量路由到正确的网关
  20. 软件测试如何快速入门

热门文章

  1. Google的特殊功能
  2. 最小生成树算法(Prim和Kruskal)
  3. 相称显微镜下细胞群体跟踪
  4. Wave Arts Tube Saturator for Mac(电子管模拟效果器插件)
  5. NoteBurner iTunes DRM Audio Converter for Mac(苹果DRM音频转换器)
  6. Magoshare Data Recovery使用教程:在mac上恢复找回删除的丢失文件
  7. Windows Server 2008 使用PowerShell开启 ssh 和 sftp
  8. SDNU 1170.津津的储蓄计划
  9. spring beans 的类型
  10. 存储虚拟化技术之解读