shell学习(19)- find查找命令
Linux find命令用来在指定目录下查找文件。任何位于参数之前的字符串都将被视为欲查找的目录名。如果使用该命令时,不设置任何参数,则find命令将在当前目录下查找子目录与文件。并且将查找到的子目录和文件全部进行显示。find命令默认的是当前目录,默认的是打印-print。
语法
find path -option [ -print ] [ -exec -ok command ] {} \;
参数说明 :
find 根据下列规则判断 path 和 expression,在命令列上第一个 - ( ) , ! 之前的部份为 path,之后的是 expression。如果 path 是空字串则使用目前路径,如果 expression 是空字串则使用 -print 为预设 expression。 expression 中可使用的选项有二三十个之多,在此只介绍最常用的部份。 -mount, -xdev : 只检查和指定目录在同一个文件系统下的文件,避免列出其它文件系统中的文件 -amin n : 在过去 n 分钟内被读取过 -anewer file : 比文件 file 更晚被读取过的文件 -atime n : 在过去n天内被读取过的文件 -cmin n : 在过去 n 分钟内被修改过 -cnewer file :比文件 file 更新的文件 -ctime n : 在过去n天内被修改过的文件 -empty : 空的文件-gid n or -group name : gid 是 n 或是 group 名称是 name -ipath p, -path p : 路径名称符合 p 的文件,ipath 会忽略大小写 -name name, -iname name : 文件名称符合 name 的文件。iname 会忽略大小写 -size n : 文件大小 是 n 单位,b 代表 位元组的区块,c 表示字元数,k 表示 kilo bytes,w 是二个位元组。-type c : 文件类型是 c 的文件。 d: 目录 c: 字型装置文件 b: 区块装置文件 p: 具名贮列 f: 一般文件 l: 符号连结 s: socket -pid n : process id 是 n 的文件 你可以使用 ( ) 将运算式分隔,并使用下列运算。 exp1 -and exp2 ! expr -not expr exp1 -or exp2 exp1, exp2
为了方便测试,先建几个文件
for i in $(seq );do touch $i.py; done;
for i in $(seq );do touch $i.txt; done;
touch TEST.txt
touch test.txt
ls
.py .txt .py .txt .py .txt .py .txt .py .txt TEST.txt test.txt
1.基础查找
find . -print
.
./.txt
./.txt
./.py
./.py
./.py
./.txt
./.py
./.txt
./.txt
./.py
.指定当前目录,..指定父目录,print选项使用\n换行符分割输出的每个文件或目录,而-print0选择则使用空字符'\0'来分割
2.根据文件名查找
find . -name '*.txt' -print
./.txt
./.txt
./.txt
./.txt
./.txt
find . -iname test.txt -print
./TEST.TXT
./test.txt
3.find逻辑查找
find . -iname '*.py' -o -iname '*.txt'
./.txt
./.txt
./.py
./TEST.TXT
./test.txt
./.py
./.py
./.txt
./.py
./.txt
./.txt
./.py
查找后缀为py且以1开头的文件
find . -iname '*.py' -and -name '1*'
./.py
正则表达式查找
find -iregex '.*\(\.py\|\.txt\)$'
./.txt
./.txt
./.py
./TEST.TXT
./test.txt
./.py
./.py
./.txt
./.py
./.txt
./.txt
./.py
4.否定参数
find也可以用!排除匹配到的模式
find . ! -name "*.txt" -print
.
./.py
./TEST.TXT
./.py
./.py
./.py
./.py
5.基于目录深度搜索
find -L /proc -maxdepth -name 'bundlemaker.def' >/dev/null
find . -mindepth -name "f*" -print
6.根据文件类型搜索
列出普通文件
find . -type f -print
列出目录
find . -type d -print
列出符号链接
find . -type l -print
find能够识别的类型与参数
|
文件类型
|
类型参数
|
|
普通文件
|
f
|
|
符合链接
|
l
|
|
目录
|
d
|
|
字符设备
|
c
|
|
块设备
|
b
|
|
套接字
|
s
|
|
FIFO
|
p
|
7.根据时间戳进行搜索
find . -type f -atime - -print
打印出恰好在7天前被访问过的所有文件
find . -type f -atime -print
打印出访问时间超过7天的所有文件
find . -type f -atime + -print
find . -type f -amin + -print
8.基于文件大小的搜索
find . -type f -size +2k
find . -type f -size -2k
find . -type f -size 2k
find . -type f -perm -print
Web服务器上的PHP文件需要具有合适的执行权限。我们可以用下面的方法找出那些没有设置好执行权限的PHP文件:
find . -type f -name "*.php" ! -perm –print
查找前目录中文件属主具有读、写权限,并且文件所属组的用户和其他用户具有读权限的文件:
find . -type f -perm -exec ls -l {} \;
find . -type f -name test.swp -delete
(2)删除其他文件
find . -type f -name "*黑名单*" -print0 | xargs - rm -f
find . -type f -name "*.py" -exec rm {} \;
find . -type f -name "*.py" -print | xargs rm -f
find . -type f -name "*.py" | xargs rm -f
find . -type f -name "*.py" | xargs -I {} rm -f {}
find /var/log -type f -mtime + -ok rm {} \;
(3)执行命令-exec
find . -type f -user root -exec chown slynux {} \;
find . -type f -mtime + -name "*.txt" -exec cp {} OLD \;
-exec ./commands.sh {} \;
11.find跳过特定的目录
find devel/source_path -name '.git' -prune -o -type f -print
shell学习(19)- find查找命令的更多相关文章
- Shell学习:grep, sed, awk命令的练习题
http://www.cnblogs.com/chengmo/archive/2013/01/17/2865479.html 文件:datafileSteve Blenheim:238-923-736 ...
- 【shell学习笔记】curl命令总结
2014-12-16 20:34 文思海辉 =========== CURL命令总结 1. 下载 curl -o [文件名称] www.baidu.com 2. 显示 HTTP request头信息 ...
- SHELL学习笔记三
SHELL学习笔记一 SHELL学习笔记二 SHELL学习笔记三 for 命令 读取列表中的复杂值 从变量读取列表 从命令读取值 更改字段分隔符 用通配符读取目录 which 使用多个测试命令 unt ...
- Shell学习(七)——sort、uniq、cut、wc命令详解
Shell学习(七)--sort.uniq.cut.wc命令详解 转自:[1]linux sort,uniq,cut,wc命令详解 https://www.cnblogs.com/ggjucheng/ ...
- Linux学习之查找命令汇总
我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索: which 查看可执行文件的位置. whereis 查看文件的位置. ...
- Linux学习之常用网络通信命令与shell简单应用技巧(四)
(一)常用网络通信命令 (1)ping命令 (2)write命令 (3)wall命令 (4)ifconfig命令 (5)shutdown命令 (6)reboot命令 (二)shell简单应用技巧 (1 ...
- bash shell学习笔记(一)—— 常用命令
一.基本的bash shell命令 1.默认bash shell 提示符是美元符号($); 2.bash手册 使用man命令来访问存储在Linux系统上的手册页面,如: bogon:~ Mac$ ma ...
- linux shell 学习笔记--内部命令学习
.基本命令 新手必须要掌握的初级命令 ls 基本的列出所有文件的命令.但是往往就是因为这个命令太简单,所以我们总是低估它.比如 ,用 -R 选项,这是递归选项,ls 将会以目录树的形式列出所有文件, ...
- 2. 2.1查找命令——linux基础增强,Linux命令学习
2.1.查找命令 grep命令 grep 命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并 把匹配的行打印出来. 格式: grep [option] pattern [file] 可使用 ...
- shell 学习笔记4-shell内置变量命令
一.shell 的一些内置命令 常用的一内部命令有:echo.eval.exec.export.read.shift 1.echo命令-在屏幕中输出信息 1)说明 格式:echo args #< ...
随机推荐
- BTC功能类
<?php/*EasyBitcoin-PHP A simple class for making calls to Bitcoin's API using PHP.https://github. ...
- “Bootstrap做的响应式菜单在iPhone上点击不了二级菜单“的解决办法!
只需把把点击的a(被点击的)变成button即可.
- JAVA获取磁盘空间
需要把磁盘空间显示在页面上,这样就不用去服务器查看了,方便 两个办法 File file = new File("D:"); long totalSpace = file.getT ...
- C++: Mac上安装Boost库并使用CLion开发
1.下载安装Boost库 官网下载最新版本1.65.0:http://www.boost.org/users/history/version_1_65_0.html 选择UNIX版本: 下载后解压cd ...
- Leetcode64.Minimum Path Sum最小路径和
给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明:每次只能向下或者向右移动一步. 示例: 输入: [ [1,3,1], [1,5,1] ...
- web前端学习(二)html学习笔记部分(7)--web存储2
1.2.20 web存储 1.2.20.1 Web存储-客户端存储数据新方法 1.两种方式 1)localStorage - 没有时间限制的数据存储 2)针对一个sessionStorage - ...
- 2017年2月27日Unicorn, US (148) and China (69), followed by the U.K. (10), India (9), Israel (5) and Germany (5).
Revisiting The Unicorn Club Get to know the newest crowd of billion dollar startups In 2013, when Ai ...
- LintCode_173 链表插入排序
题目 用插入排序对链表排序 样例 Given 1->3->2->0->null, return 0->1->2->3->null C++代码 ListN ...
- php 单向散列加密
1.加密文件 <?php //sha1_en.php header("content-type:text/html;charset=utf-8"); $str = " ...
- 微信小程序之上拉加载更多
loadmore 加载更多(分页加载) 当用户打开一个页面时,假设后台数据量庞大时,一次性地返回所有数据给客户端,页面的打开速度就会有所下降,而且用户只看上面的内容而不需要看后面的内容时,也浪费用户流 ...