I have a folder structure like this


Each Unit folder contains a file MarkSheet* that I update when I mark a students work.

I need a script that tests if the student has uploaded files newer than when I last marked their work.

The following is as close as I have been able to get (which doesn't work).

find . -mindepth 3 -maxdepth 3 -type d -name '*' -execdir \
find "{}" -type f -newer "{}"/MarkSheet* \;
  • Is the fact that the folder is Grive relevant? If not, take it out; if yes, explain what it is (apparently some Google drive client thing). Jan 25, 2014 at 17:03
  • I take it that Student is variable and you want to do this for each Student folder? Why are you setting maxdepth and mindepth?
    – terdon
    Jan 25, 2014 at 17:05
  • Student is a folder ie 10x3/John Smith/Unit_212/Marksheet_212_John_Smith.xls. The min/max depth ensure i get just the subdir Im interested in.
    – ArchNemSyS
    Jan 25, 2014 at 19:45

2 Answers 2

for ms in Class/*/*/MarkSheet*; do
  find "${ms%/*}" -type f -newer "$ms"

With your approach, you need to a shell to expand the MarkSheet* glob. So:

find . -mindepth 3 -maxdepth 3 -type d -exec sh -c '
  for dir do
    find "$dir" -type f -newer "$dir"/MarkSheet*
  done' sh {} +
  • Thank-You I couldn't workout why it worked in a terminal but not in the script.
    – ArchNemSyS
    Jan 25, 2014 at 20:46

Try this:

find /Class/ -maxdepth 1 -mindepth 1 -type d | while IFS= read -r student; do
    find "$student" -type f -newer "$student/Unit/MarkSheet"*

The first find looks for the student directories and the second for files neer than the corresponding MarkSheet.

You can also do it the other way around:

find Class/ -name 'MarkSheet*' | while IFS= read -r mark; do 
  find "$(dirname "$mark")" -newer "$mark" -type f; 

The trick here is to use dirname to get the name of the directory containing the MarkSheet* file.

  • Sorry forgot to mention it needs to support spaces in names
    – ArchNemSyS
    Jan 25, 2014 at 20:32
  • @ArchNemSyS sorry, my bad, should have assumed that. Updated answer should work with spaces but Stephane's is better anyway.
    – terdon
    Jan 25, 2014 at 20:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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