find命令
find命令主要用来搜索文件
which ls 查询ls命令所在路径
±符号里 -表示数值内,相当于小于;+表示数值外,相当于大于;
find涉及格式如下:
- -name "文件名"
- -type [文件格式]
- -size [±符号][文件大小]
- -ctime [±符号][数值]
- -mtime [±符号][数值]
- -atime [±符号][数值]
- -mmin [±符号][数值]
- -inum [数值]
-name
搜索普通文件 find 路径 -name "文件名" 实验1:在/目录下搜索一个1.txt的文件;
find / -name "1.txt"
[root@shu-test ~]# find / -name "1.txt"
/root/abc/1.txt
/root/abc/a/1.txt
/tmp/1.txt
/tmp/b/1.txt
/tmp/b/a/1.txt
/shu32/1.txt
[root@shu-test ~]#
实验2:在/目录下搜索一个名字不全的文件;
find / -name "2.*"
[root@shu-test ~]# find / -name "2.*"
/root/abc/a/b/2.txt
/root/abc/a/2.txt
/tmp/b/2.txt
/usr/share/perl5/unicore/lib/Nv/2.pl
/shu32/2.txt
[root@shu-test ~]#
-type
指定搜索文件类型 搜索目录(d) 实验1:在/目录下搜索一个名字为a的目录 find / -type d -name "a"
[root@shu-test ~]# find / -type d -name "a"
/root/abc/a
/var/lib/yum/yumdb/a
/tmp/b/a
/usr/share/terminfo/a
搜索文件(f) 实验2:在/目录下搜索一个名字为1.txt的文件 find / -type f -name "1.txt"
[root@shu-test ~]# find / -type f -name "1.txt"
/root/abc/1.txt
/root/abc/a/1.txt
/tmp/1.txt
/tmp/b/1.txt
/tmp/b/a/1.txt
/shu32/1.txt
[root@shu-test ~]#
根据文件的类型去搜索; type参数 d :目录 **f(-):普通文件 ** l:软链接文件
根据时间搜索参数 -mtime 时间:创建时间、最近更改 -ctime 时间:最近改动 -atime 时间:最近访问(使用vi、cat等),包括更改权限 stat命令 查询文件最近访问时间、最近更改时间、最近更改时间、创建时间;
[root@shu-test abc]# stat 1.txt
文件:"1.txt"
大小:5 块:8 IO 块:4096 普通文件
设备:803h/2051d Inode:33640385 硬链接:1
权限:(0700/-rwx------) Uid:( 0/ root) Gid:( 0/ root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2017-12-21 17:04:10.080922712 +0800
最近更改:2017-12-20 21:14:03.422282935 +0800
最近改动:2017-12-20 21:14:03.441283024 +0800
创建时间:-
[root@shu-test abc]#
-ctime
实验1:根据“改动时间”来查找 查找/下面ctime也就是“最近改动”范围在一天以内的文件; find / -type f -ctime -1
/tmp/b/2_heard.txt
[root@shu-test abc]# stat /tmp/b/2.txt
File: '/tmp/b/2.txt'
Size: 111 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 10292 Links: 2
Access: (0755/-rwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2017-12-21 20:36:19.195909713 +0800
Modify: 2017-12-21 20:36:19.196909718 +0800
Change: 2017-12-21 20:36:19.196909718 +0800
Birth: -
-atime
实验2: 查找/下面atime也就是“最近访问”范围在1天以上的文件;
find / -type f -atime +1
/shu32/1.txt
[root@shu-test abc]# stat /shu32/1.txt
File: '/shu32/1.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051d Inode: 72 Links: 1
Access: (0774/-rwxrwxr--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:default_t:s0
Access: 2017-11-14 01:18:02.942255849 +0800
Modify: 2017-11-14 01:18:02.942255849 +0800
Change: 2017-11-14 01:38:54.557523346 +0800
Birth: -
[root@shu-test abc]#
-mtime
实验3: 查找/下面mtime也就是“最近更改时间”范围在38天以上的文件; find / -type f -mtime +38
/home/user01/.bashrc
[root@shu-test abc]# stat /home/user01/.bashrc
File: '/home/user01/.bashrc'
Size: 231 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 33583282 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ user01) Gid: ( 1000/ user01)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2017-12-20 21:14:31.733417328 +0800
Modify: 2017-08-03 05:11:00.000000000 +0800
Change: 2017-11-18 21:09:40.175627699 +0800
Birth: -
-mmin
根据创建时间多少分钟搜索 find 路径 -type f -mmin -分钟值 实验1: 查询/下 最近180分钟以内创建的文件
find / -type f -mmin -180
/var/log/wtmp
[root@shu-test abc]# stat /var/log/wtmp
File: '/var/log/wtmp'
Size: 139008 Blocks: 272 IO Block: 4096 regular file
Device: 803h/2051d Inode: 16895248 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 0/ root) Gid: ( 22/ utmp)
Context: system_u:object_r:wtmp_t:s0
Access: 2017-11-09 03:29:43.619998029 +0800
Modify: 2017-12-22 16:08:44.755498178 +0800
Change: 2017-12-22 16:08:44.755498178 +0800
Birth: -
查找/tmp/目录下创建时间小于120分钟的文件 以ls-l的方式列出来
find /tmp/ -type f -mmin -120 -exec ls -l {} \;
-rw-r--r--. 1 root root 846 12月 22 20:04 /tmp/a/2.txt
查找/tmp/目录下创建时间小于120分钟的文件,改名为.*.bak*
find /tmp/ -type f -mmin -120 -exec mv {} {}.bak \;
[root@shu001 a]# ls
2.txt.bak
[root@shu001 a]#
-inum
根据inum号查询文件(硬链接) find 路径 -inum 数值 find / -inum 10292
[root@shu-test b]# find / -inum 10292
/sys/devices/pci0000:00/0000:00:16.7/power/async
/sys/kernel/debug/tracing/events/xfs/xfs_log_ungrant_exit/format
/tmp/b/2.txt
/tmp/b/2_heard.txt
[root@shu-test b]#
-size
根据文件大小查找 查找目录下小于10k的文件,将其ls -lh表示出来 find /tmp/ -type f -size -10k -exec ls -lh {} {} ;
[root@shu001 a]# find /tmp/ -type f -size -10k -exec ls -lh {} {} \;
-rw-r--r--. 1 root root 846 12月 22 20:04 /tmp/a/2.txt.bak
-rw-r--r--. 1 root root 846 12月 22 20:04 /tmp/a/2.txt.bak
[root@shu001 a]#
单位需要用大写,比如10M
文件后缀名
文件后缀名:Linux下午后缀名需求,只是为了好区分文件类型; 例如1.txt文件也可以写成1,一样也被系统识别; 加后缀名只是便于我们自己区分; 比如一说到txt我们就知道是文本,。bak是备份、rar是压缩等;