Cómo eliminar archivos anteriores a X horas


Estoy escribiendo un script bash que necesita eliminar archivos antiguos.

Actualmente está implementado usando:

find $LOCATION -name $REQUIRED_FILES -type f -mtime +1 -delete

Esto eliminará los archivos anteriores a 1 día.

Sin embargo, ¿qué pasa si necesito una resolución más fina que 1 día, digamos como 6 horas de edad? ¿Hay una buena manera limpia de hacerlo, como si estuviera usando find y-mtime?

 160
Author: Tom Feiner, 2008-10-30

8 answers

Hace su find ¿tiene la opción -mmin? Eso puede permitirle probar el número de minutos desde la última modificación:

find $LOCATION -name $REQUIRED_FILES -type f -mmin +360 -delete

O tal vez mirar el uso tmpwatch para hacer el mismo trabajo. phjr también recomendado tmpreaper en los comentarios.

 243
Author: Paul Dixon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-09-25 21:04:31

Podría hacer este truco: crear un archivo hace 1 hora y usar el argumento -newer file.

(O use touch -t para crear dicho archivo).

 7
Author: xtofl,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-10-30 08:51:22

Para SunOS 5.10

 Example 6 Selecting a File Using 24-hour Mode


 The descriptions of -atime, -ctime, and -mtime use the  ter-
 minology n ``24-hour periods''. For example, a file accessed
 at 23:59 is selected by:


   example% find . -atime -1 -print




 at 00:01 the next day (less than 24 hours  later,  not  more
 than one day ago). The midnight boundary between days has no
 effect on the 24-hour calculation.
 1
Author: Rajeev Rumale,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-05-04 14:41:35

Aquí está el enfoque que funcionó para mí (y no veo que se use arriba)

$ find /path/to/the/folder -name *.* -mmin +59 -delete > /dev/null

Eliminar todos los archivos de más de 59 minutos dejando las carpetas intactas.

 1
Author: Axel Ronsin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-20 14:36:58

-mmin es para minutos.

Intente mirar la página de manual.

man find

Para más tipos.

 0
Author: GavinCattell,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-10-30 08:37:26

Si no tiene "- mmin "en su versión de "find", entonces "- mtime -0.041667 "se acerca bastante a" dentro de la última hora", por lo que en su caso, use:

-mtime +(X * 0.041667)

Entonces, si X significa 6 horas, entonces:

find . -mtime +0.25 -ls

Funciona porque 24 horas * 0.25 = 6 horas

 0
Author: Malcolm Boekhoff,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-06-30 02:25:37

Esto es lo que uno puede hacer por seguir el camino que @iconoclast se preguntaba en su comentario sobre otra respuesta.

Utilice crontab para el usuario o un /etc/crontab para crear el archivo /tmp/hour:

# m h dom mon dow user  command
0 * * * * root /usr/bin/touch /tmp/hour > /dev/null 2>&1

Y luego use esto para ejecutar su comando:

find /tmp/ -daystart -maxdepth 1 -not -newer /tmp/hour -type f -name "for_one_hour_files*" -exec do_something {} \;
 0
Author: satyr0909,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-11 14:15:54

find $PATH -name $log_prefix"*"$log_ext -mmin +$num_mins -exec rm -f {} \;

 0
Author: Eragonz91,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-13 18:57:14