Search code examples
bashshellfindln

Create symlink with existing directory structure in new location


I'm using

find source -name 'Archive.folder'

which outputs:

source/1/Archive.folder
source/2/Archive.folder
...

to find all folders named Archive.folder in the source folder. Now I want to link them to a new location /var/CommuniGate/Accounts. How can I do that? I found the following solution but it doesn't work, it just links the source folder, not the found folders.

find source -name 'Archive.folder' | xargs -0 ln -s -t /var/CommuniGate/Accounts

The symlinks should be created like this:

/var/CommuniGate/Accounts/source/1/Archive.folder
/var/CommuniGate/Accounts/source/1/Archive.folder
...

Solution

  • Something like this, maybe:

    find source -name Archive.folder -print0 |
    xargs -0 -iDIR sh -c 'mkdir -p /var/CommuniGate/Accounts/$(dirname DIR); ln -s -t /var/CommuniGate/Accounts/$(dirname DIR) $PWD/DIR' 
    

    That works for my simple test case.