3

For the icon themes I'm developing, I have different files with a different number of symlinks placed in different folders. Here is an example of one of them:

FlatWoken/FlatWoken/scalable/apps$ find -L ../ -samefile bluetooth-active.svg 
../stock/stock_bluetooth.svg
../status/bluetooth-active.svg
../status/blueman-active.svg
../apps/bluetooth-active.svg
../apps/blueradio-48.svg
../apps/bluetooth.svg
../apps/bluedun.svg
../apps/bluetoothradio.svg
../apps/preferences-system-bluetooth.svg
../apps/blueman.svg

What I would like to do is to find out a terminal command that is able to move all of them in a new folder (let's say, FlatWoken/FlatWoken/24x24 ), without loosing the symlinks (i.e. FlatWoken/FlatWoken/24x24/apps/bluedun.svg should symlink to FlatWoken/FlatWoken/24x24/apps/bluetooth-active.svg ). Is it possible to do so?

For more clarification, if I have this structure:

alecive@calliope:~/Scrivania/temp$ ls *
dest:
a

orig:
a  b  c
alecive@calliope:~/Scrivania/temp$ ls -l orig/*
orig/a:
totale 0
lrwxrwxrwx 1 alecive alecive 8 apr  9 10:22 link_a -> truefile
-rw-rw-r-- 1 alecive alecive 0 apr  9 10:22 truefile

orig/b:
totale 0
lrwxrwxrwx 1 alecive alecive 13 apr  9 10:23 link_b -> ../a/truefile

orig/c:
totale 0
lrwxrwxrwx 1 alecive alecive 13 apr  9 10:23 link_c -> ../a/truefile

alecive@calliope:~/Scrivania/temp$ ls -l dest/*
dest/a:
totale 0
-rw-rw-r-- 1 alecive alecive 0 apr  9 10:24 truefile

dest/b:
totale 0

dest/c:
totale 0

I would like to fill the dest folder with the symlinks that point to dest/a/truefile (and not orig/a/truefile), without using the absolute path but only the relative path.

Thanks in advance for any help!

1 Answer 1

1

Advanced material. Use at your risk! You will need to modify it for sure. Do not use if you do not understand it.

If you can live with absolute symlinks, you can do that. Suppose you start from this:

[:~/tmp] % ls -l a b c orig 

a:
total 0
lrwxrwxrwx 1 rmano rmano 16 Apr  1 16:53 linka -> ../orig/truefile

b:
total 0
lrwxrwxrwx 1 rmano rmano 16 Apr  1 16:53 linkb -> ../orig/truefile

c:
total 0
lrwxrwxrwx 1 rmano rmano 16 Apr  1 16:53 linkc -> ../orig/truefile

orig:
total 0
-rw-rw-r-- 1 rmano rmano 0 Apr  1 16:52 truefile

and you want to move the links under ./dest...

Make the destination dir: mkdir dest

then you can use this script (save it as script.sh, remove the echo in the for loop after checking is doing the correct thing):

#! /bin/bash
#
# $1 is the "true" file name, $2 the dest directory
# note both must exist, no check done! Let as an exrecise!
#
# put absolute path in $absf
absf="$(readlink -f "$1")"
#
# loop over all of the links we found under ".". Avoid the true file. 
# Print instructions; remove the echo if you want to execute them 
# (after checking, rm is not reversible) 
#
for i in $(find -L . -samefile $1 | grep -v $1); do
        echo  ln -s $absf "$2"/"$(basename "$i")"
        echo  rm "$i"
done

...and run it as ./script.sh orig/truefile dest/. It will execute

ln -s /home/rmano/tmp/orig/truefile dest/linkb
rm ./b/linkb
ln -s /home/rmano/tmp/orig/truefile dest/linka
rm ./a/linka
ln -s /home/rmano/tmp/orig/truefile dest/linkc
rm ./c/linkc

and you'll have:

[:~/tmp/dest] % cd dest; ls -l 
total 0
lrwxrwxrwx 1 rmano rmano 30 Apr  1 17:19 linka -> /home/rmano/tmp/orig/truefile
lrwxrwxrwx 1 rmano rmano 30 Apr  1 17:18 linkb -> /home/rmano/tmp/orig/truefile
lrwxrwxrwx 1 rmano rmano 30 Apr  1 17:19 linkc -> /home/rmano/tmp/orig/truefile

If you need relative symlink it will be more complex. You can check this link to start coding...

1
  • I've edited the original post :)
    – alecive
    Apr 9, 2014 at 8:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .