一.问题情况
由于服务器长期上传图片,导致硬盘已满,所以需要清理指定时间之前的图片文件.找了一下没有现成的find命令解读,基本都是错误的说明,所以自己看了一下find的帮助文档自行整理了一下方法.
二.解决办法
在centos6和centos7上测试的,请先确认find程序版本,4.3.3以上的版本才可以用-newerXY
1 |
find --version |
1 2 3 4 5 6 7 8 |
find (GNU findutils) 4.5.11 Copyright (C) 2012 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Eric B. Decker, James Youngman, and Kevin Dalley. Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION FTS(FTS_CWDFD) CBO(level=2) |
查看了一下find的帮助文档
1 |
man find |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
-anewer file File was last accessed more recently than file was modified. If file is a symbolic link and the -H option or the -L option is in effect, the access time of the file it points to is always used. -cnewer file File's status was last changed more recently than file was modified. If file is a symbolic link and the -H option or the -L option is in effect, the status-change time of the file it points to is always used. -newer file File was modified more recently than file. If file is a symbolic link and the -H option or the -L option is in effect, the modification time of the file it points to is always used. -newerXY reference Compares the timestamp of the current file with reference. The reference argument is normally the name of a file (and one of its timestamps is used for the comparison) but it may also be a string describing an absolute time. X and Y are placeholders for other letters, and these letters select which time belonging to how reference is used for the compari‐ son. a The access time of the file reference B The birth time of the file reference c The inode status change time of reference m The modification time of the file reference t reference is interpreted directly as a time Some combinations are invalid; for example, it is invalid for X to be t. Some combinations are not implemented on all systems; for example B is not supported on all systems. If an invalid or unsupported combination of XY is specified, a fatal error results. Time specifications are interpreted as for the argument to the -d option of GNU date. If you try to use the birth time of a reference file, and the birth time cannot be determined, a fatal error message results. If you specify a test which refers to the birth time of files being examined, this test will fail for any files where the birth time is unknown. -newer Supported. If the file specified is a symbolic link, it is always dereferenced. This is a change from previous behaviour, which used to take the relevant time from the symbolic link; see the HISTORY section below. Feature Added in Also occurs in -newerXY 4.3.3 BSD |
上面这些是判断时间是否大于指定文件的时间的一些参数,-anewer是新于文件访问时间,-cnewer是新于文件创建时间,-newer是新于文件修改时间,-newerXY的XY是其他参数的占位符,不是实际功能,不知道为啥外面会有-newerX -newerY这种命令,甚至把Y说成是min和max用于判断时间上限和下限.简直坑人.X可以写a/B/c/m部分系统没有B,Y可以写t.at就是访问时间,ct就是创建时间,mt就是修改时间,Bt是文件引用时间如果默认不写XY的参数,-newer默认就是-newermt匹配,如果用文件匹配的话-anewer/-cnewer/-newer,要注意是否是软链文件,看提示是软链文件可以用-H和-L参数来确认是软链文件本身时间还是实际链接文件的时间.没有具体测试,需要的可以自己测试下.
知道了参数之后,只需要判断条件需求即可,find默认的匹配条件是and也就是-a,就是写的条件全部符合,我们需要判断小于指定时间并且多种后缀文件,那就需要写2个条件组合起来使用:
比如:判断路径下修改时间小于2025年1月1日之前的图片文件:
1 |
find /path -type f ! -newermt "2025-01-01" \( -name "*.jpg" -o -name "*.gif" -o -name "*.png" -o -name "*.jpeg" -o -name "*.ico" -o -name "*.webp" \) |
-newermt是大于指定修改时间,由于我们要小于,所以加了!反条件就变成小于指定时间了(当然也可以指定两个日期之间的比如2024年1月1日到2025年1月1日之间的-newermt "2024-01-01" ! -newermt "2025-01-01"),由于判断是文件所以有-type f,后面的多种后缀由于是或or的条件,但是和前面2个条件又是且and的关系,所以要单独括起来匹配,如果需要删除这些文件直接加个参数-delete就行了,不需要网上说的再用命令管道符"|"传递到xargs删除或者使用-exec rm -f {};删除,都没有-delete方便好记,xargs还可能因为文件有空格造成删除失败或者误删,如果要用xargs的话xargs -0和-print0搭配使用保险:
1 |
find /path -type f ! -newermt "2025-01-01" \( -name "*.jpg" -o -name "*.gif" -o -name "*.png" -o -name "*.jpeg" -o -name "*.ico" -o -name "*.webp" \) -delete |