This is fairly simple issue that has been bothering me. A little backstory. I have a folder full of scripts. These scripts takes data files *.dat
and generates output in *.eps
. The extension of my scripts is *.plt
. I create a one line shell script that runs all the *.plt
files in that folder.
#!/bin/sh
find . -name "*.plt" -exec {} \;
I just want to make sure that all the *.pdf
images I will use in my document are up to date. For a time, the one line script was good. But when the number of files is over 50, it takes some time to run. I rarely change the data files, but make changes to the *.plt
scripts frequently. The scripts are written in such way that a script named this_script_does_something.plt
will create a file called this_script_does_something.eps
.
Hence, here's my question.
- Is there way to write a refined shell script that executes only the
*.plt
files that are newer than the similarly called*.eps
?
I know I can do this in Python. But it seems like cheating. I also know that I can look for the newer *.eps
and execute all the *.plt
that are newer than this. This will solve my problem, for most practical cases. I just realized about this option while I was typing the question, so thank you SX. However, as a didactic exercise, and to solve my original doubt, I would like to search for individual cases: compare the modification time of each *.plt
with each *.eps
, and execute the script only when they are more recent than the output. Is it possible? Can it be done in a single line?
EDIT: I forgot to add, that the *.plt
scripts should also execute when there are no homonym *.eps
files, which normally means that the script is new and has not been executed yet.
I think I'd be using:
This uses the Bash/Korn shell operator
-nt
for 'newer than' (and there's the converse-ot
operator for 'older than'). I'm assuming the files are all in a single directory so there's no need for a recursive search. If that's not correct, then use a separate:(where
new-script.sh
is the script I just showed). Or use the Bash extension**
operator:You might need to set the Bash
nullglob
option:This generates nothing when an expansion does not match any files.
Also generate when the
.eps
file does not exist:The only not-completely-generic shell feature in this is the
-nt
operator. If your/bin/sh
doesn't support it, check the/bin/[
command — it might — or use Korn Shell or Bash instead of/bin/sh
in the shebang line.