Created
August 5, 2012 11:56
-
-
Save pcworld/3264177 to your computer and use it in GitHub Desktop.
Recursively update symlinks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#For updated versions see https://gist.github.com/pcworld | |
#Copyright (c) 2012, "pcworld", [email protected] | |
#All rights reserved. | |
# | |
#Redistribution and use in source and binary forms, with or without | |
#modification, are permitted provided that the following conditions are met: | |
# * Redistributions of source code must retain the above copyright | |
# notice, this list of conditions and the following disclaimer. | |
# * Redistributions in binary form must reproduce the above copyright | |
# notice, this list of conditions and the following disclaimer in the | |
# documentation and/or other materials provided with the distribution. | |
# * Neither the name of the author nor the | |
# names of its contributors may be used to endorse or promote products | |
# derived from this software without specific prior written permission. | |
# | |
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
#ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
#WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
#DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY | |
#DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
#(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
#ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
#(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
#SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
if [ $# -ne 3 ] # http://www.linuxweblog.com/bash-argument-numbers-check | |
then | |
echo "Recursively updates the target directories of matching symlinks in a given directory with new ones." | |
echo "Usage: `basename $0` <directory to search for symlinks in> <path of the symlink target to be replaced> <replace with this>" | |
echo | |
echo "Example: `basename $0` /home/myuser /opt/formerfolder /var/test/newfolder" | |
echo "This will search for symlinks in /home/myuser whose target contains \"/opt/formerfolder\" and will replace \"/opt/formerfolder/\" with \"/var/test/newfolder/\" in their targets." | |
echo "Note: If the symlink targets are relative (and not absolute), you might have to consider that for the second parameter." | |
exit 1 | |
fi | |
#http://stackoverflow.com/a/1848456 | |
SEARCHFOR=${2%/} | |
REPLACEBY=${3%/} | |
#http://superuser.com/a/157832 | |
find $1 -type l \ | |
-lname "$SEARCHFOR/*" \ | |
-printf 'ln -nsf $(readlink %p | sed s,'"$SEARCHFOR"'/,'"$REPLACEBY"'/,) $(echo %p | sed s,'"$SEARCHFOR"'/,'"$REPLACEBY"'/,)\n' \ | |
| bash | |
#readlink: use "-v" to display error messages (e.g. if file doesn't exist) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment