new tool
[hband-tools.git] / user-tools / movesymlinks
bloba3eb6cf5aa7b9bacaee1e82b45cfd3f02c7a693c
1 #!/bin/bash
3 true <<END
4 =pod
6 =head1 NAME
8 movesymlinks - Rename file and correct its symlinks to keep point to it.
10 =head1 SYNOPSIS
12 movesymlinks I<OLDNAME> I<NEWNAME> [I<DIR> [I<DIR> [...]]]
14 Rename file I<OLDNAME> to I<NEWNAME> and search I<DIR> directories for symlinks
15 pointing to I<OLDNAME> and change them to point to I<NEWNAME>.
17 =cut
19 END
21 if [ $# = 0 -o "$1" = --help ]
22 then
23 pod2text < "$0"
24 if [ $# = 0 ]
25 then
26 exit -1
28 exit 0
31 set -e -o pipefail
32 set -u
34 oldname=$1
35 shift
36 newname=$1
37 shift
39 mv -v -n "$oldname" "$newname"
41 find "$@" -maxdepth 1 -type l -printf '%f %l\n' |\
42 while IFS=$'\t' read -r symlinkfile target
44 if [ "$target" = "$oldname" ]
45 then
46 ln -v -snf "$newname" "$symlinkfile"
48 done