我尝试创建一个 liner bash 脚本,该脚本X
根据文件名查找并删除超过几天的目录,并且如果目录存在,代码可以完美运行。唯一的问题是,如果目录不存在,它会尝试删除它并出错:
find: ‘/var/www/html/resources/cache/2022-02-08’: No such file or directory
尽管我已将检查目录是否存在的条件放在 find 命令的结果中,然后将其删除。
find '/var/www/html/resources/cache/' -type d -name '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]' -exec sh -c 'd={}; [ "$(date -d "6 days ago" +%Y-%m-%d)" ">" "$(basename $d)" ] && [ -d $d ] && rm -rf $d' \;
假设我有以下目录位于/var/www/html/resources/cache
2022-02-08
2022-02-09
2022-02-10
2022-02-11
2022-02-12
2022-02-13
2022-02-14
2022-02-15
[ "$(date -d "6 days ago" +%Y-%m-%d)" ">" "$(basename $d)" ]
检查文件名是否早于 6 天
[ -d $d ]
检查它是否是一个目录
答案1
您可以使用xargs
命令而不是-exec
选项find
。
find '/var/www/html/resources/cache/' -type d -name '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]' | xargs -I {} sh -c 'd={}; [ "$(date -d "6 days ago" +%Y-%m-%d)" ">" "$(basename $d)" ] && [ -d $d ] && rm -rf $d'
上面的代码块可以工作,但它可能会123
作为值返回。 当任何重复命令出现错误时,xargs
命令将返回。123
因此,如果有一个目录名称不是六天前,就会发生错误。
find '/var/www/html/resources/cache/' -type d -name '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]' | xargs -I {} sh -c 'd={}; if [ "$(date -d "6 days ago" +%Y-%m-%d)" ">" "$(basename $d)" ]; then [ -d $d ] && rm -rf $d; fi'
您可以用语句替换条件if
来解决此问题。
答案2
您可以将六天前的日期转换为秒,将目录名称转换为秒,并执行直接数字比较。由于您只考虑最顶层的目录,因此您根本不需要find
,只需使用标准循环遍历目录即可
ago=$(date --date "6 days ago" +%s)
for dir in /var/www/html/resources/cache/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]
do
[ -d "$dir" ] || continue
this=$(date --date "${dir##*/}" +%s)
[ $this -lt $ago ] && echo "DELETE $dir"
done
如果你真的想使用find
,这个版本需要 GNUfind
或其他可以理解的版本-maxdepth
。 (如果你没有这样的,find
有一些方法可以模拟-maxdepth
。)
find /var/www/html/resources/cache -maxdepth 1 -name "[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]" -exec sh -c '
ago=$(date --date "6 days ago" +%s)
for dir in "$@"
do
this=$(date --date "${dir##*/}" +%s)
[ $this -lt $ago ] && echo "DELETE $dir"
done
' _ {} +
两种方案的输出
DELETE /var/www/html/resources/cache/2022-02-08
DELETE /var/www/html/resources/cache/2022-02-09
准备好后,将语句更改echo
为rm -rf
您喜欢的任何内容。
答案3
测试带有时间戳的目录:
~] ls -alFh
drwxr-xr-x 11 root root 4.0K Feb 15 10:51 ./
drwxr-xr-x 3 root root 4.0K Feb 15 10:51 ../
drwxr-xr-x 2 root root 4.0K Feb 15 10:51 2022-02-08/
drwxr-xr-x 2 root root 4.0K Feb 15 10:51 2022-02-09/
drwxr-xr-x 2 root root 4.0K Feb 15 10:51 2022-02-10/
drwxr-xr-x 2 root root 4.0K Feb 15 10:51 2022-02-11/
drwxr-xr-x 2 root root 4.0K Feb 15 10:51 2022-02-12/
drwxr-xr-x 2 root root 4.0K Feb 15 10:51 2022-02-13/
drwxr-xr-x 2 root root 4.0K Feb 15 10:51 2022-02-14/
drwxr-xr-x 2 root root 4.0K Feb 15 11:01 2022-02-15/
drwxr-xr-x 2 root root 4.0K Feb 15 10:51 2022-02-16/
查找中的正则表达式
要使用正则表达式查找目录,您可以使用:
~] find . -type d -regextype posix-extended -regex '\./2022-02-[01][12345890]'
./2022-02-13
./2022-02-09
./2022-02-14
./2022-02-08
./2022-02-11
./2022-02-12
./2022-02-10
./2022-02-15
find中的时间条件
查找中的时间条件 - 最好的方法是使用-newerXY
-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 comparison.
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.
例子:
~] find . -type d -regextype posix-extended -regex '\./2022-02-[01][12345890]' ! -newermt "2022-02-15 10:52:00" -print
./2022-02-13
./2022-02-09
./2022-02-14
./2022-02-08
./2022-02-11
./2022-02-12
./2022-02-10
删除目录
~] find . -type d -regextype posix-extended -regex '\./2022-02-[01][12345890]' ! -newermt "2022-02-15 10:52:00" -exec rm -r {} \;
find: ‘./2022-02-13’: No such file or directory
find: ‘./2022-02-09’: No such file or directory
find: ‘./2022-02-14’: No such file or directory
find: ‘./2022-02-08’: No such file or directory
find: ‘./2022-02-11’: No such file or directory
find: ‘./2022-02-12’: No such file or directory
find: ‘./2022-02-10’: No such file or directory
~] ls -alFh
total 16K
drwxr-xr-x 4 root root 4.0K Feb 15 11:13 ./
drwxr-xr-x 3 root root 4.0K Feb 15 10:51 ../
drwxr-xr-x 2 root root 4.0K Feb 15 11:01 2022-02-15/
drwxr-xr-x 2 root root 4.0K Feb 15 10:51 2022-02-16/