您的位置:首页 > 运维架构 > Shell

[shell基础]——find命令

2016-07-21 20:18 796 查看
find命令选项
-name 按照文件名查找
-type 查找某一类型的文件(b 代表设备块;d 目录;c 字符设备文件;l 符号(软)链接文件;f 普通文件)
-size 查找文件长度或者大小

-prune 查找文件时排除当前文件夹,不查找
-path
-depth 查找文件时,首先查找当前文件、当前目录中的文件,然后再在其子目录当中查找
-maxdepth 后面跟数字num,表示在当前目录下往下num层深度(默认为1层)

-perm 按照文件权限来查找
-user 可以按照文件属主来查找
-group 可以按照文件数组来查找
-nouser 查找无效所属主的文件
-nogroup 查找无效所属组的文件

-newer:查找比文件更新时间更新的文件
-mtime:按对象内容修改时间查找(天数)
-atime:按对象被访问的时间查找(天数)
-ctime:按对象状态被修改的时间去查找(天数)
-mmin/-amin/-cmin:(分钟数)
“+ n” n天/分钟以前的
“- n” n天/分钟以内的

-empty 查找大小为0的文件或空目录
-mount 在查找文件系统时不跨越mount的文件系统
-follow 如果find命令遇到符号链接文件,就跟踪链接文件指向的源文件

-a 且
-o 或
! 非
-ok 提醒你是否要执行后面的命令

(1) -name

查找/test下面所有以.txt结尾的文件
# find /test -name "*.txt"

查找/test下面所有以数字开头的txt文件
# find /test -name "[0-9].txt"

查找/目录下面的"passwd"文件和"profile" 文件
# find / -name "profile" -o -name "passwd"


(2) -type

查找/test下面所有的目录
# find /test -type d | xargs file

查找/etc 下面的所有l 连接文件
# find /etc -type l | xargs ls -l | more


(3) -size

查找当前目录文件大于10M的文件
# find . -size +10M | xargs du –lh

查找当前目录文件小于10M的文件
# find . -size -10M | xargs du –lh

查找/etc/目录下面大于1M的文件,并且把 文件大小信息列出来
# find /etc/ -size +1M |xargs du -sh


(4) -prune -path -depth

当前目录下有1.doc、2.doc,其子目录/test下有3.doc、4.doc
查找当前目录下所有的.doc文件
# find . -name "*.doc"
./test/4.doc
./test/3.doc
./2.doc
./1.doc

忽略子目录/test,只查询当前目录的.doc文件
# find . -path "./test" -prune -o -name "*.doc"
./test
./2.doc
./1.doc

进入(或者说是指定)子目录/test查找.doc文件
# find ./test -depth -name "*.doc"
./test/4.doc
./test/3.doc


(5) -perm


查找/test下权限为755的文件
# find /test -perm 755



(6) -user -group -nouser -nogroup


修改gongda.txt文件的属主属组为gongda
# useradd gongda
# touch gongda.txt
# chown gongda:gongda gongda.txt

查找当前目录下属主为gongda的文件
# find . -user gongda | xargs ls -l
-rw-r--r-- 1 gongda gongda 0 5月 13 20:50 ./gongda.txt

查找当前目录下属组为gongda的文件
# find . -group gongda | xargs ls -l
-rw-r--r-- 1 gongda gongda 0 5月 13 20:50 ./gongda.txt

删除gongda用户和组
# userdel -r gongda

查找当前目录下无效属主的文件
# find . -nouser | xargs ls -l
-rw-r--r-- 1 509 509 0 5月 13 20:50 ./gongda.txt

查找当前目录下无效属组的文件
# find -nogroup | xargs ls -l
-rw-r--r-- 1 509 509 0 5月 13 20:50 ./gongda.txt


(7) -newer

查询比1.txt新的文件
# find . -newer 1.txt

查询比1.txt新但是比alvin.txt旧的文件
# find . -newer 1.txt ! -newer alvin.txt


(8) -mtime

查找在/目录下面更改时间在5天以内的文件
# find / -mtime -5

查找在/目录下面更改时间在3天以前的目录
# find / -mtime +3


(9) -mount

表示不能垮文件系统查找,自己当前mount的文件系统查找
# find / -mount -name "alvinzeng.txt"


(10)使用exec或者ok来执行shell命令
find . -type f -exec ls -l {} \; 不提示
find . -type f –ok ls -l {} \; 安全提示

-ok 后面一般根删除命令rml
find /test -type f –ok rm {} \;

(11) xargs命令
xargs比exec更加方便
find /test –name “*.*” | xargs ls –l

find练习题

1、查找/目录下面的"passwd"文件和"profile" 文件

# find / -name "profile" -o -name "passwd"
/usr/local/python3.5.1/lib/python3.5/site-packages/IPython/core/profile
/usr/bin/passwd
/usr/libexec/emacs/23.1/x86_64-redhat-linux-gnu/profile
/usr/src/kernels/2.6.32-279.el6.x86_64/include/config/branch/profile
/root/.vnc/passwd
/root/apr-1.5.2/passwd
/root/passwd
/root/home/passwd
/etc/pam.d/passwd
/etc/passwd
/etc/profile
/passwd
/home/passwd


2、查找/tmp下面权限为755的文件

# find /tmp -perm 755


3、在/test/aa 下面创建一个名字为 gongda.txt的文件,用find命令把/test/aa目 录给忽略掉是否还可以查找的到?

4、创建一个用户和组 名为gongda,然后 gongda.txt所属用户和所属组全部修改成 gongda,使用user和group查找gongda的用户组,是否可 以查找到?然后删除掉gongda用户和组,再用nouser 和 nogroup查找没有所属和说是组的文件是否可以 查到?

# useradd gongda
# touch gongda.txt
# chown gongda:gongda gongda.txt
# find . -user gongda |xargs ls -l
-rw-r--r-- 1 gongda gongda 0 5月 13 20:50 ./gongda.txt
# find . -group gongda | xargs ls -l
-rw-r--r-- 1 gongda gongda 0 5月 13 20:50 ./gongda.txt
# userdel -r gongda
# find . -nouser | xargs ls -l
-rw-r--r-- 1 509 509 0 5月 13 20:50 ./gongda.txt
# find -nogroup | xargs ls -l
-rw-r--r-- 1 509 509 0 5月 13 20:50 ./gongda.txt


5、查找在/目录下面更改时间在5天以内的文件

# find / -mtime -5


6、查找在/目录下面更改时间在3天以前的目录

# find / -mtime +3


7、在/opt/test/下面创建一个名字为new.txt 文件。等5分钟后再创建一个ok.txt的文件。使 用find命令查找比gongda.txt新比ok.txt旧的 文件。

8、使用find查找/home下面所有的目录

# find /home -type d |xargs file
/home: directory
/home/user25: directory
/home/user25/.mozilla: directory


9、使用find查找/home下面所有的文件,非目 录

# find /home ! -type d |xargs file


10、使用find查找/etc/ 下面所有的连接文件

# find /etc -type l


11、查找/etc/目录下面大于1M的文件,并且把 文件大小信息列出来。

# find /etc/ -size +1M |xargs du -sh
2.0M /etc/gconf/gconf.xml.defaults/%gconf-tree.xml
6.3M /etc/selinux/targeted/policy/policy.24
6.3M /etc/selinux/targeted/modules/active/policy.kern


12、查找/etc/目录下面小于500K的文件,并且 把文件大小信息列出来

# find /etc/ -size -500k |xargs du -sh


13、查找/opt/test 子目录下面的gongda.txt 文件

# find "./test" -depth -name gongda.txt
./test/gongda.txt


14、检查系统有几个挂在的文件系统,比如/ 和/home是分开的,那么在/home/创建一个sxgongda.txt文件。使用find 参数mount查找/目录是否可以查到sxgongda.txt文件?

15、查询/opt/下面的gongda.txt文件,并且使 用exec 列出它的详细信息。

# find /opt/ -name gongda.txt -exec ls -l {} \;
-rw-r--r-- 1 509 509 0 5月 13 20:50 /opt/rh/gongda.txt
-rw-r--r-- 1 root root 0 5月 13 21:31 /opt/rh/test/gongda.txt


16、使用什么参数可以每次find后跟系统命令 时给出安全提示?
# -ok
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: