LINUX - find - Finding Selecting Deleting files older or younger then X days

SELECT FILE LESS or MORE THEN x DAYS ON LINUX
##############################################

X in this article will be the magical number 5.

Time and time again I let people know that the 'find' command in linux, is like a very advantageous file "selection" tool. Here you will see how it works. We get to select all files less then 5 or more then 5 days old (in the current folder, or recuresivly thru every folder) - and if we want we can operate on our selection with -exec, and do a delete with '-exec rm'.


MORE THAN 5 DAYS OLD
=====================

RECURSIVELY:
shopt -s dotglob
find /path/to/files/* -type f -mtime +5 
---OR---
find /path/to/files/ -type f -mtime +5 

shopt -u dotglob

NOT RECURSIVE - ONLY IN MENTIONED FOLDER:
shopt -s dotglob
find /path/to/files/* -type f -maxdepth 0 -mtime +5
---OR---
find /path/to/files/ -type f -maxdepth 1 -mtime +5
shopt -u dotglob


LESS THAN 5 DAYS OLD
====================

RECURSIVELY:
shopt -s dotglob
find /path/to/files/* -type f -mtime -5 
---OR---
find /path/to/files/ -type f -mtime -5 
shopt -u dotglob

NOT RECURSIVE - ONLY IN MENTIONED FOLDER:
shopt -s dotglob
find /path/to/files/* -type f -maxdepth 0 -mtime -5
---OR---
find /path/to/files/ -type f -maxdepth 1 -mtime -5
shopt -u dotglob


NOTES:
#######

WANT FOLDERS AS WELL?
======================

Remove the '-type f' part
Want folders only change '-type f' to '-type d'

MTIME
======
Older then 5 days use -mtime +5, for less then 5 days use -mtime -5

WHATS DOTGLOB FOR
===================
Dont forget to have the * after the path in find.
But * misses '.hidden' files, so enable dotglob shell option with 'shopt' command first, and dont forget to disable it. All of this is below.

EANBLES * TO FIND HIDDEN FILES: shopt -s dotglob
DISABLES * FROM FINDING HIDDENS FILES: shopt -u dotglob

Important knowledge: 
The root of our search path in these examples is /path/to/files. And we tell it to look thru everything that * selects there /path/to/files/*
find will select all hidden files without "shopt -s dotglob".
However this part is hard to explain why it works like this, but here is the effect:
All of our commands without dotglob enabled, will find hidden files, but not in the root of the directory your searching thru
/path/to/files/.hiddenfile <-- this file will not be seen with find (unless you enable dotglob: shopt -s dotglob)
/path/to/files/randomfolder/.hiddenfile <-- this file will always be found (with or without dotglob enabled: 'shopt -s dotglob' OR 'shopt -u dotglob')
This is just the way that find searches thru the defined folder... We tell it to look thru everything in the folder with /path/to/files/* so * doesnt select hiddens by default

This dotglob doesnt just apply on files, it also applies on folder (yah. yah. I know folders are files in linux). Also if you are searching recursively and you have a hidden folder on the root of the search path, then you will need dotglob on to look thru it or.
/path/to/files/.hiddenfolder/any-random-file-hidden-or-not <-- this file or any other file in there will not be seen with find (unless you enable dotglob: shopt -s dotglob)
/path/to/files/randomfolder/.hiddenfolder/any-random-file-hidden-or-not <-- since the hidden folder is not on the root of the search path, it will get looked thru no matter what (with or without dotglob enabled: 'shopt -s dotglob' OR 'shopt -u dotglob')

WITHOUT * MUCH EASIER
========================
All that dotglob business confusing you? Well then just remove the star and change the functions to this format (note we change maxdepth to 1 instead of 0, we need to add 1, if we leave it at 0 it will find nothing.. ever..)

With maxdepth (increase it by 1)
find /path/to/files/ -type f -maxdepth 1 -mtime +5
find /path/to/files/ -type f -maxdepth 1 -mtime -5

No max-depth? Thats fine! It will still look thru hiddens files/folders:
find /path/to/files/ -type f -mtime -5
find /path/to/files/ -type f -mtime -5

EXEC TO DELETE FILES
=====================
To delete append this to the end of the command:
-exec rm {} \;

BEFORE DELETING: Run the find command without -exec rm {} \; to simply ensure you selected the correct files

(STEP 1) Shopt to delete hidden files as well, so that * selects hidden files
shopt -s dotglob

(STEP 2) Select which delete command you want
TO DELETE FILES OLDER THAN 5 DAYS - RECURSIVELY:
find /path/to/files/* -mtime +5 -exec rm {} \;
---or---
find /path/to/files/ -mtime +5 -exec rm {} \;

OR

TO DELETE FILES YOUNGER THAN 5 DAYS - RECURSIVELY:
find /path/to/files/* -mtime -5 -exec rm {} \;
---or---
find /path/to/files/ -mtime -5 -exec rm {} \;

OR

TO DELETE FILES OLDER THAN 5 DAYS - NONE-RECURSIVELY:
find /path/to/files/* -maxdepth 0 -mtime +5 -exec rm {} \;
---or---
find /path/to/files/ -maxdepth 1 -mtime +5 -exec rm {} \;

OR

TO DELETE FILES YOUNGER THAN 5 DAYS - NONE-RECURSIVELY:
find /path/to/files/* -maxdepth 0 -mtime -5 -exec rm {} \;
---or---
find /path/to/files/ -maxdepth 1 -mtime -5 -exec rm {} \;

(STEP 3) Undo the shopt command, to get back to normal mode where * doesnt select hidden files
shopt -s dotglob

MAX DEPTH NOTE
===============
NOTE: to only search the current folder (zero folders depth) use this:
-maxdepth 0

If would of used -maxdepth 1, then it would of searched 1 folder deep recursively.

SelectionFile type iconFile nameDescriptionSizeRevisionTimeUser
Comments