How to delete all the files which are not created today
Solution 1
find . -type f -mtime +0 -exec rm -f {} +
or
find . -type f ! -mtime -1 -exec rm -f {} +
Would remove the regular files whose content has been last modified more than 24 hours ago (-mtime +0
meaning: whose age in days (rounded down to an integer, days are 24 hours, or 86400 Unix epoch second duration) is strictly greater than 0).
Some find
implementations have a -delete
predicate which you can use in place of -exec rm -f {} +
which would make it safer and more efficient.
For files that have been last modified earlier than today 00:00:00, with GNU find
, you can add the -daystart
predicate. That will include the files that were last modified yesterday even if less than 24 hours ago.
With some find
implementations, you can also do:
find . ! -newermt 00:00:00 -delete
To delete files that have been last modified before (or at exactly) 00:00:00 today.
Solution 2
Using zsh, either natively or via zsh -c "..."
:
rm -f /path/to/folder/*(.m+0) # for that directory only
rm -f /path/to/folder/**/*(.m+0) # recursively
The parens (
... )
creates a zsh "glob qualifier". Inside there, a dot .
specifies plain files (similar to find's -type f
) and the m+0
requires that the file have a modification time that is strictly more than zero days ago, after truncating down to whole days — 23 hours is 0 days; 25 hours would be 1 day.
To even more closely match find
's default behavior of finding/matching "hidden" files (that start with a dot), add the capital D qualifier:
rm -f /path/to/folder/*(D.m+0) # for that directory only
rm -f /path/to/folder/**/*(D.m+0) # recursively
Related videos on Youtube
NJMR
Updated on September 18, 2022Comments
-
NJMR almost 2 years
I want to
delete all
the files in a folder which are not created today. I know how to get the list of files which are created today usingfind . -type f -mtime -1
But, I am not getting how to get the list of all files which are not created today. Basically I have to find if there are files with old timestamp except today in a folder. If present I have to delete only the old files.
-
Nasir Riley about 6 yearsLinux only keeps creation times for
ext4
andbtrfs
as far as I know.-mtime
gives the last time that the file was modified in any way including the contents, ownership, permissions, and name changes so neither your command nor those in the answers will delete files which weren't created today. It will delete files which weren't modified today.
-
-
Stéphane Chazelas about 6 years
-delete
is not POSIX either. -
Stéphane Chazelas about 6 yearsIt can be confusing, but the idea is that time is split into one-day chunks.
*(m0)
is the files modified between 1 day ago and now.*(m1)
for the ones between 2 days ago and 1 day ago.*(m+1)
is for files older than 2 days ago. Like forfind
. -
Jeff Schaller about 6 yearsThank you for the clarification, Stéphane! My tests were too coarse to catch the difference. Still forcing myself to become familiar with zsh!
-
Jeff Schaller about 6 yearsAhhh, I see -- the explicit
d
ay qualifier would have to come right after them
(ora
orc
). Thanks again! -
vonbrand about 6 yearsBetter use
find . -type f ! -mtime -1 -print | xargs rm -f
if there are many such files. -
Stéphane Chazelas about 6 years@vonbrand, no, that has no benefit here over the
-exec {} +
syntax and breaks for file paths that contain blanks or newlines or quotes or backslash.