How to delete all the files which are not created today

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












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 using



find . -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.







share|improve this question





















  • Linux only keeps creation times for ext4 and btrfs 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.
    – Nasir Riley
    May 2 at 16:32















up vote
1
down vote

favorite












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 using



find . -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.







share|improve this question





















  • Linux only keeps creation times for ext4 and btrfs 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.
    – Nasir Riley
    May 2 at 16:32













up vote
1
down vote

favorite









up vote
1
down vote

favorite











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 using



find . -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.







share|improve this question













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 using



find . -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.









share|improve this question












share|improve this question




share|improve this question








edited May 2 at 14:31









Jeff Schaller

31.1k846105




31.1k846105









asked May 2 at 12:36









NJMR

1083




1083











  • Linux only keeps creation times for ext4 and btrfs 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.
    – Nasir Riley
    May 2 at 16:32

















  • Linux only keeps creation times for ext4 and btrfs 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.
    – Nasir Riley
    May 2 at 16:32
















Linux only keeps creation times for ext4 and btrfs 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.
– Nasir Riley
May 2 at 16:32





Linux only keeps creation times for ext4 and btrfs 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.
– Nasir Riley
May 2 at 16:32











3 Answers
3






active

oldest

votes

















up vote
6
down vote



accepted










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.






share|improve this answer























  • Better use find . -type f ! -mtime -1 -print | xargs rm -f if there are many such files.
    – vonbrand
    May 3 at 12:00










  • @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.
    – Stéphane Chazelas
    May 3 at 12:25

















up vote
1
down vote













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





share|improve this answer



















  • 1




    Like for find -mtime +1, *(m+1) is for files whose age in days (rounded down) is strictly greater than 1 (so 2 or more, 48 hours or more). You need *(.m+0) for files at least 24 hour old (find -mtime +0). *(D.m+0) to also include hidden ones like find does. Some find implementations have an alternative syntax: find -mtime +1d that is based on exact times.
    – Stéphane Chazelas
    May 2 at 17:08







  • 1




    It 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 for find.
    – Stéphane Chazelas
    May 2 at 17:14











  • Thank 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
    May 2 at 17:22










  • Ahhh, I see -- the explicit day qualifier would have to come right after the m (or a or c). Thanks again!
    – Jeff Schaller
    May 2 at 19:39

















up vote
0
down vote













find has the parameter -not or ! which negates the one after and -delete to delete the files:



find . ! -mtime -1 -type f -delete



Note that -not is not POSIX compliant






share|improve this answer

















  • 1




    -delete is not POSIX either.
    – Stéphane Chazelas
    May 2 at 12:54










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f441314%2fhow-to-delete-all-the-files-which-are-not-created-today%23new-answer', 'question_page');

);

Post as a guest






























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
6
down vote



accepted










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.






share|improve this answer























  • Better use find . -type f ! -mtime -1 -print | xargs rm -f if there are many such files.
    – vonbrand
    May 3 at 12:00










  • @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.
    – Stéphane Chazelas
    May 3 at 12:25














up vote
6
down vote



accepted










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.






share|improve this answer























  • Better use find . -type f ! -mtime -1 -print | xargs rm -f if there are many such files.
    – vonbrand
    May 3 at 12:00










  • @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.
    – Stéphane Chazelas
    May 3 at 12:25












up vote
6
down vote



accepted







up vote
6
down vote



accepted






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.






share|improve this answer















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.







share|improve this answer















share|improve this answer



share|improve this answer








edited Jun 19 at 13:10









Jeff Schaller

31.1k846105




31.1k846105











answered May 2 at 12:48









Stéphane Chazelas

279k53514846




279k53514846











  • Better use find . -type f ! -mtime -1 -print | xargs rm -f if there are many such files.
    – vonbrand
    May 3 at 12:00










  • @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.
    – Stéphane Chazelas
    May 3 at 12:25
















  • Better use find . -type f ! -mtime -1 -print | xargs rm -f if there are many such files.
    – vonbrand
    May 3 at 12:00










  • @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.
    – Stéphane Chazelas
    May 3 at 12:25















Better use find . -type f ! -mtime -1 -print | xargs rm -f if there are many such files.
– vonbrand
May 3 at 12:00




Better use find . -type f ! -mtime -1 -print | xargs rm -f if there are many such files.
– vonbrand
May 3 at 12:00












@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.
– Stéphane Chazelas
May 3 at 12:25




@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.
– Stéphane Chazelas
May 3 at 12:25












up vote
1
down vote













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





share|improve this answer



















  • 1




    Like for find -mtime +1, *(m+1) is for files whose age in days (rounded down) is strictly greater than 1 (so 2 or more, 48 hours or more). You need *(.m+0) for files at least 24 hour old (find -mtime +0). *(D.m+0) to also include hidden ones like find does. Some find implementations have an alternative syntax: find -mtime +1d that is based on exact times.
    – Stéphane Chazelas
    May 2 at 17:08







  • 1




    It 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 for find.
    – Stéphane Chazelas
    May 2 at 17:14











  • Thank 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
    May 2 at 17:22










  • Ahhh, I see -- the explicit day qualifier would have to come right after the m (or a or c). Thanks again!
    – Jeff Schaller
    May 2 at 19:39














up vote
1
down vote













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





share|improve this answer



















  • 1




    Like for find -mtime +1, *(m+1) is for files whose age in days (rounded down) is strictly greater than 1 (so 2 or more, 48 hours or more). You need *(.m+0) for files at least 24 hour old (find -mtime +0). *(D.m+0) to also include hidden ones like find does. Some find implementations have an alternative syntax: find -mtime +1d that is based on exact times.
    – Stéphane Chazelas
    May 2 at 17:08







  • 1




    It 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 for find.
    – Stéphane Chazelas
    May 2 at 17:14











  • Thank 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
    May 2 at 17:22










  • Ahhh, I see -- the explicit day qualifier would have to come right after the m (or a or c). Thanks again!
    – Jeff Schaller
    May 2 at 19:39












up vote
1
down vote










up vote
1
down vote









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





share|improve this answer















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






share|improve this answer















share|improve this answer



share|improve this answer








edited May 2 at 19:30









Stéphane Chazelas

279k53514846




279k53514846











answered May 2 at 16:54









Jeff Schaller

31.1k846105




31.1k846105







  • 1




    Like for find -mtime +1, *(m+1) is for files whose age in days (rounded down) is strictly greater than 1 (so 2 or more, 48 hours or more). You need *(.m+0) for files at least 24 hour old (find -mtime +0). *(D.m+0) to also include hidden ones like find does. Some find implementations have an alternative syntax: find -mtime +1d that is based on exact times.
    – Stéphane Chazelas
    May 2 at 17:08







  • 1




    It 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 for find.
    – Stéphane Chazelas
    May 2 at 17:14











  • Thank 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
    May 2 at 17:22










  • Ahhh, I see -- the explicit day qualifier would have to come right after the m (or a or c). Thanks again!
    – Jeff Schaller
    May 2 at 19:39












  • 1




    Like for find -mtime +1, *(m+1) is for files whose age in days (rounded down) is strictly greater than 1 (so 2 or more, 48 hours or more). You need *(.m+0) for files at least 24 hour old (find -mtime +0). *(D.m+0) to also include hidden ones like find does. Some find implementations have an alternative syntax: find -mtime +1d that is based on exact times.
    – Stéphane Chazelas
    May 2 at 17:08







  • 1




    It 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 for find.
    – Stéphane Chazelas
    May 2 at 17:14











  • Thank 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
    May 2 at 17:22










  • Ahhh, I see -- the explicit day qualifier would have to come right after the m (or a or c). Thanks again!
    – Jeff Schaller
    May 2 at 19:39







1




1




Like for find -mtime +1, *(m+1) is for files whose age in days (rounded down) is strictly greater than 1 (so 2 or more, 48 hours or more). You need *(.m+0) for files at least 24 hour old (find -mtime +0). *(D.m+0) to also include hidden ones like find does. Some find implementations have an alternative syntax: find -mtime +1d that is based on exact times.
– Stéphane Chazelas
May 2 at 17:08





Like for find -mtime +1, *(m+1) is for files whose age in days (rounded down) is strictly greater than 1 (so 2 or more, 48 hours or more). You need *(.m+0) for files at least 24 hour old (find -mtime +0). *(D.m+0) to also include hidden ones like find does. Some find implementations have an alternative syntax: find -mtime +1d that is based on exact times.
– Stéphane Chazelas
May 2 at 17:08





1




1




It 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 for find.
– Stéphane Chazelas
May 2 at 17:14





It 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 for find.
– Stéphane Chazelas
May 2 at 17:14













Thank 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
May 2 at 17:22




Thank 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
May 2 at 17:22












Ahhh, I see -- the explicit day qualifier would have to come right after the m (or a or c). Thanks again!
– Jeff Schaller
May 2 at 19:39




Ahhh, I see -- the explicit day qualifier would have to come right after the m (or a or c). Thanks again!
– Jeff Schaller
May 2 at 19:39










up vote
0
down vote













find has the parameter -not or ! which negates the one after and -delete to delete the files:



find . ! -mtime -1 -type f -delete



Note that -not is not POSIX compliant






share|improve this answer

















  • 1




    -delete is not POSIX either.
    – Stéphane Chazelas
    May 2 at 12:54














up vote
0
down vote













find has the parameter -not or ! which negates the one after and -delete to delete the files:



find . ! -mtime -1 -type f -delete



Note that -not is not POSIX compliant






share|improve this answer

















  • 1




    -delete is not POSIX either.
    – Stéphane Chazelas
    May 2 at 12:54












up vote
0
down vote










up vote
0
down vote









find has the parameter -not or ! which negates the one after and -delete to delete the files:



find . ! -mtime -1 -type f -delete



Note that -not is not POSIX compliant






share|improve this answer













find has the parameter -not or ! which negates the one after and -delete to delete the files:



find . ! -mtime -1 -type f -delete



Note that -not is not POSIX compliant







share|improve this answer













share|improve this answer



share|improve this answer











answered May 2 at 12:49









Jakub Lucký

4985




4985







  • 1




    -delete is not POSIX either.
    – Stéphane Chazelas
    May 2 at 12:54












  • 1




    -delete is not POSIX either.
    – Stéphane Chazelas
    May 2 at 12:54







1




1




-delete is not POSIX either.
– Stéphane Chazelas
May 2 at 12:54




-delete is not POSIX either.
– Stéphane Chazelas
May 2 at 12:54












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f441314%2fhow-to-delete-all-the-files-which-are-not-created-today%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?