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.

So I want to use find to return a list of all .html files in a given directory that have been last modified on a Monday. How can I do this?

share|improve this question
    
You really mean by a given day of the week and not by some actual date, right? –  slm Mar 24 at 21:53
    
Is this an XY problem, troll question, or is there a legitimate reason why you need to find all files modified on any day that happened to be a monday? –  Sammitch Mar 24 at 22:06

5 Answers 5

up vote 4 down vote accepted

I'm not sure of a good way to do this directly using only find or similar, but you can use find and grep:

find -printf '%Tw:%h/%f\0' | grep -z '^1:'

Since it's find, you can of course combine other flags:

find -name '*.html' -type f -printf '%Tw:%h/%f\0' | grep -z '^1:'

to get only files ending with .html.

Explanation

Here is my test directory:

$ ls -la
total 68
drwxr-xr-x  2 anthony anthony  4096 Mar 24 17:53 .
drwxrwxrwt 98 root    root    61440 Mar 24 17:55 ..
-rw-r--r--  1 anthony anthony     0 Mar 24 17:53 monday
-rw-r--r--  1 anthony anthony     0 Mar 23 11:12 sunday

find's -printf argument takes a format string. Here, %T means last modified time. The w after it means the day of the week, from 0 (Sunday) through 6 (Saturday). You can get the name with a (abbreviated: Mon) or A (full: Monday), but those are locale-specific. : gives a literal :. %h/%f is the path and file name. \0 null-separates the entries (like -print0 does).

So that prints out something like 1:./.<NULL>0:./sunday<NULL>1:./monday<NULL>, which if you replace the NULL with newline for readability with tr '\0' '\n' is:

1:./.
0:./sunday
1:./monday

Then grep -z '^1:' looks for things starting with 1:, which is Monday.

share|improve this answer

You can use find + grep + date Command:

find /path -iname "*.html" -printf "%TY%Tm%Td\t%p\n" | \
grep $(date -dlast-monday +%Y%m%d)

if you want to print only file name then use:

find /path -iname "*.html" -printf "%TY%Tm%Td\t%p\n" | \
awk "/$(date -dlast-monday +%Y%m%d)"'/{print $NF}'

OR

 find /path -iname "*.html" -printf "%Ta\t%p\n" | awk '/Mon/{print $NF}'
share|improve this answer

Though I find your question highly suspect, the answer is:

find ./ -type f -name '*.html' -printf "%Ta %p\n" | grep '^Mon' | sed 's:^Mon ::'

Which will find all files ending in .html that were last modified on any Monday in human history, according to the current timezone set on the server.

share|improve this answer

You can use find with stat to find all files that have a modification timestamp on a Monday as follows:

find . -type f -exec stat -t %a  {} \; | awk 'match($10, /"Mon"/) {print $16}'

The find command will find all files and stat them which will be piped to the awk filter. awk will match field number 10 which is the modification time of file with string "Mon" to extract the files modified on Mondays. The print $16 statement will print the filename. you can remove $16 to print all fields or use any number to print selected fields.

Example output of first part (on OS X ):

$ cd /usr ; find . -type file -exec stat -t %a  {} \;

16777218 20556154 -rwxr-xr-x 1 root wheel 0 19600 "Mon" "Thu" "Thu" "Sat" 4096 16 0x20 ./bin/du

16777218 20547205 -rwxr-xr-x 1 root wheel 0 14256 "Wed" "Thu" "Thu" "Wed" 4096 16 0x20 ./bin/dwarfdump

16777218 20586248 -rwxr-xr-x 2 root wheel 0 925 "Fri" "Thu" "Thu" "Sun" 4096 0 0x20 ./bin/easy_install

share|improve this answer
    
That badly breaks if your file names have spaces (or several other special characters) in them. –  derobert Mar 24 at 21:58
    
On my OS X, the filename gets printed last so, seems to be working for filenames with spaces. –  Ketan Mar 24 at 22:02
    
Interesting, here (on Linux, with GNU Coreutils), the file name is printed first... –  derobert Mar 24 at 22:04

You can get ls to output dates in a custom format

ls --time-style=+%A *.html | grep " Monday "

Assuming " Monday " is not in any file name.

share|improve this answer

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.