[shell基础]——find命令

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 提醒你是否要执行后面的命令python

 


(1) -namelinux

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

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

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

 
(2) -typeshell

查找/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
bash

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

 
(6) -user -group -nouser -nogroup
spa

修改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) -newerxml

查询比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) -mountblog

表示不能垮文件系统查找,本身当前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练习题

一、查找/目录下面的"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

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

# find /tmp -perm 755 

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

四、建立一个用户和组 名为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天之内的文件

# find / -mtime -5 

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

# find / -mtime +3 

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

八、使用find查找/home下面全部的目录

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

 
九、使用find查找/home下面全部的文件,非目 录

# find /home ! -type d |xargs file

 
十、使用find查找/etc/ 下面全部的链接文件

# find /etc -type l

 
十一、查找/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

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

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

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

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

 
1四、检查系统有几个挂在的文件系统,好比/ 和/home是分开的,那么在/home/建立一个sxgongda.txt文件。使用find 参数mount查找/目录是否能够查到sxgongda.txt文件?

1五、查询/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

 1六、使用什么参数能够每次find后跟系统命令 时给出安全提示?# -ok

相关文章
相关标签/搜索