0

I am trying to use find command to delete some old file from backup folder but the find command return nothing, and so nothing is being removed! this is the code (find part), my system is ubuntu 18.04 LTS

find  -name "*.sql" -type f -mtime +30 

the result of find command

and the output of ls -l command is : the result of ls -l command

I googled and searched the web but did find nothing to solve the problem. any help appreciated.

VeRo
  • 956
  • 13
  • 27

1 Answers1

0

You are missing the starting point for your find command, in this case . because you already execute the command in the target directory:

find . -name "*.sql" -type f -mtime +30 

the rest can stay the same. First make sure it gives you the correct result and afterwards you can tack on the -exec to execute a command for each line of the result.

find . -name "*.sql" -type f -mtime +30 -exec rm '{}' ';'

You can usually find such answers on the UNIX stackexchange: How to execute ln on find results

Please see the comment from David in this particular case it might be a misunderstanding of the mtime parameter.

I have tested exactly the commands that were listed here, below you see my preparation and some multiple usage, you can see how the files show up as expected, every time the mtime value decreases:
VIRTUAL BOX UBUNTU LTS 18.04

which is no surprise, given the man page of the find command:
FIND MAN PAGE / -mtime PARAMETER

please check for any typos... this should work.

VeRo
  • 956
  • 13
  • 27
  • 1
    Actually the `[starting-point ...]` is an optional parameter, see [man 1 find](http://man7.org/linux/man-pages/man1/find.1.html). The problem may be the `+30` and the misunderstading the file must have been modified at least `31` 24-hour periods ago. – David C. Rankin Mar 08 '20 at 14:13
  • I tried your comments about stating-point, the result was the same. and also as David said, seems the problem is with +30 , to mention that we have a lot of sql files in this directory with ages up to 3 years, added up gradually. – Shahab Bohlooli Mar 13 '20 at 14:29
  • Even if I run find command as : "find -mtime +1"; it returns nothing, bu if I run as: "find -mtime -1" I get all files listed which is not what I need. – Shahab Bohlooli Mar 13 '20 at 14:42