[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 提醒你是否要执行后面的命令
(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
[shell基础]——find命令的更多相关文章
- shell基础--test命令的使用
test :用于文件类型检查和变量比较 一.用途: 1.判断表达式 2.判断字符串 3.判断整数 4.判断文件 测试例子: (1).test [root@~_~ day5]# cat test.sh ...
- [shell基础]——sed命令
关于sed sed 是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓 ...
- [shell基础]——read命令
read命令:在shell中主要用于读取输入.变量.文本 1. 接受标准输入(键盘)的输入,并将输入的数据赋值给设置的变量 [按回车键——表示输入完毕] [若输入的数据多于设置的变 ...
- [shell基础]——echo命令
echo命令:在shell中主要用于输出 1. -n 不换行的显示结果(默认是换行的) 2. -e " " 支持双引号中使用一些特殊字符 常用的特殊字符有 \a 发出警告 ...
- [shell基础]——sort命令
sort命令 sort是按照ASCII码升序输出,且是从首字符依次向后比较的 常见选项 -c 测试文件是否已经被排序 -r 逆向排序 -n 按照数字数值大小排序 -t 指定分割 ...
- [shell基础]——uniq命令
uniq命令常见选项 去除重复行 -u 显示不重复的行 -d 显示有重复的行 -c 打印每一行重复的次数 测试文本内容如下: # cat 4.txt 11 ...
- [shell基础]——cut命令
cut命令常见选项
- shell基础--cat命令的使用
一.cat的常用用法 1.总结 2.实验 (1).非交互式编辑 [root@~_~ day5]# cat > cattest.sh <<STOP > hello > ST ...
- Shell基础-Bash命令类型
Bash命令大体可以分为两类: 第一类是可执行文件,例如ls等 第二类是Bash内建命令,常见echo,cd等 bash命令优先级表:1别名 由alias指定 2关键字 3函数 由function ...
随机推荐
- 父窗口调用iframe子窗口方法
一.父窗口调用iframe子窗口方法 1.HTML语法:<iframe name="myFrame" src="child.html"></i ...
- Android knock code analysis
My colleague she forgot the knock code and ask me for help. I know her phone is LG G3 D855 with Andr ...
- poj2031 Building a Space Station
这题目,用G++ WA,用C++ AC. 题目要求,现给出n个球,然后要使每两个球直接或者间接连通,可以在任意两球之间做管道(在表面),最后的要求是,如果使得都连通的话,管道最小长度是多少. 思路简单 ...
- C语言开源项目
值得学习的C语言开源项目 - 1. Webbench Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的 ...
- C\C++ 框架和库整理(转)
[本文系外部转贴,原文地址:http://coolshell.info/c/c++/2014/12/13/c-open-project.htm]留作存档 下次造轮子前先看看现有的轮子吧 值得学习的C语 ...
- 树莓派 B+ Yeelink实现图像监控
树莓派 B+ Yeelink实现图像监控 数值传感器请参考 : http://blog.csdn.net/xiabodan/article/details/39084877 1 安装摄像头 ...
- 调试工具-fiddler
本地资源替换线上调试 Fiddler是一个http协议调试代理工具,它能够记录并检查所有你的电脑和互联网 之间的http通讯,设置断点,查看所有的“进出”Fiddler的数据(指cookie,html ...
- linu流量监控
iftophttp://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858923.html 默认是监控第一块网卡的流量iftop 监控eth1iftop ...
- Java通过Executors提供四种线程池
http://cuisuqiang.iteye.com/blog/2019372 Java通过Executors提供四种线程池,分别为:newCachedThreadPool创建一个可缓存线程池,如果 ...
- ThinkPHP常用变量
__ROOT__ : 网站根目录地址 __APP__ : 当前项目(入口文件)地址 __GROUP__:当前分组地址 __URL__ : 当前模块地址 __ACTION__ : 当前操作地址 _ ...