码迷,mamicode.com
首页 > 系统相关 > 详细

Linux之find 命令

时间:2020-02-19 16:55:18      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:常用   hand   pre   OWIN   安装   并且   actual   语法   字符   

Linux find命令用来在指定目录下查找文件。任何位于参数之前的字符串都将被视为欲查找的目录名。如果使用该命令时,不设置任何参数,则find命令将在当前目录下查找子目录与文件。并且将查找到的子目录和文件全部进行显示。

语法

find   path   -option   [   -print ]   [ -exec   -ok   command ]   {} \;

参数说明 :

find 根据下列规则判断 path 和 expression,在命令列上第一个 - ( ) , ! 之前的部份为 path,之后的是 expression。如果 path 是空字串则使用目前路径,如果 expression 是空字串则使用 -print 为预设 expression。

expression 中可使用的选项有二三十个之多,在此只介绍最常用的部份。

-mount, -xdev : 只检查和指定目录在同一个文件系统下的文件,避免列出其它文件系统中的文件

-amin n : 在过去 n 分钟内被读取过

-anewer file : 比文件 file 更晚被读取过的文件

-atime n : 在过去n天内被读取过的文件

-cmin n : 在过去 n 分钟内被修改过

-cnewer file :比文件 file 更新的文件

-ctime n : 在过去n天内被修改过的文件

-empty : 空的文件-gid n or -group name : gid 是 n 或是 group 名称是 name

-ipath p, -path p : 路径名称符合 p 的文件,ipath 会忽略大小写

-name name, -iname name : 文件名称符合 name 的文件。iname 会忽略大小写

-size n : 文件大小 是 n 单位,b 代表 512 位元组的区块,c 表示字元数,k 表示 kilo bytes,w 是二个位元组。-type c : 文件类型是 c 的文件。

d: 目录

c: 字型装置文件

b: 区块装置文件

p: 具名贮列

f: 一般文件

l: 符号连结

s: socket

-pid n : process id 是 n 的文件

按照文件名搜索

[root@localhost ~]# find 搜索路径 [选项] 搜索内容
选项:
-name: 按照文件名搜索
-iname: 按照文件名搜索,不区分文件名大小写
-inum: 按照 inode 号搜索

按照文件大小搜索

[root@localhost ~]# find 搜索路径 [选项] 搜索内容
选项:
-size [+|-]大小: 按照指定大小搜索文件

这里的“+”的意思是搜索比指定大小还要大的文件, “-”的意思是搜索比指定大小还要小的文件。

 -size n[cwbkMG]
              File uses n units of space.  The following suffixes can be used:

              `b‘    for 512-byte blocks (this is the default if no suffix is used)

              `c‘    for bytes

              `w‘    for two-byte words

              `k‘    for Kilobytes (units of 1024 bytes)

              `M‘    for Megabytes (units of 1048576 bytes)

              `G‘    for Gigabytes (units of 1073741824 bytes)

              The size does not count indirect blocks, but it does count blocks in sparse files that are not actually allocated.  Bear in mind that the `%k‘ and
              `%b‘ format specifiers of -printf handle sparse files differently.  The `b‘ suffix always denotes 512-byte blocks and  never  1  Kilobyte  blocks,
              which is different to the behaviour of -ls.
 

按照修改时间搜索
Linux 中的文件有访问时间(atime)、数据修改时间(mtime)、状态修改时间(ctime)这三个
时间,我们也可以按照时间来搜索文件。

[root@localhost ~]# find 搜索路径 [选项] 搜索内容
选项:
-atime [+|-]时间: 按照文件访问时间搜索
-mtime [+|-]时间: 按照文件数据修改时间搜索
-ctime [+|-]时间: 按照文件状态修改时间搜索

 

这三个时间的区别我们在 stat 命令中已经解释过了,这里用 mtime 数据修改时间来举例,重点说
说“[+-]”时间的含义。
? -5:代表 5 天内修改的文件。
? 5:代表前 56 天那一天修改的文件。
? +5:代表 6 天前修改的文件。

技术图片

按照权限搜索

[root@localhost ~]# find 搜索路径 [选项] 搜索内容
选项:
-perm 权限模式: 查找文件权限刚好等于“权限模式”的文件
-perm -权限模式: 查找文件权限全部包含“权限模式”的文件
-perm +权限模式: 查找文件权限包含“权限模式”的任意一个权限的文件

按照所有者和所属组搜索

[root@localhost ~]# find 搜索路径 [选项] 搜索内容
选项:
-uid 用户 ID: 按照用户 ID 查找所有者是指定 ID 的文件
-gid 组 ID: 按照用户组 ID 查找所属组是指定 ID 的文件
-user 用户名: 按照用户名查找所有者是指定用户的文件
-group 组名: 按照组名查找所属组是指定用户组的文件
-nouser: 查找没有所有者的文件 

按照所有者和所属组搜索时, “-nouser”选项比较常用,主要用于查找垃圾文件
只有一种情况例外,那就是外来文件。比如光盘和 U 盘中的文件如果是由 Windows 复制的,在
Linux 中查看就是没有所有者的文件;再比如手工源码包安装的文件,也有可能没有所有者
 按照文件类型搜索

[root@localhost ~]# find 搜索路径 [选项] 搜索内容
选项:
-type d: 查找目录
-type f: 查找普通文件
-type l: 查找软链接文件 

逻辑运算符

[root@localhost ~]# find 搜索路径 [选项] 搜索内容 

选项:

-notnot 逻辑非
1-aand 逻辑与
find 命令也支持逻辑运算符选项,其中-a 代表逻辑与运算,也就是-a 的两个条件都成立, find
索的结果才成立。举个例子:

[root@localhost ~]# find . -size +2k -a -type f
#在当前目录下搜索大于 2KB,并且文件类型是普通文件的文件

2-oor 逻辑或
-o 选项代表逻辑或运算,也就是-o 的两个条件只要其中一个成立, find 命令就可以找到结果。例
如:

[root@localhost ~]# find . -name cangls -o -name bols
./cangls
./bols

3-notnot 逻辑非
-not 是逻辑非,也就是取反的意思。举个例子:

[root@localhost ~]# find . -not -name cangls
#在当前目录下搜索文件名不是 cangls 的文件

其他选项
1-exec 选项
这里我们主要讲解两个选项“ -exec”和“ -ok”, 这两个选项的基本作用非常相似。 我们先来看
看“ -exec”选项的格式。

[root@localhost ~]# find 搜索路径 [选项] 搜索内容 -exec 命令 2 {} \;

其次,这个选项的作用其实是把 find 命令的结果交给由“ -exec”调用的命令 2 来处理。“ {}”就
代表 find 命令的查找结果。
2-ok 选项
-ok”选项和“ -exec”选项的作用基本一致, 区别在于:“ -exec”的命令 2 会直接处理,而不询
问;“ -ok”的命令 2 在处理前会先询问用户是否这样处理, 在得到确认命令后,才会执行。

测试:

[root@iZbp145axkc98giot5b448Z find]# touch abc
[root@iZbp145axkc98giot5b448Z find]# touch abcd
[root@iZbp145axkc98giot5b448Z find]# ll
total 0
-rw-r--r-- 1 root root 0 Feb 19 13:48 abc
-rw-r--r-- 1 root root 0 Feb 19 13:48 abcd
[root@iZbp145axkc98giot5b448Z find]# touch ababab
[root@iZbp145axkc98giot5b448Z find]# touch ABC
[root@iZbp145axkc98giot5b448Z find]# ll
total 0
-rw-r--r-- 1 root root 0 Feb 19 13:48 ababab
-rw-r--r-- 1 root root 0 Feb 19 13:48 abc
-rw-r--r-- 1 root root 0 Feb 19 13:49 ABC
-rw-r--r-- 1 root root 0 Feb 19 13:48 abcd
[root@iZbp145axkc98giot5b448Z find]# find . -name "ab*"
./abcd
./abc
./ababab
[root@iZbp145axkc98giot5b448Z find]# find . -iname "abc"
./abc
./ABC
[root@iZbp145axkc98giot5b448Z find]# vim abc
[root@iZbp145axkc98giot5b448Z find]# ll
total 4
-rw-r--r-- 1 root root   0 Feb 19 13:48 ababab
-rw-r--r-- 1 root root 251 Feb 19 13:50 abc
-rw-r--r-- 1 root root   0 Feb 19 13:49 ABC
-rw-r--r-- 1 root root   0 Feb 19 13:48 abcd
[root@iZbp145axkc98giot5b448Z find]# ll -h
total 4.0K
-rw-r--r-- 1 root root   0 Feb 19 13:48 ababab
-rw-r--r-- 1 root root 251 Feb 19 13:50 abc
-rw-r--r-- 1 root root   0 Feb 19 13:49 ABC
-rw-r--r-- 1 root root   0 Feb 19 13:48 abcd
[root@iZbp145axkc98giot5b448Z find]# vim abc
[root@iZbp145axkc98giot5b448Z find]# ll -h
total 4.0K
-rw-r--r-- 1 root root    0 Feb 19 13:48 ababab
-rw-r--r-- 1 root root 1.9K Feb 19 13:51 abc
-rw-r--r-- 1 root root    0 Feb 19 13:49 ABC
-rw-r--r-- 1 root root    0 Feb 19 13:48 abcd
[root@iZbp145axkc98giot5b448Z find]# find . -size +1k
.
./abc
[root@iZbp145axkc98giot5b448Z find]# find . -mtime -5
.
./abcd
./abc
./ababab
./ABC
[root@iZbp145axkc98giot5b448Z find]# find . -perm 777
[root@iZbp145axkc98giot5b448Z find]# find . -perm 644
./abcd
./abc
./ababab
./ABC
[root@iZbp145axkc98giot5b448Z find]# find . -type f
./abcd
./abc
./ababab
./ABC
[root@iZbp145axkc98giot5b448Z find]# find . -size +1k -a -type f
./abc
[root@iZbp145axkc98giot5b448Z find]# ll
total 4
-rw-r--r-- 1 root root    0 Feb 19 13:48 ababab
-rw-r--r-- 1 root root 1931 Feb 19 13:51 abc
-rw-r--r-- 1 root root    0 Feb 19 13:49 ABC
-rw-r--r-- 1 root root    0 Feb 19 13:48 abcd
[root@iZbp145axkc98giot5b448Z find]# find . -name "ab*" -exec rm -rf {} \;
[root@iZbp145axkc98giot5b448Z find]# ll
total 0
-rw-r--r-- 1 root root 0 Feb 19 13:49 ABC
[root@iZbp145axkc98giot5b448Z find]# man find
[root@iZbp145axkc98giot5b448Z find]#

 

Linux之find 命令

标签:常用   hand   pre   OWIN   安装   并且   actual   语法   字符   

原文地址:https://www.cnblogs.com/dalianpai/p/12331567.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!