How can I change the symlink path for many files?
I was replaced by the directory name. There are thousands of files in this directory. Some projects use these files, projects have symbolic links.
- How do I find all the symlinks that got the folder name from their address?
- How can I change all these symbolic links to a different path automatically?
if only 2 bash scripts with deleting and creating a new one - I will do it, but maybe you know an easier way?
source to share
-
It's a bit difficult, but it can be done using
find
,readlink
, checks to verify whether the symbolic link relative or not, andsed
to get rid of..
the way names (copied 1: 1 from the answer ).
(Note that most convenience methods (for examplereadlink -f
) are not available due to the unavailability of existing symlinks.)
Assuming your old path/var/lib/old/path
:oldpath='/var/lib/old/path'; find / -type l -execdir bash -c 'p="$(readlink "{}")"; if [ "${p:0:1}" != "/" ]; then p="$(echo "$(pwd)/$p" | sed -e "s|/\./|/|g" -e ":a" -e "s|/[^/]*/\.\./|/|" -e "t a")"; fi; if [ "${p:0:'${#oldpath}'}" == "'"$oldpath"'" ]; then ...; fi;' \;
-
Now replace the
...
top withln -sf
(-f
to override the existing link).
Assuming your new path is/usr/local/my/awesome/new/path
:oldpath='/var/lib/old/path'; newpath='/usr/local/my/awesome/new/path'; find / -type l -execdir bash -c 'p="$(readlink "{}")"; if [ "${p:0:1}" != "/" ]; then p="$(echo "$(pwd)/$p" | sed -e "s|/\./|/|g" -e ":a" -e "s|/[^/]*/\.\./|/|" -e "t a")"; fi; if [ "${p:0:'${#oldpath}'}" == "'"$oldpath"'" ]; then ln -sf "'"$newpath"'${p:'${#oldpath}'}" "{}"; fi;' \;
Note that oldpath
and newpath
must be absolute paths.
Also note that this will convert all relative symbolic links to absolute.
One could keep them relative, but only with great effort.
Destruction
For those of you who care what that means one-line hell,
-
find
- cool executable file -
/
- where to look, in this case the system root -
-type l
- matching symbolic links -
-execdir
- for each match, the following command is launched in the directory of the corresponding file:-
bash
- well, bash -
-c
- execute the following line (delete and delete'
):-
p="$(readlink "{}")";
- starting from the innermost:-
"
- run the line to make sure there is no extension. -
{}
- placeholder for the name of the associated file (function-execdir
) -
"
- end of line -
readlink ...
- find out where the symlink points to -
p="$(...)"
- and save the result to$p
-
-
if [ "${p:0:1}" != "/" ]; then
- if the first character$p
is equal/
(i.e. the symlink is absolute) then ... -
p="$(echo "$(pwd)/$p" | sed -e "s|/\./|/|g" -e ":a" -e "s|/[^/]*/\.\./|/|" -e "t a")";
- convert path to absolute:-
$(pwd)
- the current directory (where the match file is, because we are using-execdir
) -
/$p
- add forward slash and target of symbolic link to working directory path -
echo "$(pwd)/$p" |
- translate the above command to the next command -
sed ...
- allow everything..
, see here -
p="$(...)"
and cast the result back to$p
.
-
-
fi;
- endif
-
if [ "${p:0:'${#oldpath}'}" == "'"$oldpath"'" ];
- if it$p
starts with$oldpath
-
${p:0:'${#oldpath}'}
- substring$p
starting from position0
with length$oldpath
:-
${#oldpath}
- variable length$oldpath
-
'...'
- required because we are inside the'
-quoted line
-
-
-
then
- then ... -
ln -sf
- link symbolically and override existing file with arguments:-
"'"$newpath"'${p:'${#oldpath}'}"
- replace the$oldpath
part$p
with$newpath
(actually remove as many characters from$p
as$oldpath
long and add to it$newpath
):-
"
- run the line -
'
- complete the'
-string argument beforebash -c
-
"
- add to it a line"
(in which the variable is expanded) containing: -
$newpath
- value$newpath
-
"
- complete the"
-string argument beforebash -c
-
'
- add to it a line'
containing: -
${p:
- substringp
starting with: -
'
- complete the argumentbash -c
-
${#oldpath}
- add length to it$oldpath
-
'
- add another'
jet to it -
}
- end of substring -
"
- end of line
-
-
"{}"
- a link file whose path remains the same
-
-
fi;
- endif
-
-
-
\;
- separator for-execdir
source to share
I have the following problem. I found a very nice icon theme called "Suru ++"
Suru ++ 20 [Officially bug-free and 11 DE-compliant]
But this theme icon is made in svg in the whole way. Now I want to use it in Fluxbox and generate menu items, but this is only possible with png icon image. I load the wizard inside, all the icons are in svg format, and yes, I can convert from command line svg to png everything is ok to generate for each Folder size like 16x16, 24x24 pixels. But these folders have symlinks to SVG files:
If it is possible to change the svg extension to png within all Symbolix links, the icon theme will work fine in the Fluxbox menus and file managers. I made an entry about this icon theme using in Xubuntu but in Spanish
God bless
source to share