Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find files older than X days in bash and delete

Tags:

find

bash

I have a directory with a few TB of files. I'd like to delete every file in it that is older than 14 days.

I thought I would use find . -mtime +13 -delete. To make sure the command works as expected I ran find . -mtime +13 -exec /bin/ls -lh '{}' \; | grep '<today>'. The latter should return nothing, since files that were created/modified today should not be found by find using -mtime +13. To my surprise, however, find just spew out a list of all the files modified/created today!

like image 326
user3040975 Avatar asked Nov 27 '13 09:11

user3040975


People also ask

How do I find and delete files older than x days in Linux?

The switch -type f means we want to look for files only. The -mmin stands for the modification time, and +15 means we want files that were last modified 15 minutes ago or earlier. The action flag -delete asks find to delete all the files it finds.

How do I delete files older than x days?

To delete files older than 10 days in Windows 11 or Windows 10, you can use the ForFiles command. First, open the Command Prompt with administrator rights. Then, enter this command: ForFiles /p “folder-path” /s /d -10 /c “cmd /c del /q @file”. It will remove all the files older than 10 days only.

How do I remove 7 days old files in Linux?

Your command will look at the top level directory /var/log/mbackups and also descend into any subdirectories, deleting files that match the seven day criterion. It will not delete the directories themselves. For files older than 7 days, you need -mtime +6 (or '(' -mtime 7 -o -mtime +7 ')' ), not -mtime +7 .


1 Answers

find your/folder -type f -mtime +13 -exec rm {} \; 
like image 122
Mindx Avatar answered Oct 08 '22 20:10

Mindx