How does the “1 month ago” option in date command work?
Clash Royale CLAN TAG#URR8PPP
I'm not sure if this gets the past date within the current day or if it only takes 30 or 31 days to it.
e.g.
If the current date is March 28th
, 1 month ago must be February 28th
, but what happen when it's March 30th
?
Scenario
I want to backup some files each day, the script will save this files within the current date with $(date +%Y%m%d)
format, like 20150603_bckp.tar.gz
, then when the next month arrives, remove all those files within 1 month ago except the 1st's and the 15th's files, so this is my condition:
past_month = $(date -d "-1 month" +%Y%m%d)
day = $(date +%d)
if [ "$day" != 01 ] && [ "$day" != 15 ]
then
rm /path/of/files/$past_month_bckp.tar.gz
echo "Depuration done"
else
echo "Keep file"
fi
But I want to know, what will happen when the date is 30th, 31th or even the past February example? It will keep those files? or remove day 1st files?
When it's 31th the depuration will execute, so if the past month only had 30 days, this will remove the day 1st file?
I hope I hinted.
date coreutils
add a comment |
I'm not sure if this gets the past date within the current day or if it only takes 30 or 31 days to it.
e.g.
If the current date is March 28th
, 1 month ago must be February 28th
, but what happen when it's March 30th
?
Scenario
I want to backup some files each day, the script will save this files within the current date with $(date +%Y%m%d)
format, like 20150603_bckp.tar.gz
, then when the next month arrives, remove all those files within 1 month ago except the 1st's and the 15th's files, so this is my condition:
past_month = $(date -d "-1 month" +%Y%m%d)
day = $(date +%d)
if [ "$day" != 01 ] && [ "$day" != 15 ]
then
rm /path/of/files/$past_month_bckp.tar.gz
echo "Depuration done"
else
echo "Keep file"
fi
But I want to know, what will happen when the date is 30th, 31th or even the past February example? It will keep those files? or remove day 1st files?
When it's 31th the depuration will execute, so if the past month only had 30 days, this will remove the day 1st file?
I hope I hinted.
date coreutils
1
Wouldn't you know I'm here on March 30th for this exact reason
– user7203
Mar 30 '18 at 17:02
add a comment |
I'm not sure if this gets the past date within the current day or if it only takes 30 or 31 days to it.
e.g.
If the current date is March 28th
, 1 month ago must be February 28th
, but what happen when it's March 30th
?
Scenario
I want to backup some files each day, the script will save this files within the current date with $(date +%Y%m%d)
format, like 20150603_bckp.tar.gz
, then when the next month arrives, remove all those files within 1 month ago except the 1st's and the 15th's files, so this is my condition:
past_month = $(date -d "-1 month" +%Y%m%d)
day = $(date +%d)
if [ "$day" != 01 ] && [ "$day" != 15 ]
then
rm /path/of/files/$past_month_bckp.tar.gz
echo "Depuration done"
else
echo "Keep file"
fi
But I want to know, what will happen when the date is 30th, 31th or even the past February example? It will keep those files? or remove day 1st files?
When it's 31th the depuration will execute, so if the past month only had 30 days, this will remove the day 1st file?
I hope I hinted.
date coreutils
I'm not sure if this gets the past date within the current day or if it only takes 30 or 31 days to it.
e.g.
If the current date is March 28th
, 1 month ago must be February 28th
, but what happen when it's March 30th
?
Scenario
I want to backup some files each day, the script will save this files within the current date with $(date +%Y%m%d)
format, like 20150603_bckp.tar.gz
, then when the next month arrives, remove all those files within 1 month ago except the 1st's and the 15th's files, so this is my condition:
past_month = $(date -d "-1 month" +%Y%m%d)
day = $(date +%d)
if [ "$day" != 01 ] && [ "$day" != 15 ]
then
rm /path/of/files/$past_month_bckp.tar.gz
echo "Depuration done"
else
echo "Keep file"
fi
But I want to know, what will happen when the date is 30th, 31th or even the past February example? It will keep those files? or remove day 1st files?
When it's 31th the depuration will execute, so if the past month only had 30 days, this will remove the day 1st file?
I hope I hinted.
date coreutils
date coreutils
edited Jun 8 '15 at 21:17
tachomi
asked Jun 4 '15 at 18:49
tachomitachomi
3,72731135
3,72731135
1
Wouldn't you know I'm here on March 30th for this exact reason
– user7203
Mar 30 '18 at 17:02
add a comment |
1
Wouldn't you know I'm here on March 30th for this exact reason
– user7203
Mar 30 '18 at 17:02
1
1
Wouldn't you know I'm here on March 30th for this exact reason
– user7203
Mar 30 '18 at 17:02
Wouldn't you know I'm here on March 30th for this exact reason
– user7203
Mar 30 '18 at 17:02
add a comment |
3 Answers
3
active
oldest
votes
- 1 month
will subtract one from the month number, and then if the resulting date is not valid (February 30
, for example), adjust it so that it is valid. So December 31 - 1 month
is December 1
, not a day in November, and March 31 - 1 month
is March 3
(unless executed in a leap year).
Here's quote from the info page for Gnu date
(which is the date
version which implements this syntax), which includes a good suggestion to make the arithmetic more robust:
The fuzz in units can cause problems with relative items. For
example,2003-07-31 -1 month
might evaluate to 2003-07-01, because
2003-06-31 is an invalid date. To determine the previous month more
reliably, you can ask for the month before the 15th of the current
month. For example:
$ date -R
Thu, 31 Jul 2003 13:02:39 -0700
$ date --date='-1 month' +'Last month was %B?'
Last month was July?
$ date --date="$(date +%Y-%m-15) -1 month" +'Last month was %B!'
Last month was June!
Another warning, also quoted from the info page:
Also, take care when manipulating dates around clock changes such as
daylight saving leaps. In a few cases these have added or subtracted
as much as 24 hours from the clock, so it is often wise to adopt
universal time by setting theTZ
environment variable toUTC0
before embarking on calendrical calculations.
I have a doubt in here, specifically in the February situation... When it's March 29, 30 or 31 it might take the following number of the past month if the current one doesn't exist, so in this three situations it might take March 1st no? Cause if March 29, 30, 31 and then March 1, 2, 3 as you said, then April 1 will be March 4 due to the restructure (I don't know if it's well said) that was made, what do you think?
– tachomi
Jun 4 '15 at 21:40
One month before April 1 is March 1 and one month before March 1 is February 1. These are not problems. The problem is that one month before May 31 is May 1 because April 31 doesn't exist. You should check the day number of the one-month-ago date, not the day of the current date.
– rici
Jun 4 '15 at 22:54
2
Doh! The fact that "one month ago" of some specific days in any month may result in the same month is nothing that I'd expect from adate
implementation; in my book it's semantically counter-intuitive. - Thanks for pointing that out, it's good to know.
– Janis
Jun 5 '15 at 7:13
add a comment |
Instead of relying on filenames for purging your backup files, you'd be safer relying on their time metadata.
For example you can delete all files older than 30 days with this command :
/usr/bin/find /path/to/your/files -type f -ctime +30 -delete
As a free bonus, running this everyday will allow you to keep your backup files on a rolling month, allowing a better disk space management.
For keeping 1st or 15th files, you could either store them in another directory, either update the find command with -not -name option.
add a comment |
You could try to see how the date works by the following:
date -d "$(date -d "Mar 31 2019" +%F) +1 month ago"
Sun Mar 3 00:00:00 EET 2019
So I think that based on the curent last month (not curent, if that is March), decreases the number of days from the last month from the curent month.
You could try the following syntax to delete files older then a number of days ( in the example bellow 31 days). One line to solve the problem (this is for a simple approach).
find /path/to/folder/with/arhives/ -mindepth 1 -mtime +31 -delete
For a more detailed approach you could also do something like this:
1. Finding the number of days for the curent month:
cal $(date +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF'
Finding the number of days for the last month:
echo "$(cal $(date +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF')-"$(cal $(date -d "+1 month ago" +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF') | bc
So now we find the difference between those two months:
echo "$(cal $(date +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF')-"$(cal $(date -d "+1 month ago" +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF') | bc
Now lets assign some variables to this 3 steps.
Step 1 Variable Name: FCM
Step 2 Variable Name: FLM
Step 3 Variable NAme: DCL
FCM=$(cal $(date +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF')
FLM=$(al $(date -d "+1 month ago" +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF')
DCL=$(echo "$FCM-$FLM" | bc)
Verify if $DCL equals 0 (zero) and delete based on the difference between $FCM and $FLM:
if [ $DCL -ne 0 ]; then
if [ $DCL -lt 0 ]; then
find /path/to/folder/with/arhives/ -mindepth 1 -mtime +$(echo "$DCL+$FLM" | bc) -delete
elif [ $DCL -gt 0 ]; then
find /path/to/folder/with/arhives/ -mindepth 1 -mtime +$(echo "$DCL+$FLM" | bc) -delete
fi
else
find /path/to/folder/with/arhives/ -mindepth 1 -mtime +$FLM -delete
fi
You can also add a variable to "/path/to/folde/with/arhives/".
Conclusion: If you want to delete files older then one month (exactly), you need to adjust the exact number of days, if you are using "date" command and "+1 month ago".
add a comment |
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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%2funix.stackexchange.com%2fquestions%2f207617%2fhow-does-the-1-month-ago-option-in-date-command-work%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
- 1 month
will subtract one from the month number, and then if the resulting date is not valid (February 30
, for example), adjust it so that it is valid. So December 31 - 1 month
is December 1
, not a day in November, and March 31 - 1 month
is March 3
(unless executed in a leap year).
Here's quote from the info page for Gnu date
(which is the date
version which implements this syntax), which includes a good suggestion to make the arithmetic more robust:
The fuzz in units can cause problems with relative items. For
example,2003-07-31 -1 month
might evaluate to 2003-07-01, because
2003-06-31 is an invalid date. To determine the previous month more
reliably, you can ask for the month before the 15th of the current
month. For example:
$ date -R
Thu, 31 Jul 2003 13:02:39 -0700
$ date --date='-1 month' +'Last month was %B?'
Last month was July?
$ date --date="$(date +%Y-%m-15) -1 month" +'Last month was %B!'
Last month was June!
Another warning, also quoted from the info page:
Also, take care when manipulating dates around clock changes such as
daylight saving leaps. In a few cases these have added or subtracted
as much as 24 hours from the clock, so it is often wise to adopt
universal time by setting theTZ
environment variable toUTC0
before embarking on calendrical calculations.
I have a doubt in here, specifically in the February situation... When it's March 29, 30 or 31 it might take the following number of the past month if the current one doesn't exist, so in this three situations it might take March 1st no? Cause if March 29, 30, 31 and then March 1, 2, 3 as you said, then April 1 will be March 4 due to the restructure (I don't know if it's well said) that was made, what do you think?
– tachomi
Jun 4 '15 at 21:40
One month before April 1 is March 1 and one month before March 1 is February 1. These are not problems. The problem is that one month before May 31 is May 1 because April 31 doesn't exist. You should check the day number of the one-month-ago date, not the day of the current date.
– rici
Jun 4 '15 at 22:54
2
Doh! The fact that "one month ago" of some specific days in any month may result in the same month is nothing that I'd expect from adate
implementation; in my book it's semantically counter-intuitive. - Thanks for pointing that out, it's good to know.
– Janis
Jun 5 '15 at 7:13
add a comment |
- 1 month
will subtract one from the month number, and then if the resulting date is not valid (February 30
, for example), adjust it so that it is valid. So December 31 - 1 month
is December 1
, not a day in November, and March 31 - 1 month
is March 3
(unless executed in a leap year).
Here's quote from the info page for Gnu date
(which is the date
version which implements this syntax), which includes a good suggestion to make the arithmetic more robust:
The fuzz in units can cause problems with relative items. For
example,2003-07-31 -1 month
might evaluate to 2003-07-01, because
2003-06-31 is an invalid date. To determine the previous month more
reliably, you can ask for the month before the 15th of the current
month. For example:
$ date -R
Thu, 31 Jul 2003 13:02:39 -0700
$ date --date='-1 month' +'Last month was %B?'
Last month was July?
$ date --date="$(date +%Y-%m-15) -1 month" +'Last month was %B!'
Last month was June!
Another warning, also quoted from the info page:
Also, take care when manipulating dates around clock changes such as
daylight saving leaps. In a few cases these have added or subtracted
as much as 24 hours from the clock, so it is often wise to adopt
universal time by setting theTZ
environment variable toUTC0
before embarking on calendrical calculations.
I have a doubt in here, specifically in the February situation... When it's March 29, 30 or 31 it might take the following number of the past month if the current one doesn't exist, so in this three situations it might take March 1st no? Cause if March 29, 30, 31 and then March 1, 2, 3 as you said, then April 1 will be March 4 due to the restructure (I don't know if it's well said) that was made, what do you think?
– tachomi
Jun 4 '15 at 21:40
One month before April 1 is March 1 and one month before March 1 is February 1. These are not problems. The problem is that one month before May 31 is May 1 because April 31 doesn't exist. You should check the day number of the one-month-ago date, not the day of the current date.
– rici
Jun 4 '15 at 22:54
2
Doh! The fact that "one month ago" of some specific days in any month may result in the same month is nothing that I'd expect from adate
implementation; in my book it's semantically counter-intuitive. - Thanks for pointing that out, it's good to know.
– Janis
Jun 5 '15 at 7:13
add a comment |
- 1 month
will subtract one from the month number, and then if the resulting date is not valid (February 30
, for example), adjust it so that it is valid. So December 31 - 1 month
is December 1
, not a day in November, and March 31 - 1 month
is March 3
(unless executed in a leap year).
Here's quote from the info page for Gnu date
(which is the date
version which implements this syntax), which includes a good suggestion to make the arithmetic more robust:
The fuzz in units can cause problems with relative items. For
example,2003-07-31 -1 month
might evaluate to 2003-07-01, because
2003-06-31 is an invalid date. To determine the previous month more
reliably, you can ask for the month before the 15th of the current
month. For example:
$ date -R
Thu, 31 Jul 2003 13:02:39 -0700
$ date --date='-1 month' +'Last month was %B?'
Last month was July?
$ date --date="$(date +%Y-%m-15) -1 month" +'Last month was %B!'
Last month was June!
Another warning, also quoted from the info page:
Also, take care when manipulating dates around clock changes such as
daylight saving leaps. In a few cases these have added or subtracted
as much as 24 hours from the clock, so it is often wise to adopt
universal time by setting theTZ
environment variable toUTC0
before embarking on calendrical calculations.
- 1 month
will subtract one from the month number, and then if the resulting date is not valid (February 30
, for example), adjust it so that it is valid. So December 31 - 1 month
is December 1
, not a day in November, and March 31 - 1 month
is March 3
(unless executed in a leap year).
Here's quote from the info page for Gnu date
(which is the date
version which implements this syntax), which includes a good suggestion to make the arithmetic more robust:
The fuzz in units can cause problems with relative items. For
example,2003-07-31 -1 month
might evaluate to 2003-07-01, because
2003-06-31 is an invalid date. To determine the previous month more
reliably, you can ask for the month before the 15th of the current
month. For example:
$ date -R
Thu, 31 Jul 2003 13:02:39 -0700
$ date --date='-1 month' +'Last month was %B?'
Last month was July?
$ date --date="$(date +%Y-%m-15) -1 month" +'Last month was %B!'
Last month was June!
Another warning, also quoted from the info page:
Also, take care when manipulating dates around clock changes such as
daylight saving leaps. In a few cases these have added or subtracted
as much as 24 hours from the clock, so it is often wise to adopt
universal time by setting theTZ
environment variable toUTC0
before embarking on calendrical calculations.
edited Feb 9 '17 at 17:31
luissquall
1033
1033
answered Jun 4 '15 at 19:35
ricirici
7,5572631
7,5572631
I have a doubt in here, specifically in the February situation... When it's March 29, 30 or 31 it might take the following number of the past month if the current one doesn't exist, so in this three situations it might take March 1st no? Cause if March 29, 30, 31 and then March 1, 2, 3 as you said, then April 1 will be March 4 due to the restructure (I don't know if it's well said) that was made, what do you think?
– tachomi
Jun 4 '15 at 21:40
One month before April 1 is March 1 and one month before March 1 is February 1. These are not problems. The problem is that one month before May 31 is May 1 because April 31 doesn't exist. You should check the day number of the one-month-ago date, not the day of the current date.
– rici
Jun 4 '15 at 22:54
2
Doh! The fact that "one month ago" of some specific days in any month may result in the same month is nothing that I'd expect from adate
implementation; in my book it's semantically counter-intuitive. - Thanks for pointing that out, it's good to know.
– Janis
Jun 5 '15 at 7:13
add a comment |
I have a doubt in here, specifically in the February situation... When it's March 29, 30 or 31 it might take the following number of the past month if the current one doesn't exist, so in this three situations it might take March 1st no? Cause if March 29, 30, 31 and then March 1, 2, 3 as you said, then April 1 will be March 4 due to the restructure (I don't know if it's well said) that was made, what do you think?
– tachomi
Jun 4 '15 at 21:40
One month before April 1 is March 1 and one month before March 1 is February 1. These are not problems. The problem is that one month before May 31 is May 1 because April 31 doesn't exist. You should check the day number of the one-month-ago date, not the day of the current date.
– rici
Jun 4 '15 at 22:54
2
Doh! The fact that "one month ago" of some specific days in any month may result in the same month is nothing that I'd expect from adate
implementation; in my book it's semantically counter-intuitive. - Thanks for pointing that out, it's good to know.
– Janis
Jun 5 '15 at 7:13
I have a doubt in here, specifically in the February situation... When it's March 29, 30 or 31 it might take the following number of the past month if the current one doesn't exist, so in this three situations it might take March 1st no? Cause if March 29, 30, 31 and then March 1, 2, 3 as you said, then April 1 will be March 4 due to the restructure (I don't know if it's well said) that was made, what do you think?
– tachomi
Jun 4 '15 at 21:40
I have a doubt in here, specifically in the February situation... When it's March 29, 30 or 31 it might take the following number of the past month if the current one doesn't exist, so in this three situations it might take March 1st no? Cause if March 29, 30, 31 and then March 1, 2, 3 as you said, then April 1 will be March 4 due to the restructure (I don't know if it's well said) that was made, what do you think?
– tachomi
Jun 4 '15 at 21:40
One month before April 1 is March 1 and one month before March 1 is February 1. These are not problems. The problem is that one month before May 31 is May 1 because April 31 doesn't exist. You should check the day number of the one-month-ago date, not the day of the current date.
– rici
Jun 4 '15 at 22:54
One month before April 1 is March 1 and one month before March 1 is February 1. These are not problems. The problem is that one month before May 31 is May 1 because April 31 doesn't exist. You should check the day number of the one-month-ago date, not the day of the current date.
– rici
Jun 4 '15 at 22:54
2
2
Doh! The fact that "one month ago" of some specific days in any month may result in the same month is nothing that I'd expect from a
date
implementation; in my book it's semantically counter-intuitive. - Thanks for pointing that out, it's good to know.– Janis
Jun 5 '15 at 7:13
Doh! The fact that "one month ago" of some specific days in any month may result in the same month is nothing that I'd expect from a
date
implementation; in my book it's semantically counter-intuitive. - Thanks for pointing that out, it's good to know.– Janis
Jun 5 '15 at 7:13
add a comment |
Instead of relying on filenames for purging your backup files, you'd be safer relying on their time metadata.
For example you can delete all files older than 30 days with this command :
/usr/bin/find /path/to/your/files -type f -ctime +30 -delete
As a free bonus, running this everyday will allow you to keep your backup files on a rolling month, allowing a better disk space management.
For keeping 1st or 15th files, you could either store them in another directory, either update the find command with -not -name option.
add a comment |
Instead of relying on filenames for purging your backup files, you'd be safer relying on their time metadata.
For example you can delete all files older than 30 days with this command :
/usr/bin/find /path/to/your/files -type f -ctime +30 -delete
As a free bonus, running this everyday will allow you to keep your backup files on a rolling month, allowing a better disk space management.
For keeping 1st or 15th files, you could either store them in another directory, either update the find command with -not -name option.
add a comment |
Instead of relying on filenames for purging your backup files, you'd be safer relying on their time metadata.
For example you can delete all files older than 30 days with this command :
/usr/bin/find /path/to/your/files -type f -ctime +30 -delete
As a free bonus, running this everyday will allow you to keep your backup files on a rolling month, allowing a better disk space management.
For keeping 1st or 15th files, you could either store them in another directory, either update the find command with -not -name option.
Instead of relying on filenames for purging your backup files, you'd be safer relying on their time metadata.
For example you can delete all files older than 30 days with this command :
/usr/bin/find /path/to/your/files -type f -ctime +30 -delete
As a free bonus, running this everyday will allow you to keep your backup files on a rolling month, allowing a better disk space management.
For keeping 1st or 15th files, you could either store them in another directory, either update the find command with -not -name option.
answered Dec 2 '16 at 18:20
Bertrand LupartBertrand Lupart
212
212
add a comment |
add a comment |
You could try to see how the date works by the following:
date -d "$(date -d "Mar 31 2019" +%F) +1 month ago"
Sun Mar 3 00:00:00 EET 2019
So I think that based on the curent last month (not curent, if that is March), decreases the number of days from the last month from the curent month.
You could try the following syntax to delete files older then a number of days ( in the example bellow 31 days). One line to solve the problem (this is for a simple approach).
find /path/to/folder/with/arhives/ -mindepth 1 -mtime +31 -delete
For a more detailed approach you could also do something like this:
1. Finding the number of days for the curent month:
cal $(date +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF'
Finding the number of days for the last month:
echo "$(cal $(date +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF')-"$(cal $(date -d "+1 month ago" +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF') | bc
So now we find the difference between those two months:
echo "$(cal $(date +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF')-"$(cal $(date -d "+1 month ago" +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF') | bc
Now lets assign some variables to this 3 steps.
Step 1 Variable Name: FCM
Step 2 Variable Name: FLM
Step 3 Variable NAme: DCL
FCM=$(cal $(date +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF')
FLM=$(al $(date -d "+1 month ago" +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF')
DCL=$(echo "$FCM-$FLM" | bc)
Verify if $DCL equals 0 (zero) and delete based on the difference between $FCM and $FLM:
if [ $DCL -ne 0 ]; then
if [ $DCL -lt 0 ]; then
find /path/to/folder/with/arhives/ -mindepth 1 -mtime +$(echo "$DCL+$FLM" | bc) -delete
elif [ $DCL -gt 0 ]; then
find /path/to/folder/with/arhives/ -mindepth 1 -mtime +$(echo "$DCL+$FLM" | bc) -delete
fi
else
find /path/to/folder/with/arhives/ -mindepth 1 -mtime +$FLM -delete
fi
You can also add a variable to "/path/to/folde/with/arhives/".
Conclusion: If you want to delete files older then one month (exactly), you need to adjust the exact number of days, if you are using "date" command and "+1 month ago".
add a comment |
You could try to see how the date works by the following:
date -d "$(date -d "Mar 31 2019" +%F) +1 month ago"
Sun Mar 3 00:00:00 EET 2019
So I think that based on the curent last month (not curent, if that is March), decreases the number of days from the last month from the curent month.
You could try the following syntax to delete files older then a number of days ( in the example bellow 31 days). One line to solve the problem (this is for a simple approach).
find /path/to/folder/with/arhives/ -mindepth 1 -mtime +31 -delete
For a more detailed approach you could also do something like this:
1. Finding the number of days for the curent month:
cal $(date +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF'
Finding the number of days for the last month:
echo "$(cal $(date +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF')-"$(cal $(date -d "+1 month ago" +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF') | bc
So now we find the difference between those two months:
echo "$(cal $(date +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF')-"$(cal $(date -d "+1 month ago" +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF') | bc
Now lets assign some variables to this 3 steps.
Step 1 Variable Name: FCM
Step 2 Variable Name: FLM
Step 3 Variable NAme: DCL
FCM=$(cal $(date +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF')
FLM=$(al $(date -d "+1 month ago" +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF')
DCL=$(echo "$FCM-$FLM" | bc)
Verify if $DCL equals 0 (zero) and delete based on the difference between $FCM and $FLM:
if [ $DCL -ne 0 ]; then
if [ $DCL -lt 0 ]; then
find /path/to/folder/with/arhives/ -mindepth 1 -mtime +$(echo "$DCL+$FLM" | bc) -delete
elif [ $DCL -gt 0 ]; then
find /path/to/folder/with/arhives/ -mindepth 1 -mtime +$(echo "$DCL+$FLM" | bc) -delete
fi
else
find /path/to/folder/with/arhives/ -mindepth 1 -mtime +$FLM -delete
fi
You can also add a variable to "/path/to/folde/with/arhives/".
Conclusion: If you want to delete files older then one month (exactly), you need to adjust the exact number of days, if you are using "date" command and "+1 month ago".
add a comment |
You could try to see how the date works by the following:
date -d "$(date -d "Mar 31 2019" +%F) +1 month ago"
Sun Mar 3 00:00:00 EET 2019
So I think that based on the curent last month (not curent, if that is March), decreases the number of days from the last month from the curent month.
You could try the following syntax to delete files older then a number of days ( in the example bellow 31 days). One line to solve the problem (this is for a simple approach).
find /path/to/folder/with/arhives/ -mindepth 1 -mtime +31 -delete
For a more detailed approach you could also do something like this:
1. Finding the number of days for the curent month:
cal $(date +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF'
Finding the number of days for the last month:
echo "$(cal $(date +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF')-"$(cal $(date -d "+1 month ago" +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF') | bc
So now we find the difference between those two months:
echo "$(cal $(date +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF')-"$(cal $(date -d "+1 month ago" +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF') | bc
Now lets assign some variables to this 3 steps.
Step 1 Variable Name: FCM
Step 2 Variable Name: FLM
Step 3 Variable NAme: DCL
FCM=$(cal $(date +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF')
FLM=$(al $(date -d "+1 month ago" +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF')
DCL=$(echo "$FCM-$FLM" | bc)
Verify if $DCL equals 0 (zero) and delete based on the difference between $FCM and $FLM:
if [ $DCL -ne 0 ]; then
if [ $DCL -lt 0 ]; then
find /path/to/folder/with/arhives/ -mindepth 1 -mtime +$(echo "$DCL+$FLM" | bc) -delete
elif [ $DCL -gt 0 ]; then
find /path/to/folder/with/arhives/ -mindepth 1 -mtime +$(echo "$DCL+$FLM" | bc) -delete
fi
else
find /path/to/folder/with/arhives/ -mindepth 1 -mtime +$FLM -delete
fi
You can also add a variable to "/path/to/folde/with/arhives/".
Conclusion: If you want to delete files older then one month (exactly), you need to adjust the exact number of days, if you are using "date" command and "+1 month ago".
You could try to see how the date works by the following:
date -d "$(date -d "Mar 31 2019" +%F) +1 month ago"
Sun Mar 3 00:00:00 EET 2019
So I think that based on the curent last month (not curent, if that is March), decreases the number of days from the last month from the curent month.
You could try the following syntax to delete files older then a number of days ( in the example bellow 31 days). One line to solve the problem (this is for a simple approach).
find /path/to/folder/with/arhives/ -mindepth 1 -mtime +31 -delete
For a more detailed approach you could also do something like this:
1. Finding the number of days for the curent month:
cal $(date +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF'
Finding the number of days for the last month:
echo "$(cal $(date +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF')-"$(cal $(date -d "+1 month ago" +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF') | bc
So now we find the difference between those two months:
echo "$(cal $(date +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF')-"$(cal $(date -d "+1 month ago" +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF') | bc
Now lets assign some variables to this 3 steps.
Step 1 Variable Name: FCM
Step 2 Variable Name: FLM
Step 3 Variable NAme: DCL
FCM=$(cal $(date +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF')
FLM=$(al $(date -d "+1 month ago" +%m) $(date +%y) | egrep -v "$(date +%y)|Su" | xargs | awk 'print $NF')
DCL=$(echo "$FCM-$FLM" | bc)
Verify if $DCL equals 0 (zero) and delete based on the difference between $FCM and $FLM:
if [ $DCL -ne 0 ]; then
if [ $DCL -lt 0 ]; then
find /path/to/folder/with/arhives/ -mindepth 1 -mtime +$(echo "$DCL+$FLM" | bc) -delete
elif [ $DCL -gt 0 ]; then
find /path/to/folder/with/arhives/ -mindepth 1 -mtime +$(echo "$DCL+$FLM" | bc) -delete
fi
else
find /path/to/folder/with/arhives/ -mindepth 1 -mtime +$FLM -delete
fi
You can also add a variable to "/path/to/folde/with/arhives/".
Conclusion: If you want to delete files older then one month (exactly), you need to adjust the exact number of days, if you are using "date" command and "+1 month ago".
answered Jan 29 at 10:37
Mihai MMihai M
92
92
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f207617%2fhow-does-the-1-month-ago-option-in-date-command-work%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
1
Wouldn't you know I'm here on March 30th for this exact reason
– user7203
Mar 30 '18 at 17:02