Linux – Delete Files Or Directories Older Than x Days


Hits: 165  

First find files older than X Days

-mtime n => File was last modified n*24 hours ago.

-atime n => File was last accessed n*24 hours ago.

-ctime n => File status was last changed n*24 hours ago.

+n More than n.
n Exactly n.
-n Less than n.

# files modified greater than 24 hours ago
find /location/ -mtime +0

# files modified between now and 1 day ago
find /location/ -mtime 0

# files modified less than 1 day ago
find /location/ -mtime -1

# files modified between 24 and 48 hours ago
find /location/ -mtime 1

# files modified more than 48 hours ago
find /location/ -mtime +1

-type f => For Files
-type d => For Directories

Now delete all files modified time greater than 24 hours ago (keep today’s files)
# find /var/www/html/recorded/files/* -type f -mtime +0 -exec rm {} \;