Say I have a symbolic link to a file, which has its version in its name. (The file is actually a jar file) Now say, that the version changes, i.e the old file I linked to is deleted and a new one is generated with a different version in its name. How do I generate a new link with the new name pointing to the newly generated jar file and delete the old link in a bash script.
What I have working this far is something like that:
cd $THE_FOLDER_OF_BROKEN_LINKS
find . -type l -exec test ! -e {} \; -print0 | xargs -0 -i readlink "{}" | sed -r "s#(.*/).*\.jar#\1#g"
Which gets me, for each of the broken links, a folder where the new jar file is located. What I tried doing next, is find the new jar files, like that:
cd $THE_FOLDER_OF_BROKEN_LINKS
find . -type l -exec test ! -e {} \; -print0 | xargs -0 -i find `readlink "{}" | sed -r "s#(.*/).*\.jar#\1#g"` -name "prefix*.jar"
Which should get me the new jars, and it does, but it works recursively, so I can't use it. I don't know if there's anything else matching this pattern in the recursive folders, all I know, that in this current folder there is only one jar matching the regex prefix*.jar, which is what I need to link to.
After that I need to actually create the link, which I suppose is not hard depending on how to find this one file I'm looking for, because the filename can stay the same (i.e. no second argument needed for ln). But I have no idea how to delete the broken links without calling the initial find command again.
Been at this problem for a while now, because I'm relatively new to Linux, but finally resorted to asking for help, because I really shouldn't spend that much time on such a trivial problem.