用于删除超过15个月的文件的Bash脚本

用于删除超过15个月的文件的Bash脚本,bash,shell,Bash,Shell,在bash脚本中,如果要删除目录中超过15天的文件,可以运行: find "$DIR" -type f -mtime +15 -exec rm {} \; 有人能帮我用bash脚本删除目录中超过15个月的文件吗 这与Bash中的ctime相同吗?根据手册页: -mtime n[smhdw] If no units are specified, this primary evaluates to true if the difference between the file

在bash脚本中,如果要删除目录中超过15天的文件,可以运行:

find "$DIR" -type f -mtime +15 -exec rm {} \;
有人能帮我用bash脚本删除目录中超过15个月的文件吗


这与Bash中的ctime相同吗?

根据手册页:

 -mtime n[smhdw]
         If no units are specified, this primary evaluates to true if the difference between the file last modification time and the time find was started, rounded up to the next
         full 24-hour period, is n 24-hour periods.

         If units are specified, this primary evaluates to true if the difference between the file last modification time and the time find was started is exactly n units.  Please
         refer to the -atime primary description for information on supported time units.
然后,在
-atime

 -atime n[smhdw]
         If no units are specified, this primary evaluates to true if the difference between the file last access time and the time find was started, rounded up to the next full
         24-hour period, is n 24-hour periods.

         If units are specified, this primary evaluates to true if the difference between the file last access time and the time find was started is exactly n units.  Possible
         time units are as follows:

         s       second
         m       minute (60 seconds)
         h       hour (60 minutes)
         d       day (24 hours)
         w       week (7 days)

         Any number of units may be combined in one -atime argument, for example, ``-atime -1h30m''.  Units are probably only useful when used in conjunction with the + or - modi-
         fier.
所以我们有几个星期的时间。15个月*4周/月=60周

find $DIR -type f -mtime +60w -exec rm {} \;

根据手册页:

 -mtime n[smhdw]
         If no units are specified, this primary evaluates to true if the difference between the file last modification time and the time find was started, rounded up to the next
         full 24-hour period, is n 24-hour periods.

         If units are specified, this primary evaluates to true if the difference between the file last modification time and the time find was started is exactly n units.  Please
         refer to the -atime primary description for information on supported time units.
然后,在
-atime

 -atime n[smhdw]
         If no units are specified, this primary evaluates to true if the difference between the file last access time and the time find was started, rounded up to the next full
         24-hour period, is n 24-hour periods.

         If units are specified, this primary evaluates to true if the difference between the file last access time and the time find was started is exactly n units.  Possible
         time units are as follows:

         s       second
         m       minute (60 seconds)
         h       hour (60 minutes)
         d       day (24 hours)
         w       week (7 days)

         Any number of units may be combined in one -atime argument, for example, ``-atime -1h30m''.  Units are probably only useful when used in conjunction with the + or - modi-
         fier.
所以我们有几个星期的时间。15个月*4周/月=60周

find $DIR -type f -mtime +60w -exec rm {} \;
使用450(=15*30)作为
-mtime
参数

find $DIR -type f -mtime +450 -exec rm {} \;
使用450(=15*30)作为
-mtime
参数

find $DIR -type f -mtime +450 -exec rm {} \;

一个有趣的可能性是:您可以
触摸
一个时间戳为
15个月前的tmp文件
,并将其与
查找
的(否定的)
-更新的
标志一起使用:

a=$(mktemp)
touch -d '15 months ago' -- "$a"
find "$DIR" -type f \! -newer "$a" -exec rm {} +
rm -- "$a"
当然,这是假设您的
触摸
查找
具有这些功能

如果
mktemp
有可能在目录
$DIR
的子目录中创建文件,那么它将变得非常混乱,因为“$a”引用的文件可能会在进程结束前被删除。在这种情况下,为了100%确定,请使用(否定)
-samefile
测试:

find "$DIR" -type f \! -newer "$a" \! -samefile "$a" -exec rm {} +
如果您的
find
支持,您当然可以使用
-delete
命令
find
。这将使:

a=$(mktemp)
touch -d '15 months ago' -- "$a"
find "$DIR" -type f \! -newer "$a" \! -samefile "$a" -delete
rm -- "$a"

一个有趣的可能性是:您可以
触摸
一个时间戳为
15个月前的tmp文件
,并将其与
查找
的(否定的)
-更新的
标志一起使用:

a=$(mktemp)
touch -d '15 months ago' -- "$a"
find "$DIR" -type f \! -newer "$a" -exec rm {} +
rm -- "$a"
当然,这是假设您的
触摸
查找
具有这些功能

如果
mktemp
有可能在目录
$DIR
的子目录中创建文件,那么它将变得非常混乱,因为“$a”引用的文件可能会在进程结束前被删除。在这种情况下,为了100%确定,请使用(否定)
-samefile
测试:

find "$DIR" -type f \! -newer "$a" \! -samefile "$a" -exec rm {} +
如果您的
find
支持,您当然可以使用
-delete
命令
find
。这将使:

a=$(mktemp)
touch -d '15 months ago' -- "$a"
find "$DIR" -type f \! -newer "$a" \! -samefile "$a" -delete
rm -- "$a"

专为此应用而设计。通常与cron一起使用。

专为此应用程序设计。通常与cron一起使用。

请参阅,但这是1个月。我没有看到一个类似于我想要的解决方案mtime+65w可能持续65周,基于1年是52周,一年的四分之一是13周,52+13=65请看,但这是1个月。我看不到一个类似于我想要的解决方案mtime+65w可能持续65周,基于此,1年是52周,因此一年的四分之一是13周,如果我使用ctime,52+13=65w+450相同吗?如果我使用ctime,是+450相同吗?如果我使用ctime,是+60w相同吗?请注意,15个月可能更像是65周。。。在此基础上,一年为52周,因此一年的一个季度为13周,52+13=65@user3409351:是的,格式相同。如手册页所示…如果我使用ctime,+60w是否相同?请注意,15个月可能更像是65周。。。在此基础上,一年为52周,因此一年的一个季度为13周,52+13=65@user3409351:是的,格式相同。如手册页所示…