CREATIVE CHAOS   ▋ blog

Deleting Files With GNU find

PUBLISHED ON 19/10/2023 — EDITED ON 13/12/2023 — SYSOPS

Problem

I wanted to delete all files older than approximately one month from a mounter GlusterFS volume at /ftp. The server is RHEL8.

There was an old script that worked on the old environment, but it broke here.

Testing

This worked, find displayed all the files that I was searching for:

find /ftp \( -daystart -mtime +35 -or -daystart -atime +35 \) -type f -not -name '*.sh' -not -name '*.log' -not -name '*.txt'

but deleting them, would not work:

find /ftp \( -daystart -mtime +35 -or -daystart -atime +35 \) -type f -not -name '*.sh' -not -name '*.log' -not -name '*.txt' -print -delete

I just got an empty output.

Testing it, it worked if I ran this on second to last directory in the tree.

To test a bit more, I have spun up a Fedora Kionite virtual machine. Where all worked as intended.

Differences are x.1 in version between the two of them, all enabled features are the same.

[root@FedoraKionite ftp]# find --version
find (GNU findutils) 4.9.0
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Eric B. Decker, James Youngman, and Kevin Dalley.
Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION SELINUX FTS(FTS_CWDFD) CBO(level=2) 
[root@RHEL8 ftp]# find --version
find (GNU findutils) 4.8.0
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Eric B. Decker, James Youngman, and Kevin Dalley.
Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION SELINUX FTS(FTS_CWDFD) CBO(level=2) 

Nothing special in the changelog.

Solution

I have solved the issue by using -exec rm -v {} \; instead of -delete.

find out \( -daystart -mtime +35 -or -daystart -atime +35 \) -type f -not -name '*.sh' -not -name '*.log' -not -name '*.txt' -exec rm -v {} \;

Why!?

But the question remains, why?