【linux】循序渐进学运维-find

find命令的使用

find [目录] [条件] [动作]

[目录]

不输入代表当前目录

例:

find

find /boot

[条件]

用户和组:-user -group

例:查找home目录下所有的属于指定的文件

[root@xinsz0861 ~]# find /home/ -user swk

类型:-type ( f 文件 , d 目录 , l 连接 , p 管道 ,c 字符文件 ,b 块文件 ,s socket文件 )

[root@xinsz0861 ~]# find /home/ -type f

[root@xinsz0861 ~]# find /home/ -type d

文件名:-name

[root@xinsz0861 ~]# useradd user1

[root@xinsz0861 ~]# useradd vipuser2

[root@xinsz0861 ~]# touch /home/user1/aaauserccc

[root@xinsz0861 ~]# find /home/ -name user

/home/user1

/home/user1/aaauserccc

/home/vipuser2

大小:-size +NM 大于N兆 -NM 小于N兆

例:找到boot目录下大于4M文件

[root@xinsz0861 ~]# find /boot/ -size +4M

时间: -mtime -atime -ctime

任务:百度一下如何针对分钟进行查找

扩展:Linux系统中ctime , atime ,mtime 有什么区别

[root@xinsz0861 ~]# touch a.txt

[root@xinsz0861 ~]# 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@xinsz0861 ~]# touch a.txt

[root@xinsz0861 ~]# chmod +x a.txt #ctime发生改变

[root@xinsz0861 ~]# echo aaa > a.txt #mtime ,ctime发生改变

[root@xinsz0861 ~]# 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@xinsz0861 ~]# date -s 2016-2-23

Tue Feb 23 00:00:00 EST 2016

[root@xinsz0861 ~]# find /root/ -mtime +1 | grep a.txt

注:查找出root目录48小时之前创建的文件

[root@xinsz0861 ~]# find /root/ -mtime 2 | grep a.txt

注:查找出root目录48小时之前创建的文件

[root@xinsz0861 ~]# find /root/ -mtime -3 | grep a.txt

注:查找root目录下72小时之内创建的文件

权限:-perm

[root@xinsz0861 ~]# find /boot/ -perm 755 #等于0775权限的文件或目录

SUID 4,SGID 2 ,sticky 1

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

操作方便才是硬道理

例:

[root@xinsz0861 ~]# mkdir ccc

[root@xinsz0861 ~]# chmod 777 ccc

[root@xinsz0861 ~]# mkdir test

[root@xinsz0861 ~]# chmod 1777 test/

[root@xinsz0861 ~]# touch b.sh

[root@xinsz0861 ~]# chmod 4777 b.sh

[root@xinsz0861 ~]# find /root/ -perm 777

/root/ccc

[root@xinsz0861 ~]# find /root/ -perm 1777

/root/test

[root@xinsz0861 ~]# find /root/ -perm 4777

/root/b.sh

[root@xinsz0861 ~]# find /root/ -perm -777

/root/ccc

/root/test

/root/b.sh

查找的目录深度:

[root@xinsz0861 ~]# find /boot/ -maxdepth 2

#只查找目录第二层的文件和目录

多条件:

-a -o ! 或 -and -or -not

[root@xinsz0861 ~]# find /boot/ -size +4M -a -size -8M

注:找出来boot目录下文件大小在4~8M之间的文件或目录

[root@xinsz0861 ~]# find -type f -a -perm /o+w

./b.sh

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

[动作]

-ls

-ok

-exec

xargs

-print

-printf

[root@xinsz0861 ~]# touch test

[root@xinsz0861 ~]# cp /etc/passwd test/

[root@xinsz0861 ~]# cp -r /boot/ test/

[root@xinsz0861 ~]# find /root/test/ -type f -exec rm {} ;

或者:

[root@xinsz0861 ~]# find /root/test/ -type f | xargs rm -rf

参数解释:

-exec 执行命令

rm 要执行的命令

{} 表示find -type f 查找出来了文件内容

; {} 和 ;之间要有空格。 固定语法,就是以这个结尾

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值