English 中文(简体)
Is there a way to edit a symlink without deleting it first? [duplicate]
原标题:
  • 时间:2009-11-13 05:22:00
  •  标签:
  • unix
  • symlink

So I created a symlink:

ln -s /location/to/link linkname

Now I want to change the location that the symlink links to. How do I do that? is there a way to do it without deleting it first?

最佳回答

You could create the new link with a different name, then move it to replace the old link.

ln -s /location/to/link linkname

Later

ln -s /location/to/link2 newlink
mv newlink linkname

If newlink and linkname are on the same physical device the mv should be atomic.

问题回答

Try ln -sf new_destination linkname.

Just change the symlink target:

# ln -sfT /path/to/new/target linkname

This is an instant, atomic change.

If the symlink targets are directories, you need to add the -T flag to the mv command, otherwise it moves the new symlink in to the target directory of the old symlink.

Example of atomically switching a website to a new version:

Original setup - website is stored in www1 directory, vhost pointing at www symlink:

ln -s www1 www

Browse to website, see old version.

Put new website files in new www2 directory.

Set up new symlink to new website:

ln -s www_new www2

Move www symlink to directory of new website:

mv -T www_new www

Browse to website, see new version immediately.

On OSX, the man page for ln says you can do it like this

ln -shf /location/to/link link name

From the man page:

The options are as follows:
 -F    If the target file already exists and is a directory, then remove it so that the link may occur.  The -F
       option should be used with either -f or -i options.  If none is specified, -f is implied.  The -F option is
       a no-op unless -s option is specified.

 -h    If the target_file or target_dir is a symbolic link, do not follow it.  This is most useful with the -f
       option, to replace a symlink which may point to a directory.

 -f    If the target file already exists, then unlink it so that the link may occur.  (The -f option overrides any
       previous -i options.)

 -i    Cause ln to write a prompt to standard error if the target file exists.  If the response from the standard
       input begins with the character `y  or `Y , then unlink the target file so that the link may occur.  Other-
       wise, do not attempt the link.  (The -i option overrides any previous -f options.)

 -n    Same as -h, for compatibility with other ln implementations.

 -s    Create a symbolic link.

 -v    Cause ln to be verbose, showing files as they are processed.

For directories, you want to do: ln -sfT /location/to/new/target old_linkname

No. The symlink system call will return EEXIST if newpath already exists. You can only link from a new node in the filesystem. What s the requirement here? If you re worried about a race due to the non-atomicity of the unlink/symlink calls, then you might want to rethink the architecture a little to provide synchronization elsewhere. There have been some scary security bugs introduced by this kind of thing.

As others have mentioned, you basically have to delete the symlink first, either manually or by passing the -f flag to the ln utility.

Years ago, I had to make small edits to symlinks pretty frequently, so I wrote a simple readline-based utility (edln) to make this less annoying. In case anyone else finds it useful, I ve put it online at https://github.com/jjlin/edln/.

edln will display the original symlink target; you can then use the arrow keys, or standard readline keystrokes (M-b, M-f, C-d, etc.) to move around and edit the target.

Chain the commands like this:

rm currentlink && ln -s /path/to/link currentlink

The first command removes the existing one and the 2nd immediately creates it again.

Just googled, found no good answer and had to solve myself:

ln -f -s -T `readlink SomeLibrary | sed  s/version.old/version.new/ ` SomeLibrary

Editing by definition means not recreating from scratch but changing partly. Any answer requiring to memorize a path, maybe long or with weird symbols, is definitely bad.





相关问题
Generate assembler code from C file in linux

I would like to know how to generate assembler code from a C program using Unix. I tried the gcc: gcc -c file.c I also used firstly cpp and then try as but I m getting errors. I m trying to build an ...

Function to create the array by reading the file

I am creating scripts which will store the contents of pipe delimited file. Each column is stored in a separate array. I then read the information from the arrays and process it. There are 20 pipe ...

Compare characters at the end of the string C++

This program supposed to find command line arguments entered on Unix which ends with “.exe”. For some reason it doesn t work. Here is the code: int main( int argc, char* argv[] ) { for ( int ...

Batch Job Dependencies Using Open Source/Free Software

I run a large data warehouse plant where we have a lot of nightly jobs running concerruently however many have dependencies on a extract or data load process before they start. Currently we use an ...

Writing application for both Unix and Windows

I ll write a program for Interactive UNIX (http://en.wikipedia.org/wiki/INTERACTIVE_UNIX). But in a year it will be ported to Windows. I ll write it in ANSI C and/or SH-script. When it runs on Windows ...

Development Environment in Windows [closed]

What are your recommendations for setting up a development environment in Windows, especially when not using an IDE. I am attempting to familiarize myself with Windows, and I feel a bit lost. What ...

热门标签