Linux查找命令与find命令详解
一、文件查找之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 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页的完整路径
|
1
2
3
4
5
6
7
8
|
#使用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 ls |
whatis:将会在whatis数据库中查询file,当你想确认系统命令和重要的配置文件的时候
,这个命令就非常重要了,可以当做一个简单的man命令。
|
1
2
3
|
[root@lovelace scripts]# whatis lsls (1) - list directory contentsls (1p) - list directory contents |
总结:之前面对which、whatis、whereis的时候,总是犯糊涂,这几个命令的作用老是给搞混,所以特此留记,以防止在搞混,另外,find命令对我们的运维工作至关重要,尤其是针对服务器被入侵之后,文件是否被篡改有着非同一般的功效。
Linux查找命令与find命令详解的更多相关文章
- linux管道命令grep命令参数及用法详解---附使用案例|grep
功能说明:查找文件里符合条件的字符串. 语 法:grep [-abcEFGhHilLnqrsvVwxy][-A<显示列数>][-B<显示列数>][-C<显示列数>] ...
- Linux Bash命令关于程序调试详解
转载:http://os.51cto.com/art/201006/207230.htm 参考:<Linux shell 脚本攻略>Page22-23 Linux bash程序在程序员的使 ...
- (转)linux traceroute命令参数及用法详解--linux跟踪路由命令
linux traceroute命令参数及用法详解--linux跟踪路由命令 原文:http://blog.csdn.net/liyuan_669/article/details/25362505 通 ...
- linux mount命令参数及用法详解
linux mount命令参数及用法详解 非原创,主要来自 http://www.360doc.com/content/13/0608/14/12600778_291501907.shtml. htt ...
- 【转】linux expr命令参数及用法详解
在抓包过程中,查看某个设定时间内,数据上下行多少,用命令expr 计算! --------------------------------------------------------------- ...
- linux useradd(adduser)命令参数及用法详解(linux创建新用户命令)
linux useradd(adduser)命令参数及用法详解(linux创建新用户命令) useradd可用来建立用户帐号.帐号建好之后,再用passwd设定帐号的密码.而可用userdel删除帐号 ...
- linux dmesg命令参数及用法详解(linux显示开机信息命令)
linux dmesg命令参数及用法详解(linux显示开机信息命令) http://blog.csdn.net/zhongyhc/article/details/8909905 功能说明:显示开机信 ...
- linux sed命令参数及用法详解
linux sed命令参数及用法详解 http://blog.csdn.net/namecyf/article/details/7336308 1. Sed简介 sed 是一种在线编辑器,它一次处理一 ...
- linux dd命令参数及用法详解---用指定大小的块拷贝一个文件(也可整盘备份)
linux dd命令参数及用法详解---用指定大小的块拷贝一个文件 日期:2010-06-14 点击:3830 来源: 未知 分享至: linux dd命令使用详解 dd 的主要 ...
- (转)linux expr命令参数及用法详解
linux expr命令参数及用法详解 原文:http://blog.csdn.net/tianmohust/article/details/7628694 expr用法 expr命令一般用于整数值, ...
随机推荐
- Listview 利用Datapager进行分页
原文:http://lgm9128.blog.163.com/blog/static/421734292010513111851101/ <asp:ListView ID="ListV ...
- IIS将http强转为https(重定向和重写)
最近接到一个需求,客户希望无论是http还是https请求都可以访问,并且http能转换成https.研究了一圈发现iis的重定向和重写都可以实现http强转https,记录一下. 用到的东东: In ...
- 【大数据之数据仓库】HAWQ versus GreenPlum
谈到GreenPlum,肯定会有同事说HAWQ!是的,在本系列第一篇选型流水记里,也有提到.因为对HAWQ接触有限,没有深入具体了解,所以很多信息都是来自于博文,人云亦云,我把看过的资料简要整理,希望 ...
- fiddler 代理调试本地手机页面
https://www.cnblogs.com/zichi/p/4944581.html
- python-flask之request的属性
flask的request模块的属性(参考) #代码示例,仅仅是为了测试request的属性值 @app.route('/login', methods = ['GET','POST']) def l ...
- 【大数据系统架构师】0.2 Linux基础
1. Linux基本环境 1.1 大数据Hadoop前置大纲讲解 1)Linux系统,基本命令 2)Java语言,JavaSE相关知识 3)MySQL基本的DML和DDL 1.2 常见Linux系统. ...
- requests模块处理cookie,代理ip,基于线程池数据爬取
引入 有些时候,我们在使用爬虫程序去爬取一些用户相关信息的数据(爬取张三“人人网”个人主页数据)时,如果使用之前requests模块常规操作时,往往达不到我们想要的目的. 一.基于requests模块 ...
- Windows NLB搭配IIS的ARR搭建高可用环境(转载)
原文地址:http://www.cnblogs.com/shanyou/archive/2010/04/28/1723276.html 在现行的许多网络应用中,有时一台服务器往往不能满足客户端的要求, ...
- 将0移到最后,在原数组操作,并且不能改变源数据顺序(JS编程)
一.问题描述: 将0移到最后,在原数组操作,并且不能改变源数据顺序. 示例:输入:[2,0,0,1,0,3], 结果:[2,1,3,0,0,0] 二.问题分析与解决: 注意是在原数组上操作,不要进行 ...
- SqlServer批量插入(SqlBulkCopy、表值参数)
之前做项目需要用到数据库的批量插入,于是就研究了一下,现在做个总结. 创建了一个用来测试的Student表: CREATE TABLE [dbo].[Student]( [ID] [int] PRIM ...