TASK
I need to write a bash script that would archivd files by month. That is, files with modification dates of 2016-12
would be packed into the archive 2016-12_archive.tar.gz
, and files with modification dates of 2017-01
would be packed into 2017-01_archive.tar.gz
archive, etc.
Example:
FILE NAME MODIFICATION DATE
file1.log 2016-12-30 ----> 2016-12_archive.tar.gz
file2.log 2016-12-31 ----> 2016-12_archive.tar.gz
file3.log 2017-01-01 ----> 2017-01_archive.tar.gz
file4.log 2017-01-02 ----> 2017-01_archive.tar.gz
file5.log 2017-01-15 ----> 2017-01_archive.tar.gz
My difficulties
The main problems I have:
How to get the file modification date via bash?
How to process all the files in a given directory (so that they have been archived in an appropriate archive)?
My attempts to solve the problems
I have found two ways to find out the file modification date:
date -r $ file +% F
andfind dir -name filename -printf '% TY-% Tm-% Td \ n'
. They both do not work on the computer (OS AIX, I'm not a root). Also I can't get what command 'ls -lc` shows (it doesn't seem to modification date).I have only one idea: to get all modification file dates in the
YYYY-MM
format, and then create a list of their unique values. Then for each item in this list find all files with appropriate modification date.
Consolidated attempts
Using istat
to get modification date:
$ istat filename
Inode 86741 on device 10/8 File
Protection: rw-r-----
Owner: 6361(user2) Group: 621(norgroup)
Link count: 1 Length 116 bytes
Last updated: 16 февраля 2017 г., 14:25:11 MSK
Last modified: 16 февраля 2017 г., 14:25:11 MSK
Last accessed: 16 февраля 2017 г., 16:08:46 MSK
This is how I can get Last modified value for each file:
for logFile in *.log; do
mdfDate=$(istat $logFile | grep "Last modified");
echo $logFile $mdfDate
done
Output:
file1.log Last modified: 30 декабря 2016 г., 14:25:11 MSK
file2.log Last modified: 31 декабря 2016 г., 14:26:11 MSK
file3.log Last modified: 01 января 2017 г., 14:27:11 MSK
file4.log Last modified: 02 января 2017 г., 14:28:11 MSK
file5.log Last modified: 15 января 2017 г., 14:29:11 MSK
So the next step is to extract date in unix format.
For some reason cut
doesn't work correct. Awk
is too heavy and sophisticated. Maybe sed
?
istat
which should be a good first step.istat
output?istat
. It might help if you ran it and added the output you get to your question.istat
. Could you please help me?)