Aufgabenplanung - Dateien löschen älter als x Tage

alexhell

Benutzer
Sehr erfahren
Mitglied seit
13. Mai 2021
Beiträge
2.603
Punkte für Reaktionen
758
Punkte
154
Ist doch beschrieben was der Unterschied ist bzw. wo es läuft. Man kann mal selber was lesen
 

Benares

Benutzer
Sehr erfahren
Mitglied seit
27. Sep 2008
Beiträge
12.310
Punkte für Reaktionen
2.870
Punkte
423
  • Like
Reaktionen: maxblank und alexhell

roads

Benutzer
Mitglied seit
08. Okt 2021
Beiträge
89
Punkte für Reaktionen
4
Punkte
8
Verstehe, dachte dies sei ein generelles Backupproblem das es für alle zu lösen gilt das man in diesem Faden dann nachlesen kann. Danke alexhell dann suche ich mal eines aus. Finde ich ein gut funktionierenden Code werde ich es nicht preisgeben um anderen den Spass nicht zu verderben. Kein Spoilern, versprochen.
 
  • Sad
Reaktionen: maxblank

luddi

Benutzer
Sehr erfahren
Mitglied seit
05. Sep 2012
Beiträge
3.242
Punkte für Reaktionen
586
Punkte
174
Den Befehl den ich seit langem im Einsatz habe sieht wie folgt aus.

Bash:
find <PATH_TO_DIR> -maxdepth 1 -type f -printf "%T@ %p\n" | sort -n | head -n -5 | awk {'print $NF'}  | xargs -d '\n' rm -f --

In diesem Beispiel werden die letzten (neuesten) 5 Dateien beibehalten und alle älteren gelöscht.
Möchte man anstelle von 5 Dateien eine andere Anzahl beibehalten so muss lediglich X in head -n -X angepasst werden.

Hier ein Beispiel:
Nehmen wir an es liegen 20 Dateien in einem Verzeichnis.

Bash:
# Erstellen von 20 Dateien
touch file_{1..20}

# Verzeichnis auflisten
ls -lav
----------+ 1 root root   0 Oct  7 00:11 file_1
----------+ 1 root root   0 Oct  7 00:11 file_2
----------+ 1 root root   0 Oct  7 00:11 file_3
----------+ 1 root root   0 Oct  7 00:11 file_4
----------+ 1 root root   0 Oct  7 00:11 file_5
----------+ 1 root root   0 Oct  7 00:11 file_6
----------+ 1 root root   0 Oct  7 00:11 file_7
----------+ 1 root root   0 Oct  7 00:11 file_8
----------+ 1 root root   0 Oct  7 00:11 file_9
----------+ 1 root root   0 Oct  7 00:11 file_10
----------+ 1 root root   0 Oct  7 00:11 file_11
----------+ 1 root root   0 Oct  7 00:11 file_12
----------+ 1 root root   0 Oct  7 00:11 file_13
----------+ 1 root root   0 Oct  7 00:11 file_14
----------+ 1 root root   0 Oct  7 00:11 file_15
----------+ 1 root root   0 Oct  7 00:11 file_16
----------+ 1 root root   0 Oct  7 00:11 file_17
----------+ 1 root root   0 Oct  7 00:11 file_18
----------+ 1 root root   0 Oct  7 00:11 file_19
----------+ 1 root root   0 Oct  7 00:11 file_20

Die Dateien wurden der Reihe nach aufsteigend von 1 bis 20 angelegt. Somit ist "file_1" die älteste und "file_20" die jüngste Datei.
Will man die jüngsten 5 Dateien beibehalten so sind es in diesem Beispiel die Dateien "file_16" bis "file_20".

Zunächst kontrolliert man mit dem ersten Teil des Befehls, ob die zu löschenden Dateien richtig nach unserer Vorstellung identifiziert werden ohne den Löschbefehl auszuführen.
Bash:
find ./ -maxdepth 1 -type f -printf "%T@ %p\n" | sort -n | head -n -5 | awk {'print $NF'}

# Die Ausgabe sollte wie folgt sein:
./file_1
./file_2
./file_3
./file_4
./file_5
./file_10
./file_6
./file_7
./file_8
./file_9
./file_11
./file_12
./file_13
./file_14
./file_15

Somit wissen wir, dass dies die ältesten Dateien sind, die auch gelöscht werden dürfen und fügen nun xargs -d '\n' rm -f -- hinzu.
Bash:
find ./ -maxdepth 1 -type f -printf "%T@ %p\n" | sort -n | head -n -5 | awk {'print $NF'} | xargs -d '\n' rm -f

# Auflisten des Verzeichnis um die übrigen Dateien anzuzeigen

ls -lav
d---------+ 1 root root  70 Oct  7 00:23 .
d---------+ 1 root root 456 Oct  7 00:10 ..
----------+ 1 root root   0 Oct  7 00:11 file_16
----------+ 1 root root   0 Oct  7 00:11 file_17
----------+ 1 root root   0 Oct  7 00:11 file_18
----------+ 1 root root   0 Oct  7 00:11 file_19
----------+ 1 root root   0 Oct  7 00:11 file_20

Wir fügen weitere 20 Dateien hinzu und überprüfen erneut den Befehl:
Bash:
touch file_{21..40}
ls -lav
d---------+ 1 root root 350 Oct  7 00:24 .
d---------+ 1 root root 456 Oct  7 00:10 ..
----------+ 1 root root   0 Oct  7 00:11 file_16
----------+ 1 root root   0 Oct  7 00:11 file_17
----------+ 1 root root   0 Oct  7 00:11 file_18
----------+ 1 root root   0 Oct  7 00:11 file_19
----------+ 1 root root   0 Oct  7 00:11 file_20
----------+ 1 root root   0 Oct  7 00:24 file_21
----------+ 1 root root   0 Oct  7 00:24 file_22
----------+ 1 root root   0 Oct  7 00:24 file_23
----------+ 1 root root   0 Oct  7 00:24 file_24
----------+ 1 root root   0 Oct  7 00:24 file_25
----------+ 1 root root   0 Oct  7 00:24 file_26
----------+ 1 root root   0 Oct  7 00:24 file_27
----------+ 1 root root   0 Oct  7 00:24 file_28
----------+ 1 root root   0 Oct  7 00:24 file_29
----------+ 1 root root   0 Oct  7 00:24 file_30
----------+ 1 root root   0 Oct  7 00:24 file_31
----------+ 1 root root   0 Oct  7 00:24 file_32
----------+ 1 root root   0 Oct  7 00:24 file_33
----------+ 1 root root   0 Oct  7 00:24 file_34
----------+ 1 root root   0 Oct  7 00:24 file_35
----------+ 1 root root   0 Oct  7 00:24 file_36
----------+ 1 root root   0 Oct  7 00:24 file_37
----------+ 1 root root   0 Oct  7 00:24 file_38
----------+ 1 root root   0 Oct  7 00:24 file_39
----------+ 1 root root   0 Oct  7 00:24 file_40


# Befehl um die jüngsten 5 Dateien zu behalten
find ./ -maxdepth 1 -type f -printf "%T@ %p\n" | sort -n | head -n -5 | awk {'print $NF'} | xargs -d '\n' rm -f

# Auflisten der übrigen Dateien
ls -lav
d---------+ 1 root root  70 Oct  7 00:27 .
d---------+ 1 root root 456 Oct  7 00:10 ..
----------+ 1 root root   0 Oct  7 00:24 file_36
----------+ 1 root root   0 Oct  7 00:24 file_37
----------+ 1 root root   0 Oct  7 00:24 file_38
----------+ 1 root root   0 Oct  7 00:24 file_39
----------+ 1 root root   0 Oct  7 00:24 file_40

Ich hoffe ich habe die Anforderung korrekt verstanden und es entspricht genau dem was gesucht wurde.
 

roads

Benutzer
Mitglied seit
08. Okt 2021
Beiträge
89
Punkte für Reaktionen
4
Punkte
8
Ist das deine Erwartungshaltung?

Ja genau wie es gerade passiert ist. Jemand hat es im Einsatz und postet die Zeile. Das Rad muss nicht von jedem neu erfunden werden zumal das testen mit Löschungen auch gefährlich ist.
Vielen lieben Dank luddi, ich bin mir sicher damit hilfst du vielen weiter. (y)(y)
 


 

Kaffeautomat

Wenn du das Forum hilfreich findest oder uns unterstützen möchtest, dann gib uns doch einfach einen Kaffee aus.

Als Dankeschön schalten wir deinen Account werbefrei.

:coffee:

Hier gehts zum Kaffeeautomat