[英]How to delete multiple folders by comparing folder name and system timestamp as reference, using a bash script?
I am trying to write a Bash script
which will delete a bunch of folders which are older than 30 days,by comparing the folder names to the system time.我正在尝试编写一个
Bash script
,通过将文件夹名称与系统时间进行比较,该脚本将删除一堆超过 30 天的文件夹。
These are the folders:这些是文件夹:
20201001/ 20201002/ 20201003/ 20201106/ 20201108/ 20201109/ 20201201/
I am trying to read the these folder names in a bash script and compare it with system timestamp and delete the folders which are 30 days old.我正在尝试在 bash 脚本中读取这些文件夹名称,并将其与系统时间戳进行比较,然后删除 30 天前的文件夹。
This is what i have right now:这就是我现在所拥有的:
now="$(date +'%Y%m%d')"
printf "$now"
echo
echo */
This is the output i get:这是我得到的 output:
20201203
20201001/ 20201002/ 20201003/ 20201106/ 20201108/ 20201109/ 20201201/
echo */
gives a list of folder but how to pass it into the deletion command line: echo */
给出文件夹列表,但如何将其传递到删除命令行:
find list -mtime +30 -exec rm {} \;
Since I've had similar requirements and questions in the past I wanted to share my solution approach here.由于过去我有类似的要求和问题,我想在这里分享我的解决方案。
For testing here, I've created a data structure with test/<nameFolderByDate>
.为了在这里进行测试,我使用
test/<nameFolderByDate>
创建了一个数据结构。 If listing the file statistics via stat
, some meta data including access, modification and change time is given.如果通过
stat
列出文件统计信息,则会给出一些元数据,包括访问、修改和更改时间。
stat -c "%y %s %n" test/*
2020-12-03 14:11:38.788388456 +0100 4096 test/20200101
2020-12-03 14:11:41.288389539 +0100 4096 test/20200201
2020-12-03 14:11:42.984390273 +0100 4096 test/20200301
2020-12-03 14:09:14.747326011 +0100 4096 test/20200401
As one can see the timestamps might (sometimes) not fit to the structure <nameFolderByDate>
.正如人们所看到的,时间戳可能(有时)不适合结构
<nameFolderByDate>
。
Therefore I've created a small script which sets the modification time ( mtime
) to the date within the name.因此,我创建了一个小脚本,将修改时间 (
mtime
) 设置为名称中的日期。 I use this approach under other circumstances too.我也在其他情况下使用这种方法。
cat cleanupData.sh
#!/bin/bash
echo ""
echo "Set modification time to date in name"
for FILENAME in test/*; do
echo ${FILENAME}
NAMETIME=$(echo ${FILENAME} | cut -d "/" -f 2)
echo ${NAMETIME}
TIMESTAMP=$(date --date="${NAMETIME}" "+%Y%m%d%H%M")
echo ${TIMESTAMP}
touch -m -a -t ${TIMESTAMP} ${FILENAME}
done
exit
After normalizing the data structure the result with stat
is规范化数据结构后,
stat
结果为
./cleanupData.sh
Set modification time to date in name
test/20200101
20200101
202001010000
test/20200201
20200201
202002010000
test/20200301
20200301
202003010000
test/20200401
20200401
202004010000
stat -c "%y %s %n" test/*
2020-01-01 00:00:00.000000000 +0100 4096 test/20200101
2020-02-01 00:00:00.000000000 +0100 4096 test/20200201
2020-03-01 00:00:00.000000000 +0100 4096 test/20200301
2020-04-01 00:00:00.000000000 +0200 4096 test/20200401
as expected in this case and find
can be used as required, ie正如在这种情况下所预期的那样,
find
可以根据需要使用,即
find test -mtime +300
test/20200101
test/20200201
I will leave other parts of creating a fully functional script to the audience.我将把创建功能齐全的脚本的其他部分留给观众。
Thanks also to也感谢
You can skip echo and use find command directly with following options:您可以通过以下选项跳过 echo 并直接使用 find 命令:
-maxdepth 1 -> exclude subdirectories
! -path . -> exclude current working directory
-type d -> exclude files
-mtime +30 -> exclude directory modified last 30 days
-exec rm -r {} \; -> to remove the directories
find . -maxdepth 1 ! -path . -type d -mtime +30 -exec rm -r {} \;
Otherwise, if you want to pass directory list to find command you can use ls and xargs commands:否则,如果您想将目录列表传递给 find 命令,您可以使用 ls 和 xargs 命令:
ls -d */ | xargs -I % find % -type d -maxdepth 0 -mtime +30 -exec rm -r {} \;
Edit: For a security point of view it's better to use $arg instead of % as explain here: Running multiple commands with xargs .编辑:从安全的角度来看,最好使用 $arg 而不是 % ,如下所述: Running multiple commands with xargs 。 It also allow you to use echo instead of ls command
它还允许您使用 echo 而不是 ls 命令
echo */ | xargs sh -c 'for arg do find $arg -type d -maxdepth 0 -mtime +30 -exec rm -r {} \; ; done'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.