Bash script tar specific files with retention period

Clash Royale CLAN TAG#URR8PPP
I am new to bash script. I need to keep file for retention period of one month and archive other.
Example:
File format: WE20160225.log (20160225 YYYYMMDD)
Say in the directory /oracle/Sales has 2 files:
WE20160130.log
WE20151201.log
I need to tar the WE20151201.log
But not WE20160130.log because the file is less than one month from now. How can I do this? (probably need to substring the filename)
I'm able to change directory but don't know what to do next. My logic is loop through the file and get the date from filename, then compare with current date.
I need to use the filename date, not the file modification date.
SALES_DIR="/oracle/sales/"
cd $SALES_DIR
bash
add a comment |
I am new to bash script. I need to keep file for retention period of one month and archive other.
Example:
File format: WE20160225.log (20160225 YYYYMMDD)
Say in the directory /oracle/Sales has 2 files:
WE20160130.log
WE20151201.log
I need to tar the WE20151201.log
But not WE20160130.log because the file is less than one month from now. How can I do this? (probably need to substring the filename)
I'm able to change directory but don't know what to do next. My logic is loop through the file and get the date from filename, then compare with current date.
I need to use the filename date, not the file modification date.
SALES_DIR="/oracle/sales/"
cd $SALES_DIR
bash
2
Any reason not to usefindto check on modification time (instead some number sequence that might, or might not be available as part of the filename and could be interpreted as a date? And then you create the tar file from the resulting file list.
– Anthon
Feb 25 '16 at 13:01
tar is not a tool that will expire files. backup software are.
– Archemar
Feb 25 '16 at 13:39
Can you expand on your question and confirm if the file modification dates line up with the filename date/times (if they do,findcan be used, if not, some other technique is required).
– EightBitTony
Feb 25 '16 at 13:59
add a comment |
I am new to bash script. I need to keep file for retention period of one month and archive other.
Example:
File format: WE20160225.log (20160225 YYYYMMDD)
Say in the directory /oracle/Sales has 2 files:
WE20160130.log
WE20151201.log
I need to tar the WE20151201.log
But not WE20160130.log because the file is less than one month from now. How can I do this? (probably need to substring the filename)
I'm able to change directory but don't know what to do next. My logic is loop through the file and get the date from filename, then compare with current date.
I need to use the filename date, not the file modification date.
SALES_DIR="/oracle/sales/"
cd $SALES_DIR
bash
I am new to bash script. I need to keep file for retention period of one month and archive other.
Example:
File format: WE20160225.log (20160225 YYYYMMDD)
Say in the directory /oracle/Sales has 2 files:
WE20160130.log
WE20151201.log
I need to tar the WE20151201.log
But not WE20160130.log because the file is less than one month from now. How can I do this? (probably need to substring the filename)
I'm able to change directory but don't know what to do next. My logic is loop through the file and get the date from filename, then compare with current date.
I need to use the filename date, not the file modification date.
SALES_DIR="/oracle/sales/"
cd $SALES_DIR
bash
bash
edited Feb 25 '16 at 15:58
hades
asked Feb 25 '16 at 12:45
hadeshades
1691312
1691312
2
Any reason not to usefindto check on modification time (instead some number sequence that might, or might not be available as part of the filename and could be interpreted as a date? And then you create the tar file from the resulting file list.
– Anthon
Feb 25 '16 at 13:01
tar is not a tool that will expire files. backup software are.
– Archemar
Feb 25 '16 at 13:39
Can you expand on your question and confirm if the file modification dates line up with the filename date/times (if they do,findcan be used, if not, some other technique is required).
– EightBitTony
Feb 25 '16 at 13:59
add a comment |
2
Any reason not to usefindto check on modification time (instead some number sequence that might, or might not be available as part of the filename and could be interpreted as a date? And then you create the tar file from the resulting file list.
– Anthon
Feb 25 '16 at 13:01
tar is not a tool that will expire files. backup software are.
– Archemar
Feb 25 '16 at 13:39
Can you expand on your question and confirm if the file modification dates line up with the filename date/times (if they do,findcan be used, if not, some other technique is required).
– EightBitTony
Feb 25 '16 at 13:59
2
2
Any reason not to use
find to check on modification time (instead some number sequence that might, or might not be available as part of the filename and could be interpreted as a date? And then you create the tar file from the resulting file list.– Anthon
Feb 25 '16 at 13:01
Any reason not to use
find to check on modification time (instead some number sequence that might, or might not be available as part of the filename and could be interpreted as a date? And then you create the tar file from the resulting file list.– Anthon
Feb 25 '16 at 13:01
tar is not a tool that will expire files. backup software are.
– Archemar
Feb 25 '16 at 13:39
tar is not a tool that will expire files. backup software are.
– Archemar
Feb 25 '16 at 13:39
Can you expand on your question and confirm if the file modification dates line up with the filename date/times (if they do,
find can be used, if not, some other technique is required).– EightBitTony
Feb 25 '16 at 13:59
Can you expand on your question and confirm if the file modification dates line up with the filename date/times (if they do,
find can be used, if not, some other technique is required).– EightBitTony
Feb 25 '16 at 13:59
add a comment |
2 Answers
2
active
oldest
votes
You can use something like this:
find somedir/* -mtime +5 -exec rm ;
It used find to find files older than 5 days and passes the found file over to delete.
add a comment |
Here is the script for compressing month old log files.
#!/bin/bash
files=($(find /oracle/Sales -mtime +30))
tar cvfz backup.tar.gz "$files[@]"
-mtime +number-of-days to describe the file which you need to backup.
For deleting the older file try this
find /oracle/Sales -mtime +30 | xargs -n1 echo rm
If you get the right output then remove echo from the line.
Assumes (maybe correctly, but maybe not) that the file has a timestamp which matches the filename, maybe sales data is written one month in arrears or maybe the file timestamps are different for some other reason. This might work but it's not a certainty.
– EightBitTony
Feb 25 '16 at 13:58
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%2f265721%2fbash-script-tar-specific-files-with-retention-period%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use something like this:
find somedir/* -mtime +5 -exec rm ;
It used find to find files older than 5 days and passes the found file over to delete.
add a comment |
You can use something like this:
find somedir/* -mtime +5 -exec rm ;
It used find to find files older than 5 days and passes the found file over to delete.
add a comment |
You can use something like this:
find somedir/* -mtime +5 -exec rm ;
It used find to find files older than 5 days and passes the found file over to delete.
You can use something like this:
find somedir/* -mtime +5 -exec rm ;
It used find to find files older than 5 days and passes the found file over to delete.
answered Feb 25 '16 at 13:06
SaschaSascha
1385
1385
add a comment |
add a comment |
Here is the script for compressing month old log files.
#!/bin/bash
files=($(find /oracle/Sales -mtime +30))
tar cvfz backup.tar.gz "$files[@]"
-mtime +number-of-days to describe the file which you need to backup.
For deleting the older file try this
find /oracle/Sales -mtime +30 | xargs -n1 echo rm
If you get the right output then remove echo from the line.
Assumes (maybe correctly, but maybe not) that the file has a timestamp which matches the filename, maybe sales data is written one month in arrears or maybe the file timestamps are different for some other reason. This might work but it's not a certainty.
– EightBitTony
Feb 25 '16 at 13:58
add a comment |
Here is the script for compressing month old log files.
#!/bin/bash
files=($(find /oracle/Sales -mtime +30))
tar cvfz backup.tar.gz "$files[@]"
-mtime +number-of-days to describe the file which you need to backup.
For deleting the older file try this
find /oracle/Sales -mtime +30 | xargs -n1 echo rm
If you get the right output then remove echo from the line.
Assumes (maybe correctly, but maybe not) that the file has a timestamp which matches the filename, maybe sales data is written one month in arrears or maybe the file timestamps are different for some other reason. This might work but it's not a certainty.
– EightBitTony
Feb 25 '16 at 13:58
add a comment |
Here is the script for compressing month old log files.
#!/bin/bash
files=($(find /oracle/Sales -mtime +30))
tar cvfz backup.tar.gz "$files[@]"
-mtime +number-of-days to describe the file which you need to backup.
For deleting the older file try this
find /oracle/Sales -mtime +30 | xargs -n1 echo rm
If you get the right output then remove echo from the line.
Here is the script for compressing month old log files.
#!/bin/bash
files=($(find /oracle/Sales -mtime +30))
tar cvfz backup.tar.gz "$files[@]"
-mtime +number-of-days to describe the file which you need to backup.
For deleting the older file try this
find /oracle/Sales -mtime +30 | xargs -n1 echo rm
If you get the right output then remove echo from the line.
edited Feb 25 '16 at 13:20
answered Feb 25 '16 at 13:09
MongrelMongrel
2,08131747
2,08131747
Assumes (maybe correctly, but maybe not) that the file has a timestamp which matches the filename, maybe sales data is written one month in arrears or maybe the file timestamps are different for some other reason. This might work but it's not a certainty.
– EightBitTony
Feb 25 '16 at 13:58
add a comment |
Assumes (maybe correctly, but maybe not) that the file has a timestamp which matches the filename, maybe sales data is written one month in arrears or maybe the file timestamps are different for some other reason. This might work but it's not a certainty.
– EightBitTony
Feb 25 '16 at 13:58
Assumes (maybe correctly, but maybe not) that the file has a timestamp which matches the filename, maybe sales data is written one month in arrears or maybe the file timestamps are different for some other reason. This might work but it's not a certainty.
– EightBitTony
Feb 25 '16 at 13:58
Assumes (maybe correctly, but maybe not) that the file has a timestamp which matches the filename, maybe sales data is written one month in arrears or maybe the file timestamps are different for some other reason. This might work but it's not a certainty.
– EightBitTony
Feb 25 '16 at 13:58
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%2f265721%2fbash-script-tar-specific-files-with-retention-period%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
2
Any reason not to use
findto check on modification time (instead some number sequence that might, or might not be available as part of the filename and could be interpreted as a date? And then you create the tar file from the resulting file list.– Anthon
Feb 25 '16 at 13:01
tar is not a tool that will expire files. backup software are.
– Archemar
Feb 25 '16 at 13:39
Can you expand on your question and confirm if the file modification dates line up with the filename date/times (if they do,
findcan be used, if not, some other technique is required).– EightBitTony
Feb 25 '16 at 13:59