Search code examples
findpipeexecln

Find and soft link without the parent path


So I have a find command as below which finds the libclntsh.so.* files in a directory instantclient.

find instantclient -type f -name "*libclntsh\.so\.[0-9]*\.[0-9]*"

This results in for e.g.,

instantclient/libclntsh.so.11.1

How do I now ln within instantclient directory, ln -s libclntsh.so.11.1 libclntsh.so all with a find command in combination with exec

I should mention here that I DO NOT want to cd into instantclient. And this is for Alpine Linux.


Solution

  • Use the -execdir option. As per manual:

    -execdir command {} ;

    Like -exec, but the specified command is run from the subdirectory containing the matched file, which is not normally the directory in which you started find. This a much more secure method for invoking commands, as it avoids race conditions during resolution of the paths to the matched files.

    So your command will be:

     find instantclient -type f -name "*libclntsh\.so\.[0-9]*\.[0-9]*" -execdir ln -s {} libclntsh.so \;
    

    EDIT:

    Another solution

     find instantclient -type f -name "*libclntsh\.so\.[0-9]*\.[0-9]*" | xargs -I {} sh -c 'ln -s $(basename {}) instantclient/libclntsh.so'