Deleting files older than x number of years or days
up vote
0
down vote
favorite
I huge uploads folder and tried to delete the old files eg. files older than 3 years or files added before a certain date but for some reason it ends up deleting both new and old files.
Here are the commands I tried so far
Command 1
find /home/user/uploads -maxdepth 1 -type f ! -newermt "2018-10-14 00:00:00" -exec rm -v {} ;
This command removes everything ignoring the with ! -newermt or just - newermt
Command 2
find /home/user/uploads -maxdepth 1 -mtime +1095 -type f -print -exec rm -i {} ;
It returned no result
Command 3
find /home/user/uploads -type f ! -newermt "2018-10-14 00:00:00" | xargs rm
This returned an error "xargs: unmatched single quote by default quotes are special to xargs unless you use the -o option "
I can't figure out what exactly is wrong with the commands.. I'm running on a CentOS system.
shell unix xargs
add a comment |
up vote
0
down vote
favorite
I huge uploads folder and tried to delete the old files eg. files older than 3 years or files added before a certain date but for some reason it ends up deleting both new and old files.
Here are the commands I tried so far
Command 1
find /home/user/uploads -maxdepth 1 -type f ! -newermt "2018-10-14 00:00:00" -exec rm -v {} ;
This command removes everything ignoring the with ! -newermt or just - newermt
Command 2
find /home/user/uploads -maxdepth 1 -mtime +1095 -type f -print -exec rm -i {} ;
It returned no result
Command 3
find /home/user/uploads -type f ! -newermt "2018-10-14 00:00:00" | xargs rm
This returned an error "xargs: unmatched single quote by default quotes are special to xargs unless you use the -o option "
I can't figure out what exactly is wrong with the commands.. I'm running on a CentOS system.
shell unix xargs
find /home/user/uploads -maxdepth 1 -type f ! -newermt "2018-10-14 00:00:00"
is correct for matching only files older than2018-10-14 00:00:00"
. You say "This command removes everything" -- were there files newer than that deleted?
– David C. Rankin
Nov 11 at 10:58
yes... it removed new files that were created after this date, instead of files created before this date
– iOflower
Nov 11 at 11:05
OK, hold on. Let me come up with a test for you.
– David C. Rankin
Nov 11 at 11:08
Make adatetst
dir, e.g.md datetst && cd datetst
create a file for every day of Oct. 2018, e.gfor i in {1..31}; do touch -d "2018-10-$i" file_$i; done
, Now let'sfind . -maxdepth 1 -type f ! -newermt "2018-10-16" | sort
and see only files corresponding to days1-16
are selected as not newer than"2018-10-16"
which is right. Does you test given different results?
– David C. Rankin
Nov 11 at 11:18
Yes, i got the results I wanted after testing your code.. it sorted out the files created before 2018-10-16. ! -newermt sorted files before and just -newermt sorted the files modified after.. I'm going to try this with my command and post the results..
– iOflower
Nov 11 at 11:35
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I huge uploads folder and tried to delete the old files eg. files older than 3 years or files added before a certain date but for some reason it ends up deleting both new and old files.
Here are the commands I tried so far
Command 1
find /home/user/uploads -maxdepth 1 -type f ! -newermt "2018-10-14 00:00:00" -exec rm -v {} ;
This command removes everything ignoring the with ! -newermt or just - newermt
Command 2
find /home/user/uploads -maxdepth 1 -mtime +1095 -type f -print -exec rm -i {} ;
It returned no result
Command 3
find /home/user/uploads -type f ! -newermt "2018-10-14 00:00:00" | xargs rm
This returned an error "xargs: unmatched single quote by default quotes are special to xargs unless you use the -o option "
I can't figure out what exactly is wrong with the commands.. I'm running on a CentOS system.
shell unix xargs
I huge uploads folder and tried to delete the old files eg. files older than 3 years or files added before a certain date but for some reason it ends up deleting both new and old files.
Here are the commands I tried so far
Command 1
find /home/user/uploads -maxdepth 1 -type f ! -newermt "2018-10-14 00:00:00" -exec rm -v {} ;
This command removes everything ignoring the with ! -newermt or just - newermt
Command 2
find /home/user/uploads -maxdepth 1 -mtime +1095 -type f -print -exec rm -i {} ;
It returned no result
Command 3
find /home/user/uploads -type f ! -newermt "2018-10-14 00:00:00" | xargs rm
This returned an error "xargs: unmatched single quote by default quotes are special to xargs unless you use the -o option "
I can't figure out what exactly is wrong with the commands.. I'm running on a CentOS system.
shell unix xargs
shell unix xargs
asked Nov 11 at 10:44
iOflower
248
248
find /home/user/uploads -maxdepth 1 -type f ! -newermt "2018-10-14 00:00:00"
is correct for matching only files older than2018-10-14 00:00:00"
. You say "This command removes everything" -- were there files newer than that deleted?
– David C. Rankin
Nov 11 at 10:58
yes... it removed new files that were created after this date, instead of files created before this date
– iOflower
Nov 11 at 11:05
OK, hold on. Let me come up with a test for you.
– David C. Rankin
Nov 11 at 11:08
Make adatetst
dir, e.g.md datetst && cd datetst
create a file for every day of Oct. 2018, e.gfor i in {1..31}; do touch -d "2018-10-$i" file_$i; done
, Now let'sfind . -maxdepth 1 -type f ! -newermt "2018-10-16" | sort
and see only files corresponding to days1-16
are selected as not newer than"2018-10-16"
which is right. Does you test given different results?
– David C. Rankin
Nov 11 at 11:18
Yes, i got the results I wanted after testing your code.. it sorted out the files created before 2018-10-16. ! -newermt sorted files before and just -newermt sorted the files modified after.. I'm going to try this with my command and post the results..
– iOflower
Nov 11 at 11:35
add a comment |
find /home/user/uploads -maxdepth 1 -type f ! -newermt "2018-10-14 00:00:00"
is correct for matching only files older than2018-10-14 00:00:00"
. You say "This command removes everything" -- were there files newer than that deleted?
– David C. Rankin
Nov 11 at 10:58
yes... it removed new files that were created after this date, instead of files created before this date
– iOflower
Nov 11 at 11:05
OK, hold on. Let me come up with a test for you.
– David C. Rankin
Nov 11 at 11:08
Make adatetst
dir, e.g.md datetst && cd datetst
create a file for every day of Oct. 2018, e.gfor i in {1..31}; do touch -d "2018-10-$i" file_$i; done
, Now let'sfind . -maxdepth 1 -type f ! -newermt "2018-10-16" | sort
and see only files corresponding to days1-16
are selected as not newer than"2018-10-16"
which is right. Does you test given different results?
– David C. Rankin
Nov 11 at 11:18
Yes, i got the results I wanted after testing your code.. it sorted out the files created before 2018-10-16. ! -newermt sorted files before and just -newermt sorted the files modified after.. I'm going to try this with my command and post the results..
– iOflower
Nov 11 at 11:35
find /home/user/uploads -maxdepth 1 -type f ! -newermt "2018-10-14 00:00:00"
is correct for matching only files older than 2018-10-14 00:00:00"
. You say "This command removes everything" -- were there files newer than that deleted?– David C. Rankin
Nov 11 at 10:58
find /home/user/uploads -maxdepth 1 -type f ! -newermt "2018-10-14 00:00:00"
is correct for matching only files older than 2018-10-14 00:00:00"
. You say "This command removes everything" -- were there files newer than that deleted?– David C. Rankin
Nov 11 at 10:58
yes... it removed new files that were created after this date, instead of files created before this date
– iOflower
Nov 11 at 11:05
yes... it removed new files that were created after this date, instead of files created before this date
– iOflower
Nov 11 at 11:05
OK, hold on. Let me come up with a test for you.
– David C. Rankin
Nov 11 at 11:08
OK, hold on. Let me come up with a test for you.
– David C. Rankin
Nov 11 at 11:08
Make a
datetst
dir, e.g. md datetst && cd datetst
create a file for every day of Oct. 2018, e.g for i in {1..31}; do touch -d "2018-10-$i" file_$i; done
, Now let's find . -maxdepth 1 -type f ! -newermt "2018-10-16" | sort
and see only files corresponding to days 1-16
are selected as not newer than "2018-10-16"
which is right. Does you test given different results?– David C. Rankin
Nov 11 at 11:18
Make a
datetst
dir, e.g. md datetst && cd datetst
create a file for every day of Oct. 2018, e.g for i in {1..31}; do touch -d "2018-10-$i" file_$i; done
, Now let's find . -maxdepth 1 -type f ! -newermt "2018-10-16" | sort
and see only files corresponding to days 1-16
are selected as not newer than "2018-10-16"
which is right. Does you test given different results?– David C. Rankin
Nov 11 at 11:18
Yes, i got the results I wanted after testing your code.. it sorted out the files created before 2018-10-16. ! -newermt sorted files before and just -newermt sorted the files modified after.. I'm going to try this with my command and post the results..
– iOflower
Nov 11 at 11:35
Yes, i got the results I wanted after testing your code.. it sorted out the files created before 2018-10-16. ! -newermt sorted files before and just -newermt sorted the files modified after.. I'm going to try this with my command and post the results..
– iOflower
Nov 11 at 11:35
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
So after playing with the commands for a while this is the command that worked.
find . -maxdepth 1 -type f ! -newermt "2018-10-16" -exec rm -v {} ;
These flags also helped incase you're come across this situation.
-maxdepth 1
means only files within that folder
-newermt
used for files modified on that date or after..
! -newermt
used for removing files before the said date.
I also added the -v flag so I would see the output of files been deleted.
Thanks for all the help this saved me a lot of time..
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
So after playing with the commands for a while this is the command that worked.
find . -maxdepth 1 -type f ! -newermt "2018-10-16" -exec rm -v {} ;
These flags also helped incase you're come across this situation.
-maxdepth 1
means only files within that folder
-newermt
used for files modified on that date or after..
! -newermt
used for removing files before the said date.
I also added the -v flag so I would see the output of files been deleted.
Thanks for all the help this saved me a lot of time..
add a comment |
up vote
0
down vote
So after playing with the commands for a while this is the command that worked.
find . -maxdepth 1 -type f ! -newermt "2018-10-16" -exec rm -v {} ;
These flags also helped incase you're come across this situation.
-maxdepth 1
means only files within that folder
-newermt
used for files modified on that date or after..
! -newermt
used for removing files before the said date.
I also added the -v flag so I would see the output of files been deleted.
Thanks for all the help this saved me a lot of time..
add a comment |
up vote
0
down vote
up vote
0
down vote
So after playing with the commands for a while this is the command that worked.
find . -maxdepth 1 -type f ! -newermt "2018-10-16" -exec rm -v {} ;
These flags also helped incase you're come across this situation.
-maxdepth 1
means only files within that folder
-newermt
used for files modified on that date or after..
! -newermt
used for removing files before the said date.
I also added the -v flag so I would see the output of files been deleted.
Thanks for all the help this saved me a lot of time..
So after playing with the commands for a while this is the command that worked.
find . -maxdepth 1 -type f ! -newermt "2018-10-16" -exec rm -v {} ;
These flags also helped incase you're come across this situation.
-maxdepth 1
means only files within that folder
-newermt
used for files modified on that date or after..
! -newermt
used for removing files before the said date.
I also added the -v flag so I would see the output of files been deleted.
Thanks for all the help this saved me a lot of time..
answered Nov 12 at 7:43
iOflower
248
248
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247956%2fdeleting-files-older-than-x-number-of-years-or-days%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
find /home/user/uploads -maxdepth 1 -type f ! -newermt "2018-10-14 00:00:00"
is correct for matching only files older than2018-10-14 00:00:00"
. You say "This command removes everything" -- were there files newer than that deleted?– David C. Rankin
Nov 11 at 10:58
yes... it removed new files that were created after this date, instead of files created before this date
– iOflower
Nov 11 at 11:05
OK, hold on. Let me come up with a test for you.
– David C. Rankin
Nov 11 at 11:08
Make a
datetst
dir, e.g.md datetst && cd datetst
create a file for every day of Oct. 2018, e.gfor i in {1..31}; do touch -d "2018-10-$i" file_$i; done
, Now let'sfind . -maxdepth 1 -type f ! -newermt "2018-10-16" | sort
and see only files corresponding to days1-16
are selected as not newer than"2018-10-16"
which is right. Does you test given different results?– David C. Rankin
Nov 11 at 11:18
Yes, i got the results I wanted after testing your code.. it sorted out the files created before 2018-10-16. ! -newermt sorted files before and just -newermt sorted the files modified after.. I'm going to try this with my command and post the results..
– iOflower
Nov 11 at 11:35