Search code examples
unixfindtcsh

Unix find -exec show "day of week" file modified


Using tcsh

I want to be able to find files and delete them if for example they were created on a Friday.

So far I have the following but I get "Illegal variable name.".

find previous/*/fmpsw/ -name "Daily_*" -exec date -r $(stat -f %m '{}') +%A \;

Any ideas?

Thank you


Solution

  • 1. Find the files.

    Here is a possibile solution

    find previous/*/fmpsw/ -name "Daily_*" -exec tcsh -c 'date -r `stat -f %m {}` +%A' \;
    

    The {} needs to be substituted by find before it is actually passed to the subshell which is spawn by

    `stat -f %m {}`
    

    2. Delete the files.

    Now we want to actually delete the files (e.g., those that were created on Friday). The construction here is rather more involved:

    find previous/*/fmpsw/ -name "Daily_*" -type f -exec tcsh -c '  \
        date -r `stat -c %n {}` +%A | grep Friday > /dev/null;      \
        test $? -eq 0 && rm -f {}                                   \
    ' \;
    

    Here we use grep to filter based on the chosen day-of-week.