简体   繁体   English

如何使用 bash 脚本通过比较文件夹名称和系统时间戳作为参考来删除多个文件夹?

[英]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.

相关问题 Linux, shell 脚本] 如何通过按文件夹名称搜索来动态删除文件夹 - Linux, shell script] How to dynamically delete folders by searching by folder name bash housekeeing脚本需要权限才能删除不同的文件夹和文件夹中的文件 - bash housekeeing script needed permission to delete different folders and files in folder 使用 bash 脚本根据名称移动文件夹 - Using bash script to move folders based on their name 在bash脚本中基于名称作为日期删除文件夹(以mm / dd / yy为单位) - delete folders based on name as date in mm/dd/yy in bash script 如何从多个文件夹中提取文件并根据文件夹名称 (bash) 重命名? - How can I extract files from multiple folders and rename according to folder name (bash)? 如何使用 bash 脚本从一个文件中删除多个相同的字符? - How to delete multiple same characters from a file using bash script? 如何使用ffmpeg bash脚本从一个文件夹中的多个视频中提取单个图像(每次使用不同的名称) - How to extract single image (with different name each time) from multiple videos in the one folder using ffmpeg bash script 保留最近的3个文件夹,然后在bash脚本中删除其余的文件夹? - Keep the recent 3 folders and delete the rest in bash script? 如何使用 Bash 脚本将文件随机分布在 3 个文件夹中? - How to randomly distribute the files across 3 folders using Bash script? 如何通过比较文件名和文件夹名称将文件复制到具有相同名称的文件夹? - How to copy files to folders with same name by comparing file names with folder names?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM