2

I have a script for creating backups and deleting old backups. It has this line:

 find . -type d -mtime +29 -user admin -name "20*" -exec rm -rf {} \;

This way, directories with backups older than 29 days are deleted. This works, directories are deleted, but this message is displayed every time:

find: `./2020-06-16-23-30': No such file or directory

(the date changes in the message)

Please tell me why this message appears and how to correct the script.

Using output redirection (>/dev/null 2>&1) is not suitable for me, because I would like to see messages about some real errors.

This is a directory listing with backups:

$ cd /volume1/Backup && ls -l
drwxr-xr-x  2 admin users       4096 Jun 19 23:30 2020-06-19-23-30
drwxr-xr-x  2 admin users       4096 Jun 20 23:30 2020-06-20-23-30
drwxr-xr-x  2 admin users       4096 Jun 21 23:30 2020-06-21-23-30
drwxr-xr-x  2 admin users       4096 Jun 22 23:30 2020-06-22-23-30
drwxr-xr-x  2 admin users       4096 Jun 23 23:30 2020-06-23-23-30
drwxr-xr-x  2 admin users       4096 Jun 24 23:30 2020-06-24-23-30
drwxr-xr-x  2 admin users       4096 Jun 25 23:30 2020-06-25-23-30
drwxr-xr-x  2 admin users       4096 Jun 26 23:30 2020-06-26-23-30
drwxr-xr-x  2 admin users       4096 Jun 27 23:30 2020-06-27-23-30
drwxr-xr-x  2 admin users       4096 Jun 28 23:30 2020-06-28-23-30
drwxr-xr-x  2 admin users       4096 Jun 29 23:30 2020-06-29-23-30
drwxr-xr-x  2 admin users       4096 Jun 30 23:30 2020-06-30-23-30
drwxr-xr-x  2 admin users       4096 Jul  1 23:30 2020-07-01-23-30
drwxr-xr-x  2 admin users       4096 Jul  2 23:30 2020-07-02-23-30
drwxr-xr-x  2 admin users       4096 Jul  3 23:30 2020-07-03-23-30
drwxr-xr-x  2 admin users       4096 Jul  4 23:30 2020-07-04-23-30
drwxr-xr-x  2 admin users       4096 Jul  5 23:30 2020-07-05-23-30
drwxr-xr-x  2 admin users       4096 Jul  6 23:30 2020-07-06-23-30
drwxr-xr-x  2 admin users       4096 Jul  7 23:30 2020-07-07-23-30
drwxr-xr-x  2 admin users       4096 Jul  8 23:30 2020-07-08-23-30
drwxr-xr-x  2 admin users       4096 Jul  9 23:30 2020-07-09-23-30
drwxr-xr-x  2 admin users       4096 Jul 10 23:30 2020-07-10-23-30
drwxr-xr-x  2 admin users       4096 Jul 11 23:30 2020-07-11-23-30
drwxr-xr-x  2 admin users       4096 Jul 12 23:30 2020-07-12-23-30
drwxr-xr-x  2 admin users       4096 Jul 13 23:30 2020-07-13-23-30
drwxr-xr-x  2 admin users       4096 Jul 14 23:30 2020-07-14-23-30
drwxr-xr-x  2 admin users       4096 Jul 15 23:30 2020-07-15-23-30
drwxr-xr-x  2 admin users       4096 Jul 16 23:30 2020-07-16-23-30
Evgeny
  • 21

1 Answers1

1

That looks like a race-condition:

Suppose you have a file /a/b/c and you run find /a -exec rm -rf {} \;

In some cases (not always) things will happen it this order

  1. find will notice that /a/b and /a/b/c exist and remember them both
  2. It will launch rm -rf /a/b which will delete both /a/b and /a/b/c
  3. It will not know that /a/b/c is gone, it will still try to delete this (non-existing) file and you will get that error.

Possible solutions:

  • What you could do: Use regexps to filter lines ending with No such file or directory out
  • What would probably be better: (Test it first by placing echo in front of rm -rf):
find . -type d -mtime +29 -user admin -name "20*" > /tmp/list_of_dirs
perl -ne 'system("rm -rf $_") < /tmp/list_of_dirs
  • What I myself would do: I would not consider this as a real problem. If rm would break on some dirs, you will probably notice it...
Garo
  • 2,059
  • 11
  • 16
  • 1
    A simple way is to just use find . -depth .... The error is not shown because the whole command tries to delete a file that is gone, but because find tries to descend into a directory after deleting it. -depth tells find to process the content of directories before the directories themselves. – fra-san Jul 17 '20 at 09:27
  • I have to admit that that kind of race condition is more likely. – Garo Jul 17 '20 at 09:36