How to find all files for same month and year?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to find all files for same month and year?
# 1  
Old 04-22-2014
How to find all files for same month and year?

Hi All,
I find all files for same month and year
lets say there are following files in directory

Code:
-rwxr-xr-x   1 user  userg         1596 Mar 19 2012 c.txt
-rwxr-xr-x   1 user  userg         1596 Mar 21 2012 d.txt
-rwxr-xr-x   1 user  userg         1596 Mar 22 2012 f.txt
-rwxr-xr-x   1 user  userg         1596 Mar 24 2012 h.txt
-rwxr-xr-x   1 user  userg         1596 Mar 25 2012 w.txt
-rwxr-xr-x   1 user  userg         1596 Feb 12 2012 q.txt
-rwxr-xr-x   1 user  userg         1596 Feb 21 2012 a.txt
-rwxr-xr-x   1 user  userg         1596 Feb 22 2012 s.txt
-rwxr-xr-x   1 user  userg         1596 Feb 23 2012 k.txt
-rwxr-xr-x   1 user  userg         1596 Feb 27 2012 j.txt
-rwxr-xr-x   1 user  userg         1596 Apr 12 2014 q.txt
-rwxr-xr-x   1 user  userg         1596 Apr 21 2014 a.txt
-rwxr-xr-x   1 user  userg         1596 Apr 22 2014 s.txt
-rwxr-xr-x   1 user  userg         1596 Apr 23 2014 k.txt
-rwxr-xr-x   1 user  userg         1596 Apr 27 2014 j.txt

output should be:
first set should be
Code:
-rwxr-xr-x   1 user  userg         1596 Mar 19 2012 c.txt
-rwxr-xr-x   1 user  userg         1596 Mar 21 2012 d.txt
-rwxr-xr-x   1 user  userg         1596 Mar 22 2012 f.txt
-rwxr-xr-x   1 user  userg         1596 Mar 24 2012 h.txt
-rwxr-xr-x   1 user  userg         1596 Mar 25 2012 w.txt

and second set should be
Code:
-rwxr-xr-x   1 user  userg         1596 Feb 12 2012 q.txt
-rwxr-xr-x   1 user  userg         1596 Feb 21 2012 a.txt
-rwxr-xr-x   1 user  userg         1596 Feb 22 2012 s.txt
-rwxr-xr-x   1 user  userg         1596 Feb 23 2012 k.txt
-rwxr-xr-x   1 user  userg         1596 Feb 27 2012 j.txt

third set should be
Code:
-rwxr-xr-x   1 user  userg         1596 Apr 12 2014 q.txt
-rwxr-xr-x   1 user  userg         1596 Apr 21 2014 a.txt
-rwxr-xr-x   1 user  userg         1596 Apr 22 2014 s.txt
-rwxr-xr-x   1 user  userg         1596 Apr 23 2014 k.txt
-rwxr-xr-x   1 user  userg         1596 Apr 27 2014 j.txt

# 2  
Old 04-22-2014
You could use ls -rlt to have ls order the listing by date/time.
How do you want the month sets separated? Blank line between each; output to filename with month in it; or something else?
# 3  
Old 04-22-2014
basically i want to itearte though each of this sets in
Code:
for loop

.
first time for loop should itearet though first set ...
second time second set & so on
# 4  
Old 04-22-2014
An oddly sorted output from ls -l indeed.

It might be easier to get the tools to do the work for you rather than to search and manipulate the output yourself. Using the touch command, you can create files with specific timestamps:-
Code:
$ touch -mt 201203010000 /tmp/my_begin
$ touch -mt 201204010000 /tmp/my_ended
$ ls -l /tmp/my_*
-rw-r--r--    1 RBATTE1  staff             0 01 Mar 2012  /tmp/my_begin
-rw-r--r--    1 RBATTE1  staff             0 01 Apr 2012  /tmp/my_ended

find /path/to/files -type f -newer /tmp/my_begin ! -newer /tmp/my_ended

You should be able to build a for loop around this to set the start and end dates.


I hope that this helps,
Robin
Liverpool/Blackburn
UK
# 5  
Old 04-22-2014
You could also increment a set# variable every time the month changes like this (ksh/bash code):

Code:
ls -rlt | while read line
do
   mth=$(echo "$line" | awk '{print $6}')
   if [ "$mth" != "$last" ]
   then
      ((set++))
      last="$mth"
   fi
   echo $set ":" $line
done

# 6  
Old 04-22-2014
Code:
ls -l | awk '{idx=$(NF-3) FS $(NF-1);a[idx]=a[idx]?a[idx] ORS $0:$0}END {for (i in a) print i ORS a[i] ORS}

Code:
Feb 2012
-rwxr-xr-x   1 user  userg         1596 Feb 12 2012 q.txt
-rwxr-xr-x   1 user  userg         1596 Feb 21 2012 a.txt
-rwxr-xr-x   1 user  userg         1596 Feb 22 2012 s.txt
-rwxr-xr-x   1 user  userg         1596 Feb 23 2012 k.txt
-rwxr-xr-x   1 user  userg         1596 Feb 27 2012 j.txt

Mar 2012
-rwxr-xr-x   1 user  userg         1596 Mar 19 2012 c.txt
-rwxr-xr-x   1 user  userg         1596 Mar 21 2012 d.txt
-rwxr-xr-x   1 user  userg         1596 Mar 22 2012 f.txt
-rwxr-xr-x   1 user  userg         1596 Mar 24 2012 h.txt
-rwxr-xr-x   1 user  userg         1596 Mar 25 2012 w.txt

Apr 2014
-rwxr-xr-x   1 user  userg         1596 Apr 12 2014 q.txt
-rwxr-xr-x   1 user  userg         1596 Apr 21 2014 a.txt
-rwxr-xr-x   1 user  userg         1596 Apr 22 2014 s.txt
-rwxr-xr-x   1 user  userg         1596 Apr 23 2014 k.txt
-rwxr-xr-x   1 user  userg         1596 Apr 27 2014 j.txt

# 7  
Old 04-23-2014
There are some very strange things going on in this thread:
Makarand Dodmis tells us that he has the following files in a directory:
Code:
-rwxr-xr-x   1 user  userg         1596 Mar 19 2012 c.txt
-rwxr-xr-x   1 user  userg         1596 Mar 21 2012 d.txt
-rwxr-xr-x   1 user  userg         1596 Mar 22 2012 f.txt
-rwxr-xr-x   1 user  userg         1596 Mar 24 2012 h.txt
-rwxr-xr-x   1 user  userg         1596 Mar 25 2012 w.txt
-rwxr-xr-x   1 user  userg         1596 Feb 12 2012 q.txt
-rwxr-xr-x   1 user  userg         1596 Feb 21 2012 a.txt
-rwxr-xr-x   1 user  userg         1596 Feb 22 2012 s.txt
-rwxr-xr-x   1 user  userg         1596 Feb 23 2012 k.txt
-rwxr-xr-x   1 user  userg         1596 Feb 27 2012 j.txt
-rwxr-xr-x   1 user  userg         1596 Apr 12 2014 q.txt
-rwxr-xr-x   1 user  userg         1596 Apr 21 2014 a.txt
-rwxr-xr-x   1 user  userg         1596 Apr 22 2014 s.txt
-rwxr-xr-x   1 user  userg         1596 Apr 23 2014 k.txt
-rwxr-xr-x   1 user  userg         1596 Apr 27 2014 j.txt

Note that there are several pairs of files with the same names, but different dates. Obviously this can' t happen in a single directory!

Standard ls -lt or ls -ltr output would sort the dates from oldest to newest or newest to oldest; not in the order shown above with the oldest files in the middle of the listings.

And, files with dates in the past six months would be shown with a timestamp rather than a year in the date display. (This means that some files from October 2013 will be displayed with a timestamp and some will be presented with a year if ls -l is run in April 2014.

And, finally, ls -l with or without other options will also print a line something like:
Code:
total 75

at the start of the output.

Chubler_XL's script ignores the year and assumes that grouping by month will be sufficient to get what is desired. (And, it works for the sample data given. Although it also prints the "total" line.)

vgersh99's script gets a syntax error on the "total" line and treats each timestamp on files in the previous six months as a different year.

In a directory where ls -lrt (when run on April 22, 2014 after 8pm) produces the output:
Code:
total 40
-rw-r--r--  1 dwc  staff     0 Feb 12  2012 q.txt
-rw-r--r--  1 dwc  staff     0 Feb 21  2012 a.txt
-rw-r--r--  1 dwc  staff     0 Feb 22  2012 s.txt
-rw-r--r--  1 dwc  staff     0 Feb 23  2012 k.txt
-rw-r--r--  1 dwc  staff     0 Feb 27  2012 j.txt
-rw-r--r--  1 dwc  staff     0 Mar 19  2012 c.txt
-rw-r--r--  1 dwc  staff     0 Mar 21  2012 d.txt
-rw-r--r--  1 dwc  staff     0 Mar 22  2012 f.txt
-rw-r--r--  1 dwc  staff     0 Mar 24  2012 h.txt
-rw-r--r--  1 dwc  staff     0 Mar  1  2013 y.txt
-rw-r--r--  1 dwc  staff     0 Oct  1  2013 b.txt
-rw-r--r--  1 dwc  staff     0 Oct 31 12:00 z.txt
-rw-r--r--  1 dwc  staff     0 Apr 12 01:02 e.txt
-rw-r--r--  1 dwc  staff     0 Apr 21 03:04 g.txt
-rw-r--r--  1 dwc  staff     0 Apr 22 05:06 i.txt
-rw-r--r--  1 dwc  staff  2266 Apr 22 08:22 problem
-rwxr-xr-x  1 dwc  staff   186 Apr 22 18:12 Chubler
-rwxr-xr-x  1 dwc  staff   135 Apr 22 18:23 vgersh99
-rwxr-xr-x  1 dwc  staff   579 Apr 22 19:32 tester.ksh
-rwxr-xr-x  1 dwc  staff   410 Apr 22 19:32 tester
-rw-r--r--  1 dwc  staff     0 Apr 23  2014 m.txt
-rw-r--r--  1 dwc  staff     0 Apr 27  2014 n.txt

the awk script:
Code:
#!/bin/ksh
ls -lrt | awk -v cy=$(date +%Y) '
BEGIN {	y["Jan"] = y["Feb"] = y["Mar"] = y["Apr"] = y["May"] = y["Jun"] = " " cy
	y["Jul"] = y["Aug"] = y["Sep"] = y["Oct"] = y["Nov"] = y["Dec"] = " " cy - 1
}
NF > 8 {if(length($8) == 4)	# Do we have a year or a timestamp?
		my = $6 " " $8	#   year
	else	my = $6 y[$6]	#   timestamp
	if(my != last) {
		set++
		last = my
	}
	printf("%3d:%s:%s\n", set, my, $0)
}'

(change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk on Solaris/SunOS systems) and the following Korn shell script (when using a 1993 or later version of ksh):
Code:
#!/bin/ksh
cy=$(date +%Y)
last=""
ly=$((cy - 1))
set=""
typeset -A y
y["Jan"]=$cy
y["Feb"]=$cy
y["Mar"]=$cy
y["Apr"]=$cy
y["May"]=$cy
y["Jun"]=$cy
y["Jul"]=$ly
y["Aug"]=$ly
y["Sep"]=$ly
y["Oct"]=$ly
y["Nov"]=$ly
y["Dec"]=$ly
ls -lrt | while read -r line
do	if [ "$set" == "" ]
	then	# skip "total" line from ls
		set=0
		continue
	fi
	set -- $line
	if [ ${#8} -eq 4 ]		# Do we have a year or a timestamp?
	then	my="$6 $8"		#   year
	else	my="$6 ${y[$6]}"	#   timestamp
	fi
	if [ "$my" != "$last" ]
	then
		((set++))
		last="$my"
	fi
	printf "%3d:%s:%s\n" $set "$my" "$line"
done

both produce the output:
Code:
  1:Feb 2012:-rw-r--r--  1 dwc  staff     0 Feb 12  2012 q.txt
  1:Feb 2012:-rw-r--r--  1 dwc  staff     0 Feb 21  2012 a.txt
  1:Feb 2012:-rw-r--r--  1 dwc  staff     0 Feb 22  2012 s.txt
  1:Feb 2012:-rw-r--r--  1 dwc  staff     0 Feb 23  2012 k.txt
  1:Feb 2012:-rw-r--r--  1 dwc  staff     0 Feb 27  2012 j.txt
  2:Mar 2012:-rw-r--r--  1 dwc  staff     0 Mar 19  2012 c.txt
  2:Mar 2012:-rw-r--r--  1 dwc  staff     0 Mar 21  2012 d.txt
  2:Mar 2012:-rw-r--r--  1 dwc  staff     0 Mar 22  2012 f.txt
  2:Mar 2012:-rw-r--r--  1 dwc  staff     0 Mar 24  2012 h.txt
  3:Mar 2013:-rw-r--r--  1 dwc  staff     0 Mar  1  2013 y.txt
  4:Oct 2013:-rw-r--r--  1 dwc  staff     0 Oct  1  2013 b.txt
  4:Oct 2013:-rw-r--r--  1 dwc  staff     0 Oct 31 12:00 z.txt
  5:Apr 2014:-rw-r--r--  1 dwc  staff     0 Apr 12 01:02 e.txt
  5:Apr 2014:-rw-r--r--  1 dwc  staff     0 Apr 21 03:04 g.txt
  5:Apr 2014:-rw-r--r--  1 dwc  staff     0 Apr 22 05:06 i.txt
  5:Apr 2014:-rw-r--r--  1 dwc  staff  2266 Apr 22 08:22 problem
  5:Apr 2014:-rwxr-xr-x  1 dwc  staff   186 Apr 22 18:12 Chubler
  5:Apr 2014:-rwxr-xr-x  1 dwc  staff   135 Apr 22 18:23 vgersh99
  5:Apr 2014:-rwxr-xr-x  1 dwc  staff   579 Apr 22 19:32 tester.ksh
  5:Apr 2014:-rwxr-xr-x  1 dwc  staff   410 Apr 22 19:32 tester
  5:Apr 2014:-rw-r--r--  1 dwc  staff     0 Apr 23  2014 m.txt
  5:Apr 2014:-rw-r--r--  1 dwc  staff     0 Apr 27  2014 n.txt

This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to find all files other than last two dates per month and year?

Hi All, lets say there are following files in directory -rwxr-xr-x 1 user userg 1596 Mar 19 2012 a.txt -rwxr-xr-x 1 user userg 1596 Mar 19 2012 b.txt -rwxr-xr-x 1 user userg 1596 Mar 22 2012 c.txt -rwxr-xr-x 1 user userg 1596 Mar 24 2012 d.txt... (16 Replies)
Discussion started by: Makarand Dodmis
16 Replies

2. Shell Programming and Scripting

How to find all files other than first two dates & last date per month and year?

how to find all files other than first two dates & last date per month and year Hi All, lets say there are following files in directory -rwxr-xr-x 1 user userg 1596 Mar 19 2012 a.txt -rwxr-xr-x 1 user userg 1596 Mar 19 2012 b.txt -rwxr-xr-x 1 user userg ... (6 Replies)
Discussion started by: Makarand Dodmis
6 Replies

3. UNIX for Advanced & Expert Users

Find all files other than first two files dates & last file date for month

Hi All, I need to find all files other than first two files dates & last file date for month and month/year wise list. lets say there are following files in directory Mar 19 2012 c.txt Mar 19 2012 cc.txt Mar 21 2012 d.txt Mar 22 2012 f.txt Mar 24 2012 h.txt Mar 25 2012 w.txt Feb 12... (16 Replies)
Discussion started by: Makarand Dodmis
16 Replies

4. Shell Programming and Scripting

How to list files that are not first two files date & last file date for every month-year?

Hi All, I need to find all files other than first two files dates & last file date for month and month/year wise list. lets say there are following files in directory Mar 19 2012 c.txt Mar 19 2012 cc.txt Mar 21 2012 d.txt Mar 22 2012 f.txt Mar 24 2012 h.txt Mar 25 2012 w.txt Feb 12... (2 Replies)
Discussion started by: Makarand Dodmis
2 Replies

5. UNIX for Dummies Questions & Answers

Unix man command to find out month of the year?

how can i display month of the year i was born with using man command? thanks (2 Replies)
Discussion started by: janetroop95
2 Replies

6. Shell Programming and Scripting

want to get last year and month from the file

Hi I have files like abc_cd_20110302_123423 abc_cd_ef_20110301_123423 abc_cd_ef_20110403_123423 abc_ef_20110401_123423 I want to extract the the year and month associated with each file. I tried logfileyearmonth=`echo $logfile | awk -F_'{print $NF}'` Any other way can I... (6 Replies)
Discussion started by: dgmm
6 Replies

7. Programming

Interval year to month

Hi, I'm working on a Informix4gl module. I'm just trying to find out any built-in function to fetch only the year/month from an INTERVAL YEAR TO MONTH data value. Please let me know, if there are any functions to do this. If not, let me know for any alternative solutions to attain this. ... (5 Replies)
Discussion started by: dvah
5 Replies

8. Shell Programming and Scripting

Concatenating Files In A Year/Month/Day File Structure

Hi Im trying to concatenate a specific file from each day in a year/month/day folder structure using Bash or equivalent. The file structure ends up like this: 2009/01/01/products 2009/01/02/products .... 2009/12/31/products The file I need is in products everyday and I need the script to... (3 Replies)
Discussion started by: Grizzly
3 Replies

9. UNIX for Advanced & Expert Users

how to get the last month and year in UNIX

how to get the last month and year in UNIx (2 Replies)
Discussion started by: Vijay06
2 Replies

10. UNIX for Advanced & Expert Users

find files with a perticular year of access

Hello all, Might be a silly question, on my AIX machine the year had changed to 2022 and some files were accessed on this date hence the time stamp on these files is with year 2022, there are many such files. i want to list all these file from the root dir and subdir with 2022 year... (3 Replies)
Discussion started by: pradeepmacha
3 Replies
Login or Register to Ask a Question