Delete files older than X days with lastest modified file
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
Is there is any solution to delete the files based on comparing their modified date with the most recently modified file? The older files need to be deleted with respect to most recent file.
For example, if the last modified date is 09-10-2017, it means the files that are 10 days older need to be deleted. Same if the modified date is changed; the files to be deleted should change accordingly.
linux files rm timestamps
add a comment |Â
up vote
1
down vote
favorite
Is there is any solution to delete the files based on comparing their modified date with the most recently modified file? The older files need to be deleted with respect to most recent file.
For example, if the last modified date is 09-10-2017, it means the files that are 10 days older need to be deleted. Same if the modified date is changed; the files to be deleted should change accordingly.
linux files rm timestamps
1
what if Last modified date is01-03-2017
?
â RomanPerekhrest
Oct 9 '17 at 10:06
You want to delete the files that are more than 10 days older than the most recently modified file?
â Stéphane Chazelas
Oct 9 '17 at 10:09
yes, It needs to deleted accordingly with latest file
â Vignesh Vignasivakara
Oct 9 '17 at 10:58
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Is there is any solution to delete the files based on comparing their modified date with the most recently modified file? The older files need to be deleted with respect to most recent file.
For example, if the last modified date is 09-10-2017, it means the files that are 10 days older need to be deleted. Same if the modified date is changed; the files to be deleted should change accordingly.
linux files rm timestamps
Is there is any solution to delete the files based on comparing their modified date with the most recently modified file? The older files need to be deleted with respect to most recent file.
For example, if the last modified date is 09-10-2017, it means the files that are 10 days older need to be deleted. Same if the modified date is changed; the files to be deleted should change accordingly.
linux files rm timestamps
linux files rm timestamps
edited Oct 9 '17 at 12:38
Jeff Schaller
32.3k849109
32.3k849109
asked Oct 9 '17 at 10:00
Vignesh Vignasivakara
61
61
1
what if Last modified date is01-03-2017
?
â RomanPerekhrest
Oct 9 '17 at 10:06
You want to delete the files that are more than 10 days older than the most recently modified file?
â Stéphane Chazelas
Oct 9 '17 at 10:09
yes, It needs to deleted accordingly with latest file
â Vignesh Vignasivakara
Oct 9 '17 at 10:58
add a comment |Â
1
what if Last modified date is01-03-2017
?
â RomanPerekhrest
Oct 9 '17 at 10:06
You want to delete the files that are more than 10 days older than the most recently modified file?
â Stéphane Chazelas
Oct 9 '17 at 10:09
yes, It needs to deleted accordingly with latest file
â Vignesh Vignasivakara
Oct 9 '17 at 10:58
1
1
what if Last modified date is
01-03-2017
?â RomanPerekhrest
Oct 9 '17 at 10:06
what if Last modified date is
01-03-2017
?â RomanPerekhrest
Oct 9 '17 at 10:06
You want to delete the files that are more than 10 days older than the most recently modified file?
â Stéphane Chazelas
Oct 9 '17 at 10:09
You want to delete the files that are more than 10 days older than the most recently modified file?
â Stéphane Chazelas
Oct 9 '17 at 10:09
yes, It needs to deleted accordingly with latest file
â Vignesh Vignasivakara
Oct 9 '17 at 10:58
yes, It needs to deleted accordingly with latest file
â Vignesh Vignasivakara
Oct 9 '17 at 10:58
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
3
down vote
With GNU tools:
find . -type f -printf '%T@:%p' |
sort -rzn |
awk -v RS='' -v ORS='' -F: -v d=10 '
NR == 1 t = $1 - 86400 * d
$1 < t, 0 sub(/[^:]*:/, ""); print' |
xargs -r0 echo rm
(remove the echo
when happy).
We're getting find
to print 123456.123:./path/to/file<NUL>
for each file (with 123456.123
being the last modification time of the files).
We're sorting that numerically (so the newest file comes first) and then using awk
to find the ones that are more than 10 days older than the newest file (found in the first record in that sorted list).
add a comment |Â
up vote
0
down vote
I feel below command would help if you are working on Linux based Operating system.
find /path/to/directory/ -mindepth 1 -mtime +7 -delete
+7 = variable which means files older than 7 days will be deleted
Be careful with spaces, quotes to rm.
But that doesnâÂÂt answer the question, which is about deleting the latest modified file and files older than 10 days.
â Stephen Kitt
Jul 24 at 13:14
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
With GNU tools:
find . -type f -printf '%T@:%p' |
sort -rzn |
awk -v RS='' -v ORS='' -F: -v d=10 '
NR == 1 t = $1 - 86400 * d
$1 < t, 0 sub(/[^:]*:/, ""); print' |
xargs -r0 echo rm
(remove the echo
when happy).
We're getting find
to print 123456.123:./path/to/file<NUL>
for each file (with 123456.123
being the last modification time of the files).
We're sorting that numerically (so the newest file comes first) and then using awk
to find the ones that are more than 10 days older than the newest file (found in the first record in that sorted list).
add a comment |Â
up vote
3
down vote
With GNU tools:
find . -type f -printf '%T@:%p' |
sort -rzn |
awk -v RS='' -v ORS='' -F: -v d=10 '
NR == 1 t = $1 - 86400 * d
$1 < t, 0 sub(/[^:]*:/, ""); print' |
xargs -r0 echo rm
(remove the echo
when happy).
We're getting find
to print 123456.123:./path/to/file<NUL>
for each file (with 123456.123
being the last modification time of the files).
We're sorting that numerically (so the newest file comes first) and then using awk
to find the ones that are more than 10 days older than the newest file (found in the first record in that sorted list).
add a comment |Â
up vote
3
down vote
up vote
3
down vote
With GNU tools:
find . -type f -printf '%T@:%p' |
sort -rzn |
awk -v RS='' -v ORS='' -F: -v d=10 '
NR == 1 t = $1 - 86400 * d
$1 < t, 0 sub(/[^:]*:/, ""); print' |
xargs -r0 echo rm
(remove the echo
when happy).
We're getting find
to print 123456.123:./path/to/file<NUL>
for each file (with 123456.123
being the last modification time of the files).
We're sorting that numerically (so the newest file comes first) and then using awk
to find the ones that are more than 10 days older than the newest file (found in the first record in that sorted list).
With GNU tools:
find . -type f -printf '%T@:%p' |
sort -rzn |
awk -v RS='' -v ORS='' -F: -v d=10 '
NR == 1 t = $1 - 86400 * d
$1 < t, 0 sub(/[^:]*:/, ""); print' |
xargs -r0 echo rm
(remove the echo
when happy).
We're getting find
to print 123456.123:./path/to/file<NUL>
for each file (with 123456.123
being the last modification time of the files).
We're sorting that numerically (so the newest file comes first) and then using awk
to find the ones that are more than 10 days older than the newest file (found in the first record in that sorted list).
edited Oct 12 '17 at 5:21
answered Oct 9 '17 at 10:17
Stéphane Chazelas
283k53522859
283k53522859
add a comment |Â
add a comment |Â
up vote
0
down vote
I feel below command would help if you are working on Linux based Operating system.
find /path/to/directory/ -mindepth 1 -mtime +7 -delete
+7 = variable which means files older than 7 days will be deleted
Be careful with spaces, quotes to rm.
But that doesnâÂÂt answer the question, which is about deleting the latest modified file and files older than 10 days.
â Stephen Kitt
Jul 24 at 13:14
add a comment |Â
up vote
0
down vote
I feel below command would help if you are working on Linux based Operating system.
find /path/to/directory/ -mindepth 1 -mtime +7 -delete
+7 = variable which means files older than 7 days will be deleted
Be careful with spaces, quotes to rm.
But that doesnâÂÂt answer the question, which is about deleting the latest modified file and files older than 10 days.
â Stephen Kitt
Jul 24 at 13:14
add a comment |Â
up vote
0
down vote
up vote
0
down vote
I feel below command would help if you are working on Linux based Operating system.
find /path/to/directory/ -mindepth 1 -mtime +7 -delete
+7 = variable which means files older than 7 days will be deleted
Be careful with spaces, quotes to rm.
I feel below command would help if you are working on Linux based Operating system.
find /path/to/directory/ -mindepth 1 -mtime +7 -delete
+7 = variable which means files older than 7 days will be deleted
Be careful with spaces, quotes to rm.
answered Jul 24 at 9:29
GURUVEER
11
11
But that doesnâÂÂt answer the question, which is about deleting the latest modified file and files older than 10 days.
â Stephen Kitt
Jul 24 at 13:14
add a comment |Â
But that doesnâÂÂt answer the question, which is about deleting the latest modified file and files older than 10 days.
â Stephen Kitt
Jul 24 at 13:14
But that doesnâÂÂt answer the question, which is about deleting the latest modified file and files older than 10 days.
â Stephen Kitt
Jul 24 at 13:14
But that doesnâÂÂt answer the question, which is about deleting the latest modified file and files older than 10 days.
â Stephen Kitt
Jul 24 at 13:14
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f396972%2fdelete-files-older-than-x-days-with-lastest-modified-file%23new-answer', 'question_page');
);
Post as a guest
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
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
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
1
what if Last modified date is
01-03-2017
?â RomanPerekhrest
Oct 9 '17 at 10:06
You want to delete the files that are more than 10 days older than the most recently modified file?
â Stéphane Chazelas
Oct 9 '17 at 10:09
yes, It needs to deleted accordingly with latest file
â Vignesh Vignasivakara
Oct 9 '17 at 10:58