delete old log files except the last one (alphanumerically sorted)

Marcus Junius Brutus

I have a number of log files in the form:

log.2014-02-19-10_24_22

I.e. log.YYYY-MM-DD-H24_MI_SS

The date that's part of the log file's name is when the log file was first created. So at any given moment in time I can have the following log files in my directory:

log.2014-02-19-10_18_54
log.2014-02-19-10_21_20
log.2014-02-19-10_23_11
etc.

Now I have a script that's invoked by a cronjob and that deletes "old" log files:

$ cat delete-old-rotated-logs 
#!/usr/bin/env bash
find /home/foo -maxdepth 1 -iname log\* -type f -mmin +1800 -exec rm {} \;

The problem I am facing is that sometimes the process that's logging has crashed so the "latest" log file also becomes "old" after some time (since no process is writing on it) and gets deleted, thus losing me the trace. How can I re-write the delete-old-rotated-logs script so that it deletes old files except the last one (or the last N) ? For the ordering one can use both the filename itself or the modification timestamp (more robust).

Graeme
find /home/foo -maxdepth 1 -iname log\* -type f -mmin +1800 |
  sort | head -n -1 | xargs rm

Or if you want to use mtime instead of the filename:

find /home/foo -maxdepth 1 -iname log\* -type f -mmin +1800 -exec ls -t {} + |
  tail -n +2 | xargs rm

From @Stephane's comments, a more robust approach would be to do:

IFS=$'\n'
set -f
rm $(
  find /home/foo -maxdepth 1 -iname log\* ! -name $'*\n*' -type f -mmin +1800 |
  sort | head -n -1 )

Or for POSIX shell (still requires GNU tools):

IFS='
'
ex_newline='*
*'
set -f
rm $(
  find /home/foo -maxdepth 1 -iname log\* ! -name "$ex_newline" -type f -mmin +1800 |
  sort | head -n -1 )

A single (robust) pipeline can be used with a recent version of GNU sed/sort (and GNU find as with all of the above):

find /home/foo -maxdepth 1 -iname log\* -type f -mmin +1800 -print0 |
  sort -z | sed -z '$d' | xargs -0 rm

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

list all files with extension .log except one

From Dev

Delete files of particular extension except one in Linux

From Dev

Simple PHP Delete All Files Except the 3 last days -

From Dev

MongoDB - How to delete expired documents except last one

From Dev

c# delete all files but not last one

From Dev

Delete All .log files execept for one

From Dev

In Linux terminal, how to delete all files in a directory except one or two

From Dev

Windows batch command to delete files from a folder except one file

From Dev

how delete All files except one in folder with php?

From Dev

Zsh: Delete all files except the one in highest lexicographic sorting order

From Dev

MS-DOS command to delete all files except one

From Dev

Cat files except one

From Dev

Scala mkString except the last one

From Dev

Delete files which have almost identical filenames EXCEPT last word (Windows)

From Dev

Delete all rows, except last 10 for each client that has related row(s) in the table in one query?

From Dev

Python - Delete all files EXCEPT for

From Dev

Powershell - delete old folders but not old files

From Dev

Delete all folders except one

From Dev

delete all directories except one

From Dev

Delete All Except One Python

From Dev

Camel route to delete old files

From Dev

how and when to delete old neo4j transaction log files

From Dev

Delete Records Except Last Three Records

From Dev

cmd delete all folders except last 5

From Dev

Delete unknown number of lines except the last line

From Dev

How can I run "tail -f" on all log files in the current directory — except for one?

From Dev

Why doesn't this FOR command delete all files except the one I want to keep?

From Dev

Batch command to delete everything (sub folders and files) from a folder except one file

From Dev

BAT file: Delete all files in directory older than 5 days except one file

Related Related

  1. 1

    list all files with extension .log except one

  2. 2

    Delete files of particular extension except one in Linux

  3. 3

    Simple PHP Delete All Files Except the 3 last days -

  4. 4

    MongoDB - How to delete expired documents except last one

  5. 5

    c# delete all files but not last one

  6. 6

    Delete All .log files execept for one

  7. 7

    In Linux terminal, how to delete all files in a directory except one or two

  8. 8

    Windows batch command to delete files from a folder except one file

  9. 9

    how delete All files except one in folder with php?

  10. 10

    Zsh: Delete all files except the one in highest lexicographic sorting order

  11. 11

    MS-DOS command to delete all files except one

  12. 12

    Cat files except one

  13. 13

    Scala mkString except the last one

  14. 14

    Delete files which have almost identical filenames EXCEPT last word (Windows)

  15. 15

    Delete all rows, except last 10 for each client that has related row(s) in the table in one query?

  16. 16

    Python - Delete all files EXCEPT for

  17. 17

    Powershell - delete old folders but not old files

  18. 18

    Delete all folders except one

  19. 19

    delete all directories except one

  20. 20

    Delete All Except One Python

  21. 21

    Camel route to delete old files

  22. 22

    how and when to delete old neo4j transaction log files

  23. 23

    Delete Records Except Last Three Records

  24. 24

    cmd delete all folders except last 5

  25. 25

    Delete unknown number of lines except the last line

  26. 26

    How can I run "tail -f" on all log files in the current directory — except for one?

  27. 27

    Why doesn't this FOR command delete all files except the one I want to keep?

  28. 28

    Batch command to delete everything (sub folders and files) from a folder except one file

  29. 29

    BAT file: Delete all files in directory older than 5 days except one file

HotTag

Archive