极客进化岛
技术自由路

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

find命令的使用

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

[目录]

不输入代表当前目录

例:

find

find /boot

[条件]

用户和组:-user -group

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

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

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

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

/home/qi/.bash_logout
/home/qi/.bash_profile
/home/qi/.bashrc

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

/home/
/home/lost+found
/home/qi
/home/qi/.mozilla
/home/qi/.mozilla/extensions
/home/qi/.mozilla/plugins
/home/qi/.gnome2

文件名:-name

[root@zmkjedu71 ~]# useradd user1

[root@zmkjedu71 ~]# useradd vipuser2

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

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

/home/user1

/home/user1/aaauserccc

/home/vipuser2

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

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

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

/boot/initramfs-2.6.32-431.el6.x86_64.img
/boot/initrd-2.6.32-431.el6.x86_64kdump.img

时间: -mtime -atime -ctime

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

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

[root@zmkjedu71 ~]# touch a.txt

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

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

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

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

Tue Feb 23 00:00:00 EST 2016

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

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

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

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

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

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

权限:-perm

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

/boot/efi
/boot/efi/EFI
/boot/efi/EFI/redhat
/boot/efi/EFI/redhat/grub.efi
/boot/vmlinuz-2.6.32-431.el6.x86_64
/boot/grub

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

/tmp/
/tmp/pulse-pmASocj0OLnY/native
/tmp/.X11-unix
/tmp/.X11-unix/X0
/tmp/.esd-0/socket
/tmp/.ICE-unix
/tmp/.ICE-unix/2630
/tmp/VMwareDnD

操作方便才是硬道理

例:

[root@zmkjedu71 ~]# mkdir ccc

[root@zmkjedu71 ~]# chmod 777 ccc

[root@zmkjedu71 ~]# mkdir test

[root@zmkjedu71 ~]# chmod 1777 test/

[root@zmkjedu71 ~]# touch b.sh

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

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

/root/ccc

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

/root/test

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

/root/b.sh

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

/root/ccc

/root/test

/root/b.sh

查找的目录深度:

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

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

多条件:

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

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

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

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

./b.sh

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

[动作]

-ls

-ok

-exec

xargs

-print

-printf

[root@zmkjedu71 ~]# touch test

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

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

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

或者:

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

参数解释:

-exec 执行命令

rm 要执行的命令

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

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

赞(0)
未经允许不得转载:极客进化岛 » 【linux】循序渐进学运维-find