Как удалить все файлы, которые в последний раз изменялись за один и более месяцев до того, как

Я нашел такое решение:

find . -type f ! -newermt '04/29/2018 16:00:00' -exec rm -f {} \;

Но мне нужны динамические даты. Я имею в виду не одну фиксированную дату, но я буду использовать этот скрипт с cron каждый день, и он должен удалять все файлы, измененные за один месяц до (и более).

Спасибо!

2
задан 22 September 2020 в 11:19

3 ответа

Согласно man find , вы можете использовать строки, которые распознает команда date .

-newerXY reference

       Succeeds  if  timestamp  X  of the file being considered is 
       newer than timestamp Y of the file reference.  The letters X 
       and Y can be any of the following letters:

       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.

Согласно man date , вы можете использовать удобочитаемые фразы, например «месяц назад» . Для получения дополнительной информации см. Руководство GNU Coreutils: Формат ввода даты .

DATE STRING
       The --date=STRING is a mostly free format human readable date
       string  such  as  "Sun,  29  Feb  2004 16:21:42  -0800"  or  
       "2004-02-29 16:21:42" or even "next Thursday".  A date string 
       may contain items indicating calendar date, time of day, time 
       zone, day of week, relative time, relative date, and numbers.   
       An  empty  string indicates the beginning of the day.  The date 
       string format is more complex than is easily documented here but 
       is fully described in the info documentation.

Как bac0n notes , вас могут заинтересовать find - удалить флаг:

-delete
       Delete  files;  true if removal succeeded.  If the 
       removal failed, an error message is issued. If -delete 
       fails, find's exit status will be nonzero  (when  it  
       eventually  exits).   Use  of -delete automatically turns 
       on the `-depth' option.

       Warnings:  Don't  forget  that the find command line is 
       evaluated as an expression, so putting -delete first will 
       make find try to delete everything below the starting 
       points you specified. When testing a find command line 
       that you later intend to use with -delete, you should 
       explicitly specify -depth in order to avoid later 
       surprises.  Because -delete  implies  -depth,  you cannot 
       usefully use -prune and -delete together.

       Together  with  the -ignore_readdir_race option, find 
       will ignore errors of the -delete action in the case the 
       file has disappeared since the parent directory was read: 
       it will  not  output an error diagnostic, and the return 
       code of the -delete action will be true.

Таким образом, необходимая вам команда может выглядеть примерно так:

find -type f \! -newermt "last month" -delete
6
ответ дан 4 January 2021 в 08:22

Мое решение:

find . -type f ! -newermt "$(date -d 'now - 1 month' +'%D %T')" -exec rm -f {} \;
0
ответ дан 4 January 2021 в 08:22

Вы можете использовать

find . -type f -mtime +30 -exec rm -f {} \;

или более простой вариант

find . -type f -mtime +30 -delete

, который удалит все файлы, измененные более 30 дней назад.

2
ответ дан 4 January 2021 в 08:22

Другие вопросы по тегам:

Похожие вопросы: