I am writing a script to create/copy files to speed up workflow. In this instance a directory needs to be searched for files ending in a specific string, and if one or more is found the last one in the sequence needs to be targeted to be copied. (If none are found, the script copies a fresh file in from a different directory).
This directory can contain multiple sets of files that all follow the format;
YYYYMMDD_##_user_filename.json
So an example folder might contain the following;
20161021_01_test_parameters.json
20161021_01_test_stack.json
20161021_02_test_parameters.json
20161021_02_test_stack.json
20161022_03_test_parameters.json
20161025_04_test_parameters.json
20161025_04_test_stack.json
In this instance I need the script to read for any file ending in parameters.json
and then sort them based on their version number (01, 02, etc) to discover which is the last one in the sequence. This file should then be copied into the same folder using the next version number. For this purpose the date is irrelevant and you can assume there will never be duplicate version numbers.
I was using if [ -e $1/*"parameters.json" ]; then cp $1/*"parameters.json" "$FILENAME"_parameters.json
(where $1
is the directory these files are being moved about in - important because the script is located outside of the directories to be acted upon, and $FILENAME
is just a variable to calculate the location, date, version number, etc) but of course this doesn't do anything about finding the most recent one.