Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

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:

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.

share|improve this question
I ended up creating a python script that gets called by a find function in a bash script. The python script is given the broken link and it tries to guess, which new jar it should point to by simply removing everything after the last dash(which is where the version number always starts in my case). – Deiwin Dec 2 '12 at 14:20

I solved the same issue by creating a link without a version number by using gsub in awk to clean it from the filename, and replace the link(s) to upgraded jar files.

Since we use specific version number scheme, it wouldn't help to post it here, but if you really need it, ask and I'll add it.

A really basic example:

find ./ -name \*[0-9]\*.jar | awk '{dest=$0;gsub(/[0-9]*/,"");\
    print "ln -sf "dest" "$0}'
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.