according to ln manual page:
man ln
-f, --force
remove existing destination files
so as I understand if I want to re-create new link to some destination directory/file
I can simply do
ln -s -f some_directory new_link
but this is not what I got from my linux/solaris machines
for example
[u@h w]# mkdir dir1
[u@h w]# ln -s dir1 link
[u@h w]# ls -l
drwxr-xr-x 2 root root 4096 Mar 16 20:26 dir1
lrwxrwxrwx 1 root root 4 Mar 16 20:27 link -> dir1
[u@h w]# ln -s -f dir1 new_link
now I accepted to see that new_link is now pointed to dir1 while "link" isn’t exists but link still pointed to dir1 also?
how to recreate new link and on the same time remove the old link in one command ?
( I not want to use rm command )
[u@h w]# ls -l
drwxr-xr-x 2 root root 4096 Mar 16 20:26 dir1
lrwxrwxrwx 1 root root 4 Mar 16 20:27 link -> dir1
lrwxrwxrwx 1 root root 4 Mar 16 20:28 new_link -> dir1
Other different example
#
# mkdir dir
#
# ln -s dir link
#
# mv dir dir_new
# ls -l
total 32
drwxr-xr-x 2 root root 117 Mar 16 21:44 dir_new
lrwxrwxrwx 1 root root 3 Mar 16 21:44 link -> dir
# ln -s -f dir_new link
ln: cannot create link: File exists
is it possible to update the current link to point on new directory/file
( without to get File exists ? ) ,
or force to create the link again on different directory ?
The
-f
option allows you to overwrite the target file, i.e. the file with the name you're creating the link with, with a new link. It doesn't check for other links in the same directory.Example:
If you just want to move the name of the link, so that instead of being called
link1
it should be callednewlink
, just move it:If you're working with directories instead of files, you need to add the option
-n
as well as-f
:mv
is atomic, and will do what you want, as far as I can tell: