EN VI

Linux - Why is the same ln command running differently?

2024-03-12 16:30:06
Linux - Why is the same ln command running differently?

Consider a folder bar is a target to be created symbolic link to:

mkdir bar

Now create a symbolic link to bar

ln -s bar foo
# It will be now: foo -> bar

However running the same ln command above again, an undesired symlink appears:

ln -s bar foo
# Instead of showing error of existing link
# the command creates a faulty link 'bar/bar'

Currently in order to avoid that faulty link I have to check first

if [[ ! -d foo ]]; then
    ln -s bar foo
fi

That check is a solution, however, why is ln command creating such faulty link? Any options to ln command to tell the symlink is existing?

Solution:

Use the -T option

ln -sT bar foo

which means "treat LINK_NAME as a normal file always" instead of a target destination directory.

Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login