How to create link to directory in makefile in Linux?

1.9k views Asked by At

I have makefile containing code below.

ddd :
    @mkdir -p /full/path_new/to/new_dir
    @ln -fs /full/path_old/to/old_dir/private /full/path_new/to/new_dir/private

Linux ln command creates link in both directory taget and parent. It means I have:

/full/path_new/to/new_dir:
private -> /full/path_old/to/old_dir/private

but also old one gets link

/full/path_old/to/old_dir/private
private -> /full/path_old/to/old_dir/private

It cause I have something like

/full/path_old/to/old_dir/private/private/private/private (...) endless

How should I use ln command to have link in new_dir only?

2

There are 2 answers

0
MadScientist On

You need to remove the existing link before you create it:

ddd :
        @mkdir -p /full/path_new/to/new_dir
        @rm -f /full/path_new/to/new_dir/private
        @ln -fs /full/path_old/to/old_dir/private /full/path_new/to/new_dir/private
3
tripleee On

Assuming a clean slate, the proper way to do this would be to create a target which knows which dependency it is creating, and avoid creating it if it alread exists.

ddd: /full/path_new/to/new_dir/private
/full/path_new/to/new_dir/private:
    mkdir -p /full/path_new/to/new_dir
    ln -s /full/path/to/old_dir/private /full/path_new/to/new_dir