First I want to apologize for my lack of knowledge; this is quite literally my first ever bash script and my Linux knowledge is basically nonexistent. Explanatory answers would be much appreciated; I'd like to learn the why not just the how.
I am trying to write a script to automate a tedious process that has to be done multiple times a day. In short, this script should prepare a set of files based on certain requirements. The script must either move a template file in and assign it a version number, or copy an existing file using a new version number, based on what is found inside of one particular directory (eg create if empty, copy if there's something there).
As an example the file might be named
YYYYMMDD_##_username_testfile.json
where the ## (version number) should be 01 for the first creation, then 02 for the copy, 03 for the copy after that, etc. A single directory could contain upwards of 10+ of these files, all with different dates and sequential version numbers, as well as other files using the same format but ending in something other than testfile.json
I can't seem to find a way to have the script look in the directory "test" for the file ending in testfile.json that has the highest version number, and if found extract the version number as a variable/parameter I can pass back into the cp sequence.
At this point I'm not even sure what command to be using; the internet has offered everything from grep to awk to listing and sorting...my head's spinning!
Any help would be appreciated. I've stripped all identifying info out, please let me know if you need more information.
20161025_02_foo_testfile.json
and20161026_01_foo_testfile.json
both exist, which one is the highest? – Gilles 'SO- stop being evil' Oct 25 '16 at 22:32find . -maxdepth 1 -name '*testfile.json' | cut -d_ -f2 | sort -n | tail -n1
maybe? You can modify that to use null terminators if your filenames are allowed to contain newlines. – steeldriver Oct 25 '16 at 22:34