0

I have been struggling with this one for quite a few days now. It seems simple enough but I cannot seem to find the right command or arguments needed to accomplish this (seemingly) simple task. I need to delete wildcarded files in a directory older than x days. I have tried the following to no avail and I am wondering if the wildcard is possibly the problem.

find /path/to/files/ -name file_* -mtime +45 -exec rm '{}' +
find /path/to/files/ -name file_* -mtime +45 -exec rm {} ;\
find /path/to/files/ -name file_* -mtime +45 | xargs rm

The find works fine, it lists the correct files. It's the deletion that is not working.

Kara
  • 6,115
  • 16
  • 50
  • 57
oitson13
  • 13
  • 1
  • 1
  • 5

3 Answers3

1

You need to pass the wildcard to findby protecting it from shell expansion and despecialized the final ;:

find /path/to/files/ -name "file_*" -mtime +45 -exec rm {} \;
Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
0

I realized the following code WAS working properly for me:

find /path/to/files/ -name file_* -mtime +45 -exec rm '{}' +
oitson13
  • 13
  • 1
  • 1
  • 5
  • Only if you run it in a directory where the wildcard doesn't match anything. The correct and robust solution is to quote the wildcard, like in @Jean-Baptiste's answer. – tripleee May 20 '21 at 13:17
0

For AIX 6.1, there is a slight change needed. Verbose explanation included (fair warning)

find . /path/to/files/ -name "Files*.*" -mtime +45 -exec

That dot is needed. For the "Files*." you will use your names. My folder is a custom report folder and the date of the file is included in the name. But all the reports start with "Daily". So I have "Daily.". Each day we generate 2 reports. The specific report name is sandwiched between "Daily" and the date it was generated in the file name. So Daily.* gets them all. I have been unsuccessful in using ".". This may be specific to AIX 6.1, which is what we are using. AIX commands can vary by version.

Good luck!

Dan
  • 1