其他参考 find常用参数详解
find命令使用说明 man find 。用法详细
find命令help
[root@docker136 ~]# find --help
用法: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
默认路径为当前目录;默认表达式为 -print
表达式可能由下列成份组成:操作符、选项、测试表达式以及动作:
操作符 (优先级递减;未做任何指定时默认使用 -and):
( EXPR ) ! EXPR -not EXPR EXPR1 -a EXPR2 EXPR1 -and EXPR2
EXPR1 -o EXPR2 EXPR1 -or EXPR2 EXPR1 , EXPR2
positional options (always true): -daystart -follow -regextype
normal options (always true, specified before other expressions):
-depth --help -maxdepth LEVELS -mindepth LEVELS -mount -noleaf
--version -xautofs -xdev -ignore_readdir_race -noignore_readdir_race
比较测试 (N 可以是 +N 或 -N 或 N): -amin N -anewer FILE -atime N -cmin N
-cnewer 文件 -ctime N -empty -false -fstype 类型 -gid N -group 名称
-ilname 匹配模式 -iname 匹配模式 -inum N -ipath 匹配模式 -iregex 匹配模式
-links N -lname 匹配模式 -mmin N -mtime N -name 匹配模式 -newer 文件
-nouser -nogroup -path PATTERN -perm [-/]MODE -regex PATTERN
-readable -writable -executable
-wholename PATTERN -size N[bcwkMG] -true -type [bcdpflsD] -uid N
-used N -user NAME -xtype [bcdpfls]
-context 文本
操作: -delete -print0 -printf 格式 -fprintf 文件 格式 -print
-fprint0 文件 -fprint 文件 -ls -fls 文件 -prune -quit
-exec 命令 ; -exec 命令 {} + -ok 命令 ;
-execdir 命令 ; -execdir 命令 {} + -okdir 命令 ;
(1)例子
find命令原理:从指定的起始目录开始,递归地搜索其各个子目录,查找满足寻找条件的文件,并可以对其进行相关的操作。
格式:find [查找目录] [参数] [匹配模型]
多参数格式:find [查找目录] [参数] [匹配模型] [参数] [匹配模型]
例如:
1、find . -name “*.sh”
查找在当前目录(及子目录)下找以sh结尾的文件。
2、find . -perm 755
查找在当前目录(及子目录)下找属性为755的文件。
查找有权限属性为700的文件
find /sbin -perm /700 | xargs ls -l
[root@docker136 ~]# find /sbin -perm /700 | xargs ls -l
lrwxrwxrwx. 1 root root 8 5月 19 2018 /sbin -> usr/sbin
3、find -user root
查找在当前目录(及子目录)下找属主为root的文件。
4、find /var -mtime -5
查找在/var下找更改时间在5天以内的文件。
5、find /var -mtime +3
查找在/var下找更改时间在3天以前的文件。
6、find /etc -type l
查找在/etc下查找文件类型为|的链接文件。
7、find . -size +1000000c
查找在当前目录(及子目录)下查找文件大小大于1M的文件,1M是1000000个字节。
8、find . -perm 700 |xargs chmod 777
查找出当前目录(及子目录)下所有权限为700的文件,并把其权限重设为777。
9、find . -type f |xargs ls -l
查找出文件并查看其详细信息。
10、删除nexus下自己上传文件多余的库文件
find /home/nexus/storage/central/ -name "*-lastUpdated.properties" -exec rm -f {} \; -name "*\.lastUpdated" -exec rm -f {} \; -or -name "_remote.repositories" -exec rm -f {} \;
-查找指定文件名并删除
find /home/dir -name filename.ext -delete #全匹配,注意 . 加\匹配
其他方式
find /home/dir -name filename.ext -exec rm -f {} \;
find /home/dir -name filename.ext | xargs rm -f
删除单条件文件
find /home/docker/docker_storage/nexus/nexus-data2/storage/central/org/mousio/etcd4j -name *\.lastUpdated -delete
或
find /home/docker/docker_storage/nexus/nexus-data2/storage/central/ -name "*-lastUpdated.properties" -exec rm -f {} \;
多条件查找文件删除
find /home/docker -name "*-lastUpdated.properties" -exec rm -f {} \; -name "*\.lastUpdated" -exec rm -f {} \; -or -name "_remote.repositories" -exec rm -f {} \;
等效
find /home/docker -name "*\.lastUpdated" -delete -or -name "_remote.repositories" -delete
/home/docker/docker_storage/nexus/nexus-data2/storage/central/ 要查找的文件目录
-exec rm -f {} ; 中 {}后必须要空格再加\;
两种方法:
1. 在一个目录中保留最近三个月的文件,三个月前的文件自动删除。
find /email/v2_bak -mtime +92 -type f -name *.mail[12] -exec rm -rf {} \;
/email/v1_bak --设置查找的目录;
-mtime +92 --设置时间为91天前;
-type f --设置查找的类型为文件;
-name *.mail[12] --设置文件名称中包含mail1或者mail2;
-exec rm -f --查找完毕后执行删除操作;
将此命令写入crontab后即可自动完成查找并删除的工作了。
2. 或者用:find . -ctime +40 -type f | xargs rm -rf
按时间删除文件: atime、ctime、mtime
需求:删除 /home/file/ 目录下最后修改小于10天之前的txt类型的文件
find /home/file/ -ctime +10 -name "*.txt" -print | xargs rm -f
或
find /home/file/ -ctime +10 -name "*.txt" | xargs rm -f
或者
find /home/file/ -ctime +10 -name "*.txt" -delete
经过测试在文件比较多的情况下 第二条命令比第一条速度更快。
//时间条件
分钟和天数的命令类似。n为正数就是>n天或分钟 , n为负数就是离现在的时间n天或分钟以内文件
-amin n: 查找最后访问时间 > n分钟以前的所有文件。
-atime n: 查找最后访问时间 >n天以前的所有文件。n为负数访问时间离现在n天内的文件。
-cmin n: 查找n分钟以前文件状态被修改过的所有文件。
-ctime n: 查找n天以前文件状态被修改过的所有文件。n为负数最后修改文件状态时间离现在n天内的文件。
-mmin n: 查找n分钟以前文件内容被修改过的所有文件。
-mtime n: 查找n天以前文件内容被修改过的所有文件。n为负数文件内容最后修改时间离现在n天内的文件。
参考
find命令之(-atime,-ctime,-mtime)
https://blog.csdn.net/li_ning_/article/details/51468980
[root@docker136 ~]# man find|grep -E "ctime|atime|mtime"
Measure times (for -amin, -atime, -cmin, -ctime, -mmin, and -mtime) from the beginning of today rather than from 24 hours ago. This option only affects tests which appear later on the command line.
-atime n
File was last accessed n*24 hours ago. When find figures out how many 24-hour periods ago the file was last accessed, any fractional part is ignored, so to match -atime +1, a file has to have been accessed at least
-ctime n
File's status was last changed n*24 hours ago. See the comments for -atime to understand how rounding affects the interpretation of file status change times.
-mtime n
File's data was last modified n*24 hours ago. See the comments for -atime to understand how rounding affects the interpretation of file modification times.
1.查找大文件:
find . -type f -size +100M #查找100M以上的文件
find . -type f -size +100M -print0 | xargs -0 du -h | sort -nr #对查找结果按照文件大小做一个排序
2.查找当前目录下前20的大目录
sudo du -hm --max-depth=2 | sort -nr | head -20
找出大文件
磁盘空间被耗尽的时候,免不了要清理一下,比如说/home目录太大,就可以使用下面命令看看到底是谁:
du -s /home/* | sort -nr
du命令参考
https://www.jb51.net/LINUXjishu/34649.html
1、编写清理日志脚本clearlog.sh
#!/bin/sh
find /newdisk/ss/mg/log -mtime +1 -name "*.log" -exec rm {} \;
find /newdisk/ss/msu/log -mtime +1 -name "*.log" -exec rm {} \;
2、给脚本增加可执行权限
我的脚本默认放在/opt/下
chmod 755 /opt/clearlog.sh
3、加入定时执行任务,定时执行脚本
切换到root用户
使用crontab 命令建立定时执行
命令:crontab –e
加入行:*/10 * * * * /opt/clearlog.sh
改行含义为每10分执行/usr/clearlog.sh脚本一次
#找到文件并删除
find /home/docker/docker_storage/nexus/nexus-data2/storage/central/ -name "*-lastUpdated.properties" -exec rm -f {} \; -name "*\.lastUpdated" -exec rm -f {} \; -or -name "_remote.repositories" -exec rm -f {} \;
#查找名字包含containers的文件或目录
find / -name containers
#查找超过200M以上的普通文件
find / -type f -size +200M
-type 查找某一类型的文件,诸如:
b - 块设备文件。
d - 目录。
c - 字符设备文件。
p - 管道文件。
l - 符号链接文件。
f - 普通文件。
使用权限: root用户和crontab文件的所有者
语法: crontab [-e [UserName]|-l [UserName]|-r [UserName]|-v [UserName]|File ]
说明: crontab 是用来让使用者在固定时间或固定间隔执行程序之用,换句话说,也就是类似使用者的时程表。-u user 是指设定指定 user 的时程表,这个前提是你必须要有其权限(比如说是 root)才能够指定他人的时程表。如果不使用 -u user 的话,就是表示设定自己的时程表。
删除crontab文件,可以用:
$ crontab -r
列出crontab文件,可以用:
$crontab -l
编辑cribtab
$ crontab -e
恢复丢失的crontab文件
如果不小心误删了crontab文件,假设你在自己的$HOME目录下还有一个备份,那么可以将其拷贝到/var/spool/cron/,其中是用户名。如果由于权限问题无法完成拷贝,可以用: $ crontab 其中,是你在$HOME目录中副本的文件名。
crontab中的输出配置
crontab中经常配置运行脚本输出为:>/dev/null 2>&1,来避免crontab运行中有内容输出。
shell命令的结果可以通过‘> ’的形式来定义输出/dev/null
代表空设备文件>
代表重定向到哪里,例如:echo “123” > /home/123.txt
1 表示stdout标准输出,系统默认值是1,所以">/dev/null"等同于"1>/dev/null"
2 表示stderr标准错误
& 表示等同于的意思,2>&1,表示2的输出重定向等同于1 那么重定向输出语句的含义:
1>/dev/null 首先表示标准输出重定向到空设备文件,也就是不输出任何信息到终端,不显示任何信息。
2>&1 表示标准错误输出重定向等同于标准输出,因为之前标准输出已经重定向到了空设备文件,所以标准错误输出也重定向到空设备文件。