Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Actually I need to search for files and folder that were created in 2012 on Friday of every month.

I did some attempts using the FIND command but it didn't work.

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

The simplest way with find is:

find / -daystart -mtime +41 -mtime -408 \
  -printf "%M %n %u %g %10s %TY-%Tm-%Td %Ta %TH:%TM:%TS %h/%f\n" |
awk '($7=="Fri"){print}'

Adjust the -printf as required, I've made it look close to ls -l here. %T (and %A %C) let you use strftime() formatting for timestamps, %Ta being the day of the week. (You may need to adjust the day ranges 41 - 408, but that's really just an optimisation, you can just grep 2012, or adjust -printf to make it easier to grep.)

Edit: a more robust version, with some slight loss of clarity:

find / -daystart -mtime +41 -mtime -408 \
   -printf "%M %n %u %g %10s %TY-%Tm-%Td %Ta %TH:%TM:%TS\0%h/%f\0\0" |
gawk 'BEGIN{RS="\0\0"; FS="[\0]"} ($1~/ Fri /) { printf $2 "\0"}' | 
xargs -0 -n 1 -i ls -l "{}"

This emulates -print0, but each line has two \0 delimited fields, the filename being the second. Replace ls -l "{}" at the end with whatever you need to do to the file(s). I'm explicitly using gawk, other awks do not take so kindly to \0 bytes in RS/FS (updated to handle newlines in file names too).

Also, as suggested by mreithub you can use %Tu as well as, or instead of %Ta for a numbered weekday, a language independent option.

share|improve this answer
1  
Note that something like this will also show files with names containing Fri, even if they were never touched on a Friday. I guess you could work around that with a separator, splitting and checking each part. Still, it's a decent approximation using commonly available tools in a semi-readable manner, so +1. –  Michael Kjörling Feb 11 '13 at 10:45
1  
So, to simplify this, I'd use: find /mnt/music/Datenbank -mtime +41 -mtime -408 -printf "%Ta %p\n"|grep ^Fri|sed 's/^Fri //'. You might need to prepend a LC_ALL=C to make sure the week day is english –  mreithub Feb 11 '13 at 10:48
    
@MichaelKjörling Yep, fixed, thanks! –  mr.spuratic Feb 11 '13 at 10:48
    
I like it even better now, but can't upvote twice unfortunately :) @mreithub's solution in comment is good too. –  Michael Kjörling Feb 11 '13 at 10:51
2  
also note you could use `... -printf %Tu %p\n"|grep ^5|sed 's/^5 //' to make it language-independent (but a little less readable... –  mreithub Feb 11 '13 at 10:55
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.