Delete files older than ... with find
- Kategorie: Blog
- Veröffentlicht: Samstag, 02. Mai 2020 15:34
- Geschrieben von Martin Wilmes
- Zugriffe: 1327
I need a reminder on how to delete files that are older than a certain date so here it is:
sudo find . -not -newermt "2012-05-11" -type f -print0 | xargs -0 -L 100 sudo rm
This deletes every file in . that is older than 2012-05-11.
The two letters behind the "newer" option define that we are using the modification time of the files and that the parameter to the option is a date rather than a file name.
The xargs argument -L specifies that a maximum of 100 file names will be appended for a single execution of rm.
To also remove all empty directories that remain after this operation issue
sudo find . -type d -empty print0 | xargs -0 -L 1 sudo rmdir