7

I have a folder structure like this

/Class/Student/Unit/files

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).

#!/bin/sh
find . -mindepth 3 -maxdepth 3 -type d -name '*' -execdir \
find "{}" -type f -newer "{}"/MarkSheet* \;
3
  • 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

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

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 {} +
1
  • Thank-You I couldn't workout why it worked in a terminal but not in the script.
    – ArchNemSyS
    Jan 25, 2014 at 20:46
2

Try this:

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

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; 
done

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

2
  • 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .