Get last month files

Hi All,

How to get last month files. Ex : 1st Jan i have to get Dec 31 days files and on Feb 1st i have to get Jan 31 days files and on Mar 1st i have to get Feb 28 days files. Below are the example files with date and timestamp.
aaa.txt.timestamp
aaa.txt.timestamp
aaa.txt.timestamp
Please let me know how can i achieve this in a command and redirect all the files to one file.
Thanks.

The simplest, accurate way is to use the modify time stamp and the 'find' comand. Some versions of 'find' allow direct references ( -newerXY datetime1 \! -newerXY datetime2, others need marker files ( -newer file1 \! -newer file2 ) that you can make with 'touch'. The logic should be >= MM/01/YYYY 00:00:00 but < MM+1/01/YYYY 00:00:00. Obviously, it needs to be at least the next month to get a complete list.

Later GNU date commands can help you generate the date strings, or you may need to write a little year-month date arithmetic in shell $(( ... )), i.e., 201501 - 1 = 201500, needs correction from 2015 to 2014 and 00 to 12.

Working off the first second of the month frees you from month length and leap year issues.

There is a little booble when DST time shifts in and out, as the month will be a hour short or long. You might want to export a $TZ that does not do DST (GMT?), so you do not have some files never or twice.

Generally, it's doable, but not "in a command", though (imho). Your timestamps are not meaningful, at all, because there are tons of different timestamps. Please update your post with representative timestamps. Ideally, they are in YYYYMMDD format. Do you have a bash and GNU cal on your system?

my file is in the format

aaa.txt.yyyymmdd
aaa.txt.yyyymmdd

Please let me know the command or script how to get last month files and if it is new year how to get last year dec files.

What have you tried so far?
The 2 above posts give you the idea... The command there are no for such task and the script you will have to write if you have not found one in the numerous fora here, we are here do help you with the writing, not to do the job for you

Hello kiranparsha,

Following may help you in same to get last year month December 's files. I just created some test files and got the output.

Test files with last year's dates are as follows.

-rw-r--r-- 1 Singh420 Singh420     0 Dec  3  2013 aaa.txt20131212
-rw-r--r-- 1 Singh420 Singh420     0 Dec  4  2013 aaa.txt20131216
-rw-r--r-- 1 Singh420 Singh420     0 Dec  9  2013 aaa.txt90904383
-rw-r--r-- 1 Singh420 Singh420     0 Dec  9  2013 aaa.txt.20131201
-rw-r--r-- 1 Singh420 Singh420     0 Dec  9  2013 aaa.txt.20131200
-rw-r--r-- 1 Singh420 Singh420    64 Nov 21 08:11 xaa.txt
-rw-r--r-- 1 Singh420 Singh420   153 Dec 11 04:44 dataa.txt
-rw-r--r-- 1 Singh420 Singh420     0 Dec 19 07:53 aaa.txt20131213
-rw-r--r-- 1 Singh420 Singh420     0 Dec 19 07:53 aaa.txt20131214
-rw-r--r-- 1 Singh420 Singh420     0 Dec 19 07:53 aaa.txt20131215
for i in aaa.txt`date -d"1 year ago" +%Y%m`*
do
    echo $i
done

Output will be as follows.

aaa.txt20131212
aaa.txt20131213
aaa.txt20131214
aaa.txt20131215
aaa.txt20131216

Thanks,
R. Singh

1 Like

RavinderSingh13,
not bad :eek: +1 for this cheat :D:b:

I was thinking waaay too abstract:

  • get current year and month
  • decreasing month by 1 or setting month to 12 and descreasing year by 1, as the case may be (within case / esac )
  • parsing cal -d year-month output to get the last day of the previous month (to have a valid from-to range for the particular month)
  • in the for loop: comparing timestamps if ... -ge ... && ... -le ... bla blah

In fact, the OP needs to change one single word from your code (and maybe adding a period . after txt) to get all files he needs.

1 Like

I don't think s/he wants last year's december files, but just last month's files even if run in January. Try - should GNU date be available on your system -

ls aaa.txt.$(date -d-$(( $(date +%d) ))days +%Y%m)??