I need to rename a lot of directories and files in my Linux/Solaris machines. There are many links that pointed to those directories/files.
So first I create the following script in order to find all directories that I want to rename (like DOMAIN.city.country
, DOMAIN.type.country
etc.)
and their links.
The script takes the old and new names of the node from file info.file
file. The first field in each line represents the old directory name and the second field represents the new directory name.
File find_and_recreate_link_to_new_dir.ksh
while read -r line ; do
[[ -z $line ]] && continue
name_old_dir=` echo $line | awk '{print $1}' `
name_new_dir=` echo $line | awk '{print $2}' `
find / -type l -exec ls -l 2>/dev/null '{}' \; | perl -ne'BEGIN { $str = shift(@ARGV); } print if /\Q$str\E/; ' $name_old_dir
done < /tmp/test/info.file
File /tmp/test/info.file
DOMAIN.city.country DOMAIN.world.country
DOMAIN.city1.country DOMAIN.world1.country
...
When I run my script I get this output
lrwxrwxrwx 1 root root 19 Mar 15 11:22 /var/test/info.domain2.com -> DOMAIN.city.country
lrwxrwxrwx 1 root root 19 Mar 15 11:22 /var/test/info.domain.com -> DOMAIN.city.country
lrwxrwxrwx 1 root root 19 Mar 15 11:22 /var/test/info.domain1.com -> DOMAIN.city.country
lrwxrwxrwx 1 root root 5 Mar 15 11:57 /var/test/DOMAIN.type.country -> mkdir
lrwxrwxrwx 1 root root 19 Mar 15 11:58 /var/test/info.tyep.com -> DOMAIN.type.country
...
Now I need to add to this script a section that will recreate the current links to the new directories. For example
/var/test/info.domain2.com -> DOMAIN.world.country
Please advise the best solution to automatically recreate the current links to the new directories.