find命令的使用

find搜索文件系统、实时搜索
find [目录] [条件] [动作]

[目录]
不输入代表当前目录
例:
find
find /boot

[条件]
用户和组:-user -group
例:查找home目录下所有的属于指定的文件
[root@localhost ~]# find /home/ -user swk

类型:-type ( f 文件 , d 目录 , l 连接 , p 管道 ,c 字符文件 ,b 块文件 ,s socket文件 )
[root@localhost ~]# find /home/ -type f
[root@localhost ~]# find /home/ -type d

文件名:-name
[root@localhost ~]# useradd user1
[root@localhost ~]# useradd user2
[root@localhost ~]# useradd vipuser
[root@localhost ~]# touch /home/user1/userabc
[root@localhost ~]# find /home/ -name user
/home/user1
/home/user1/userabc
/home/user2
/home/vipuser

大小:-size +NM 大于N兆 -NM 小于N兆
例:找到boot目录下大于4M文件
[root@localhost ~]# find /boot/ -size +4M

find / -amin -10 # 查找在系统中最后10分钟访问的文件
find / -atime -2 # 查找在系统中最后48小时访问的文件
find / -empty # 查找在系统中为空的文件或者文件夹
find / -group cat # 查找在系统中属于 groupcat的文件
find / -mmin -5 # 查找在系统中最后5分钟里修改过的文件
find / -mtime -1 #查找在系统中最后24小时里修改过的文件
find / -nouser #查找在系统中属于作废用户的文件
find / -user fred #查找在系统中属于FRED这个用户的文件
时间: -mtime -atime -ctime
扩展:Linux系统中ctime , atime ,mtime 有什么区别
[root@localhost ~]# touch a.txt
[root@localhost ~]# stat a.txt
File: ‘a.txt’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd00h/64768d Inode: 36433014 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2016-02-20 07:55:13.285056713 -0500
Modify: 2016-02-20 07:55:13.285056713 -0500
Change: 2016-02-20 07:55:13.285056713 -0500

ctime:“改变时间(change time)”
mtime :“修改时间(modification time)”
atime :“访问时间(access time)”

例:
改变和修改之间的区别在于是改文件的属性还是更改它的内容。
chmod a-w myfile,那么这是一个改变;改变的ctime
echo aaa > bajie 那么这是一个修改。mtime :“修改时间
atime,改变是文件的索引节点发生了改变;mtime 修改是文本本身的内容发生了变化。

总结:当文件的属性发生修改时,ctime
当文件内容发生修改时,mtime,ctime
当文件被访问时,atime

[root@localhost ~]# touch a.txt
[root@localhost ~]# chmod +x a.txt #ctime发生改变
[root@localhost ~]# echo aaa > a.txt #mtime ,ctime发生改变
[root@localhost ~]# cat a.txt #atime发生改变

ls(1) 命令可用来列出文件的 atime、ctime 和 mtime。
ls -lc filename         列出文件的 ctime ll -c
ls -lu filename         列出文件的 atime ll -u
ls -l filename          列出文件的 mtime  ll

[root@localhost ~]# date -s 2016-2-23
Tue Feb 23 00:00:00 EST 2016
Mtime +(时间之钱) -(时间之内)
[root@localhost ~]# find /root/ -mtime +1 | grep a.txt
注:查找出root目录24小时之前创建的文件

[root@localhost ~]# find /root/ -mtime +2 | grep a.txt
注:查找出root目录48小时之前创建的文件

[root@localhost ~]# find /root/ -mtime -3 | grep a.txt
注:查找root目录下72小时之内创建的文件

权限:-perm
[root@localhost ~]# find /boot/ -perm 755 #等于0775权限的文件或目录
SUID 4,SGID 2 ,sticky 1

[root@localhost ~]# find /tmp/ -perm -777 #至少有777权限的文件或目录

例:
[root@localhost ~]# mkdir ccc
[root@localhost ~]# chmod 777 ccc
[root@localhost ~]# mkdir test
[root@localhost ~]# chmod 1777 test/
[root@localhost ~]# touch b.sh
[root@localhost ~]# chmod 4777 b.sh

[root@localhost ~]# find /root/ -perm 777
/root/ccc
[root@localhost ~]# find /root/ -perm 1777
/root/test
[root@localhost ~]# find /root/ -perm 4777
/root/b.sh
[root@localhost ~]# find /root/ -perm -777
/root/ccc
/root/test
/root/b.sh

查找的目录深度:
[root@localhost ~]# find /boot/ -maxdepth 2
#只查找目录第二层的文件和目录

多条件:
-a -o ! 或 -and -or -not
[root@localhost ~]# find /boot/ -size +4M -a -size -8M
注:找出来boot目录下文件大小在4~8M之间的文件或目录
[root@localhost ~]# find -type f -a -perm /o+w
./b.sh

[root@localhost ~]# find ! -type f -a -perm -001

[动作]
-ls
-ok
-exec
xargs
-print
-printf
[root@localhost ~]# touch test
[root@localhost ~]# cp /etc/passwd test/
[root@localhost ~]# cp -r /boot/ test/

[root@localhost ~]# find /root/test/ -type f -exec rm {} \;
或者:
[root@localhost ~]# find /root/test/ -type f | xargs rm -rf
参数解释:
-exec 执行命令
rm 要执行的命令
{} 表示find -type f 查找出来了文件内容
\; {} 和 \;之间要有空格。 固定语法,就是以这个结尾

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章