How to avoid error with ln command?

mkdir logs
mkdir: Failed to make directory "logs"; File exists

To avoid this error i use the -p argument so it creates a folder only if it is does not exists like you see below.

mkdir -p logs 

In the similar manner i wish to avoid this error with ln command

ln -s /tmp/myfolder var
ln: cannot create var: File exists

You can test if it is a symlink.

Check out man test on your operating system.

test -h /path/mylink && echo "link" || echo "not link"

Regards.

Sure this is a reasonable request? Which of the links should persist - the old one or the new one? Are you aware that overwriting the old value may destroy valuable information?

Read man ln for info on the -b , --backup , and -f options.

I thought that the -p flag of mkdir was to make up any required paths, so if you issue mkdir /a/b/c/d and only /a/b exist, /a/b/c will be created for you.

My testing seems to suggest your way will still fail:-

$ touch /tmp/my-test
$ mkdir -p /tmp/my-test
mkdir: cannot create directory `/tmp/my-test': File exists
$

Am I missing something? It is always best to test before you try to create/replace something.

Robin

They are not equivalent. mkdir always does the same thing - create an empty folder - while the entries ln creates depend on the source.