I have this script that I thougth only deleted stuff more than 90 days old but after I ran it, it deleted pretty much everything on my backup drive. Thankfuly I have another backup drive so I did not lose my backup but I need to fix this so it only deletes things older than 90 days.

find /Volumes/Backup/* -type d -mtime +90 -exec rm -R {} \;

Thanks

Recommended Answers

All 3 Replies

Hello,

I see three things.
First the -type d is looking for directories not files.
Second, you don't need the asterisk on the directory
Third, I normally run a test command using* ls *instead of rm to make sure the output I am getting is what I intended.

find /Volumes/Backup/ -type f -mtime +90 -exec /bin/ls -la {} \;

rch1231,

Thanks for the reply.

I think this clarified what I am trying to do. My backup creates folders for the days of the month, Day01, Day02, Day03 ... Day31. Since all of these directories were created more than 90 days ago so they got deleted. Correct? I don't want to delete the folders just any files on the entire Backup drive that are older than 90 days. Is this what the modified script you show in your post will do?

Also, I notices at the end of the command you removed my "-R" and replaced it with "-la". What will this do?

Sorry if these are novice questions. I am aking over for a previous guy and am new to scripting though I am trying to learn.

Thanks.

Hello,

Sorry to take so long responding but it has been busy at work.
The find command I gave you looks for files older than 90 days not directories. Linux does not update the directory date when files are changed or copied into a directory so the directories you created will have their original date instead of the latest date. That is why all of your files were deleted.

The reason I replaced the rm with ls -la is to show you what results your find is getting. I have made it a rule to run any find that deletes files using the ls -la command first so I can verify what the rm command is going to delete. Peraonally I also am very nervious about running a rm -R via a script. For example what if your find command somehow detected the .. directory reference as a directory older than 90 days and therefor did an rm -R .. and was able to get up the the / directory. It could conceivablly deleted every file on the system.

I also suggest that you run the find and redirect the output to a file and keep the file for a few days so you can verify what you deleted.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.