how to check my file is created within 10 days in shell script












0














I have a bunch of log files which are named according to their creation dates. For example; if my log file is created on 12 March 2018, the name of the logfile is log-2018-03-12.log



Here is what I want to do: From today's date, I want to check the name of my log files and zip the log files which are created in last 10 days.



Here is my code that zip all log files in a specific directory:



#!/bin/bash

# current time
now=$(date +"%F")

backupfile="backup-$now"
scripthome=/opt/MyStore/action_scripts/deneme/

tendaysbefore= date -d "$now - 10 days" '+%F'

for file in $scripthome;
do
find "$(basename "$file")" | zip -R $backupfile.zip "log-2*.log"
done


But I want to zip last 10 days log file, not all log files, and also I want to continue doing it for every 10 days after this. Also, after having zip file, I want to delete old log files.
In other words, I am trying to write a log-backup script. Can you help me please?



Thank you very much!










share|improve this question
























  • Does this help you? stackoverflow.com/questions/158044/…
    – Dominique
    Nov 19 '18 at 13:21










  • @Dominique I tried adding find . -type f -newerct $tendaysbefore ! -newerct $now But it gives me this error: find: I cannot figure out how to interpret ‘!’ as a date or time
    – smsms
    Nov 19 '18 at 13:53












  • That is because your $tendaysbefore is empty thus executing the actual command find . -type f -newerct ! …. Probably because your script above is incorrect. At the very least you want something like: tendaysbefore=$(date -d "$now - 10 days" '+%F')
    – grifferz
    Nov 19 '18 at 21:22










  • Actually it is not empty. I can print the value in it and it is correct. The error is most probably caused by syntax that '!' is not understood by the compiler. Any suggestion about this? @grifferz
    – smsms
    Nov 20 '18 at 6:14










  • I'm sorry, but you are mistaken. Running your code above results in tendaysbefore being left empty, that is why find gives you that error. Your assignment to tendaysbefore is missing $() around the date command as I said.
    – grifferz
    Nov 20 '18 at 15:00


















0














I have a bunch of log files which are named according to their creation dates. For example; if my log file is created on 12 March 2018, the name of the logfile is log-2018-03-12.log



Here is what I want to do: From today's date, I want to check the name of my log files and zip the log files which are created in last 10 days.



Here is my code that zip all log files in a specific directory:



#!/bin/bash

# current time
now=$(date +"%F")

backupfile="backup-$now"
scripthome=/opt/MyStore/action_scripts/deneme/

tendaysbefore= date -d "$now - 10 days" '+%F'

for file in $scripthome;
do
find "$(basename "$file")" | zip -R $backupfile.zip "log-2*.log"
done


But I want to zip last 10 days log file, not all log files, and also I want to continue doing it for every 10 days after this. Also, after having zip file, I want to delete old log files.
In other words, I am trying to write a log-backup script. Can you help me please?



Thank you very much!










share|improve this question
























  • Does this help you? stackoverflow.com/questions/158044/…
    – Dominique
    Nov 19 '18 at 13:21










  • @Dominique I tried adding find . -type f -newerct $tendaysbefore ! -newerct $now But it gives me this error: find: I cannot figure out how to interpret ‘!’ as a date or time
    – smsms
    Nov 19 '18 at 13:53












  • That is because your $tendaysbefore is empty thus executing the actual command find . -type f -newerct ! …. Probably because your script above is incorrect. At the very least you want something like: tendaysbefore=$(date -d "$now - 10 days" '+%F')
    – grifferz
    Nov 19 '18 at 21:22










  • Actually it is not empty. I can print the value in it and it is correct. The error is most probably caused by syntax that '!' is not understood by the compiler. Any suggestion about this? @grifferz
    – smsms
    Nov 20 '18 at 6:14










  • I'm sorry, but you are mistaken. Running your code above results in tendaysbefore being left empty, that is why find gives you that error. Your assignment to tendaysbefore is missing $() around the date command as I said.
    – grifferz
    Nov 20 '18 at 15:00
















0












0








0







I have a bunch of log files which are named according to their creation dates. For example; if my log file is created on 12 March 2018, the name of the logfile is log-2018-03-12.log



Here is what I want to do: From today's date, I want to check the name of my log files and zip the log files which are created in last 10 days.



Here is my code that zip all log files in a specific directory:



#!/bin/bash

# current time
now=$(date +"%F")

backupfile="backup-$now"
scripthome=/opt/MyStore/action_scripts/deneme/

tendaysbefore= date -d "$now - 10 days" '+%F'

for file in $scripthome;
do
find "$(basename "$file")" | zip -R $backupfile.zip "log-2*.log"
done


But I want to zip last 10 days log file, not all log files, and also I want to continue doing it for every 10 days after this. Also, after having zip file, I want to delete old log files.
In other words, I am trying to write a log-backup script. Can you help me please?



Thank you very much!










share|improve this question















I have a bunch of log files which are named according to their creation dates. For example; if my log file is created on 12 March 2018, the name of the logfile is log-2018-03-12.log



Here is what I want to do: From today's date, I want to check the name of my log files and zip the log files which are created in last 10 days.



Here is my code that zip all log files in a specific directory:



#!/bin/bash

# current time
now=$(date +"%F")

backupfile="backup-$now"
scripthome=/opt/MyStore/action_scripts/deneme/

tendaysbefore= date -d "$now - 10 days" '+%F'

for file in $scripthome;
do
find "$(basename "$file")" | zip -R $backupfile.zip "log-2*.log"
done


But I want to zip last 10 days log file, not all log files, and also I want to continue doing it for every 10 days after this. Also, after having zip file, I want to delete old log files.
In other words, I am trying to write a log-backup script. Can you help me please?



Thank you very much!







linux shell date






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 6:02

























asked Nov 19 '18 at 13:14









smsms

54




54












  • Does this help you? stackoverflow.com/questions/158044/…
    – Dominique
    Nov 19 '18 at 13:21










  • @Dominique I tried adding find . -type f -newerct $tendaysbefore ! -newerct $now But it gives me this error: find: I cannot figure out how to interpret ‘!’ as a date or time
    – smsms
    Nov 19 '18 at 13:53












  • That is because your $tendaysbefore is empty thus executing the actual command find . -type f -newerct ! …. Probably because your script above is incorrect. At the very least you want something like: tendaysbefore=$(date -d "$now - 10 days" '+%F')
    – grifferz
    Nov 19 '18 at 21:22










  • Actually it is not empty. I can print the value in it and it is correct. The error is most probably caused by syntax that '!' is not understood by the compiler. Any suggestion about this? @grifferz
    – smsms
    Nov 20 '18 at 6:14










  • I'm sorry, but you are mistaken. Running your code above results in tendaysbefore being left empty, that is why find gives you that error. Your assignment to tendaysbefore is missing $() around the date command as I said.
    – grifferz
    Nov 20 '18 at 15:00




















  • Does this help you? stackoverflow.com/questions/158044/…
    – Dominique
    Nov 19 '18 at 13:21










  • @Dominique I tried adding find . -type f -newerct $tendaysbefore ! -newerct $now But it gives me this error: find: I cannot figure out how to interpret ‘!’ as a date or time
    – smsms
    Nov 19 '18 at 13:53












  • That is because your $tendaysbefore is empty thus executing the actual command find . -type f -newerct ! …. Probably because your script above is incorrect. At the very least you want something like: tendaysbefore=$(date -d "$now - 10 days" '+%F')
    – grifferz
    Nov 19 '18 at 21:22










  • Actually it is not empty. I can print the value in it and it is correct. The error is most probably caused by syntax that '!' is not understood by the compiler. Any suggestion about this? @grifferz
    – smsms
    Nov 20 '18 at 6:14










  • I'm sorry, but you are mistaken. Running your code above results in tendaysbefore being left empty, that is why find gives you that error. Your assignment to tendaysbefore is missing $() around the date command as I said.
    – grifferz
    Nov 20 '18 at 15:00


















Does this help you? stackoverflow.com/questions/158044/…
– Dominique
Nov 19 '18 at 13:21




Does this help you? stackoverflow.com/questions/158044/…
– Dominique
Nov 19 '18 at 13:21












@Dominique I tried adding find . -type f -newerct $tendaysbefore ! -newerct $now But it gives me this error: find: I cannot figure out how to interpret ‘!’ as a date or time
– smsms
Nov 19 '18 at 13:53






@Dominique I tried adding find . -type f -newerct $tendaysbefore ! -newerct $now But it gives me this error: find: I cannot figure out how to interpret ‘!’ as a date or time
– smsms
Nov 19 '18 at 13:53














That is because your $tendaysbefore is empty thus executing the actual command find . -type f -newerct ! …. Probably because your script above is incorrect. At the very least you want something like: tendaysbefore=$(date -d "$now - 10 days" '+%F')
– grifferz
Nov 19 '18 at 21:22




That is because your $tendaysbefore is empty thus executing the actual command find . -type f -newerct ! …. Probably because your script above is incorrect. At the very least you want something like: tendaysbefore=$(date -d "$now - 10 days" '+%F')
– grifferz
Nov 19 '18 at 21:22












Actually it is not empty. I can print the value in it and it is correct. The error is most probably caused by syntax that '!' is not understood by the compiler. Any suggestion about this? @grifferz
– smsms
Nov 20 '18 at 6:14




Actually it is not empty. I can print the value in it and it is correct. The error is most probably caused by syntax that '!' is not understood by the compiler. Any suggestion about this? @grifferz
– smsms
Nov 20 '18 at 6:14












I'm sorry, but you are mistaken. Running your code above results in tendaysbefore being left empty, that is why find gives you that error. Your assignment to tendaysbefore is missing $() around the date command as I said.
– grifferz
Nov 20 '18 at 15:00






I'm sorry, but you are mistaken. Running your code above results in tendaysbefore being left empty, that is why find gives you that error. Your assignment to tendaysbefore is missing $() around the date command as I said.
– grifferz
Nov 20 '18 at 15:00














1 Answer
1






active

oldest

votes


















0














#!/bin/bash
END=10
for ((i=1;i<=END;i++)); do
file=log-`date -d "$i days ago" +"%F"`.log
echo $file
done


With the above script you have file names for last 10 days. Later(inside loop) you can do whatever you want like adding it to existing zip or searching for its existence.



Edit:



Following code may be useful according to your requirement



#!/bin/bash

# current time
now=$(date +"%F")

backupfile="backup-$now"
scripthome=/home/bhanu/opt/MyStore/action_scripts/deneme/

tendaysbefore=`date -d "$now - 10 days" '+%F'`
for file in $scripthome;
do
zip -r -tt $now -t $tendaysbefore "$backupfile.zip" $scripthome/log-*.log > add.log 2>&1
zip "$backupfile.zip" -d "*" -tt $tendaysbefore > delete.log 2>&1
done





share|improve this answer























  • Thx for your answer. But I already have many log files and do not need to create any more. What I am asking for is how to find the ones that are created in last 10 days and I also want to zip these log files (which are created in last 10 days) in every 10 days. So this is like making a back-up script for my log files(which are created every day) in every 10 days. So your answer is not what I am looking for. Now, do you have any other suggestion?
    – smsms
    Nov 19 '18 at 14:51










  • Updated my answer
    – bhanu7k
    Nov 20 '18 at 7:38










  • Thanks! I guess it is working.. but I am a lil bit confused..what does 2>&1 mean? also -tt?
    – smsms
    Nov 20 '18 at 12:29










  • Oh I got 2>&1 but -tt and -t still are still confusing. Can you please briefly explain them? Thanks!
    – smsms
    Nov 20 '18 at 14:35










  • try to read the output of zip -h2. It will give description of lot of options using zip. -t and -tt are the options to set the date range.
    – bhanu7k
    Nov 21 '18 at 6:01













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375441%2fhow-to-check-my-file-is-created-within-10-days-in-shell-script%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














#!/bin/bash
END=10
for ((i=1;i<=END;i++)); do
file=log-`date -d "$i days ago" +"%F"`.log
echo $file
done


With the above script you have file names for last 10 days. Later(inside loop) you can do whatever you want like adding it to existing zip or searching for its existence.



Edit:



Following code may be useful according to your requirement



#!/bin/bash

# current time
now=$(date +"%F")

backupfile="backup-$now"
scripthome=/home/bhanu/opt/MyStore/action_scripts/deneme/

tendaysbefore=`date -d "$now - 10 days" '+%F'`
for file in $scripthome;
do
zip -r -tt $now -t $tendaysbefore "$backupfile.zip" $scripthome/log-*.log > add.log 2>&1
zip "$backupfile.zip" -d "*" -tt $tendaysbefore > delete.log 2>&1
done





share|improve this answer























  • Thx for your answer. But I already have many log files and do not need to create any more. What I am asking for is how to find the ones that are created in last 10 days and I also want to zip these log files (which are created in last 10 days) in every 10 days. So this is like making a back-up script for my log files(which are created every day) in every 10 days. So your answer is not what I am looking for. Now, do you have any other suggestion?
    – smsms
    Nov 19 '18 at 14:51










  • Updated my answer
    – bhanu7k
    Nov 20 '18 at 7:38










  • Thanks! I guess it is working.. but I am a lil bit confused..what does 2>&1 mean? also -tt?
    – smsms
    Nov 20 '18 at 12:29










  • Oh I got 2>&1 but -tt and -t still are still confusing. Can you please briefly explain them? Thanks!
    – smsms
    Nov 20 '18 at 14:35










  • try to read the output of zip -h2. It will give description of lot of options using zip. -t and -tt are the options to set the date range.
    – bhanu7k
    Nov 21 '18 at 6:01


















0














#!/bin/bash
END=10
for ((i=1;i<=END;i++)); do
file=log-`date -d "$i days ago" +"%F"`.log
echo $file
done


With the above script you have file names for last 10 days. Later(inside loop) you can do whatever you want like adding it to existing zip or searching for its existence.



Edit:



Following code may be useful according to your requirement



#!/bin/bash

# current time
now=$(date +"%F")

backupfile="backup-$now"
scripthome=/home/bhanu/opt/MyStore/action_scripts/deneme/

tendaysbefore=`date -d "$now - 10 days" '+%F'`
for file in $scripthome;
do
zip -r -tt $now -t $tendaysbefore "$backupfile.zip" $scripthome/log-*.log > add.log 2>&1
zip "$backupfile.zip" -d "*" -tt $tendaysbefore > delete.log 2>&1
done





share|improve this answer























  • Thx for your answer. But I already have many log files and do not need to create any more. What I am asking for is how to find the ones that are created in last 10 days and I also want to zip these log files (which are created in last 10 days) in every 10 days. So this is like making a back-up script for my log files(which are created every day) in every 10 days. So your answer is not what I am looking for. Now, do you have any other suggestion?
    – smsms
    Nov 19 '18 at 14:51










  • Updated my answer
    – bhanu7k
    Nov 20 '18 at 7:38










  • Thanks! I guess it is working.. but I am a lil bit confused..what does 2>&1 mean? also -tt?
    – smsms
    Nov 20 '18 at 12:29










  • Oh I got 2>&1 but -tt and -t still are still confusing. Can you please briefly explain them? Thanks!
    – smsms
    Nov 20 '18 at 14:35










  • try to read the output of zip -h2. It will give description of lot of options using zip. -t and -tt are the options to set the date range.
    – bhanu7k
    Nov 21 '18 at 6:01
















0












0








0






#!/bin/bash
END=10
for ((i=1;i<=END;i++)); do
file=log-`date -d "$i days ago" +"%F"`.log
echo $file
done


With the above script you have file names for last 10 days. Later(inside loop) you can do whatever you want like adding it to existing zip or searching for its existence.



Edit:



Following code may be useful according to your requirement



#!/bin/bash

# current time
now=$(date +"%F")

backupfile="backup-$now"
scripthome=/home/bhanu/opt/MyStore/action_scripts/deneme/

tendaysbefore=`date -d "$now - 10 days" '+%F'`
for file in $scripthome;
do
zip -r -tt $now -t $tendaysbefore "$backupfile.zip" $scripthome/log-*.log > add.log 2>&1
zip "$backupfile.zip" -d "*" -tt $tendaysbefore > delete.log 2>&1
done





share|improve this answer














#!/bin/bash
END=10
for ((i=1;i<=END;i++)); do
file=log-`date -d "$i days ago" +"%F"`.log
echo $file
done


With the above script you have file names for last 10 days. Later(inside loop) you can do whatever you want like adding it to existing zip or searching for its existence.



Edit:



Following code may be useful according to your requirement



#!/bin/bash

# current time
now=$(date +"%F")

backupfile="backup-$now"
scripthome=/home/bhanu/opt/MyStore/action_scripts/deneme/

tendaysbefore=`date -d "$now - 10 days" '+%F'`
for file in $scripthome;
do
zip -r -tt $now -t $tendaysbefore "$backupfile.zip" $scripthome/log-*.log > add.log 2>&1
zip "$backupfile.zip" -d "*" -tt $tendaysbefore > delete.log 2>&1
done






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 7:38

























answered Nov 19 '18 at 14:15









bhanu7k

535




535












  • Thx for your answer. But I already have many log files and do not need to create any more. What I am asking for is how to find the ones that are created in last 10 days and I also want to zip these log files (which are created in last 10 days) in every 10 days. So this is like making a back-up script for my log files(which are created every day) in every 10 days. So your answer is not what I am looking for. Now, do you have any other suggestion?
    – smsms
    Nov 19 '18 at 14:51










  • Updated my answer
    – bhanu7k
    Nov 20 '18 at 7:38










  • Thanks! I guess it is working.. but I am a lil bit confused..what does 2>&1 mean? also -tt?
    – smsms
    Nov 20 '18 at 12:29










  • Oh I got 2>&1 but -tt and -t still are still confusing. Can you please briefly explain them? Thanks!
    – smsms
    Nov 20 '18 at 14:35










  • try to read the output of zip -h2. It will give description of lot of options using zip. -t and -tt are the options to set the date range.
    – bhanu7k
    Nov 21 '18 at 6:01




















  • Thx for your answer. But I already have many log files and do not need to create any more. What I am asking for is how to find the ones that are created in last 10 days and I also want to zip these log files (which are created in last 10 days) in every 10 days. So this is like making a back-up script for my log files(which are created every day) in every 10 days. So your answer is not what I am looking for. Now, do you have any other suggestion?
    – smsms
    Nov 19 '18 at 14:51










  • Updated my answer
    – bhanu7k
    Nov 20 '18 at 7:38










  • Thanks! I guess it is working.. but I am a lil bit confused..what does 2>&1 mean? also -tt?
    – smsms
    Nov 20 '18 at 12:29










  • Oh I got 2>&1 but -tt and -t still are still confusing. Can you please briefly explain them? Thanks!
    – smsms
    Nov 20 '18 at 14:35










  • try to read the output of zip -h2. It will give description of lot of options using zip. -t and -tt are the options to set the date range.
    – bhanu7k
    Nov 21 '18 at 6:01


















Thx for your answer. But I already have many log files and do not need to create any more. What I am asking for is how to find the ones that are created in last 10 days and I also want to zip these log files (which are created in last 10 days) in every 10 days. So this is like making a back-up script for my log files(which are created every day) in every 10 days. So your answer is not what I am looking for. Now, do you have any other suggestion?
– smsms
Nov 19 '18 at 14:51




Thx for your answer. But I already have many log files and do not need to create any more. What I am asking for is how to find the ones that are created in last 10 days and I also want to zip these log files (which are created in last 10 days) in every 10 days. So this is like making a back-up script for my log files(which are created every day) in every 10 days. So your answer is not what I am looking for. Now, do you have any other suggestion?
– smsms
Nov 19 '18 at 14:51












Updated my answer
– bhanu7k
Nov 20 '18 at 7:38




Updated my answer
– bhanu7k
Nov 20 '18 at 7:38












Thanks! I guess it is working.. but I am a lil bit confused..what does 2>&1 mean? also -tt?
– smsms
Nov 20 '18 at 12:29




Thanks! I guess it is working.. but I am a lil bit confused..what does 2>&1 mean? also -tt?
– smsms
Nov 20 '18 at 12:29












Oh I got 2>&1 but -tt and -t still are still confusing. Can you please briefly explain them? Thanks!
– smsms
Nov 20 '18 at 14:35




Oh I got 2>&1 but -tt and -t still are still confusing. Can you please briefly explain them? Thanks!
– smsms
Nov 20 '18 at 14:35












try to read the output of zip -h2. It will give description of lot of options using zip. -t and -tt are the options to set the date range.
– bhanu7k
Nov 21 '18 at 6:01






try to read the output of zip -h2. It will give description of lot of options using zip. -t and -tt are the options to set the date range.
– bhanu7k
Nov 21 '18 at 6:01




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375441%2fhow-to-check-my-file-is-created-within-10-days-in-shell-script%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith