Results 1 to 5 of 5

Thread: Breaking symlinks

  1. #1
    Join Date
    Nov 2007
    Beans
    19
    Distro
    Ubuntu 16.04 Xenial Xerus

    Question Breaking symlinks

    Is there a built-in command to break a symbolic (or even hard) link? i don't mean deleting the link (or the linked file/directory), i mean replacing the link with the linked content.

    For example, let's say i have a file FILE_A that has the text "Hello", and i create a link with ln -s FILE_A FILE_B.

    Code:
    dstar@space:~$ ln -s FILE_A FILE_B
    dstar@space:~$ cat FILE_A
    Hello
    dstar@space:~$ cat FILE_B
    Hello
    dstar@space:~$ file FILE_A FILE_B
    FILE_A: ASCII text
    FILE_B: symbolic link to `FILE_A'
    Now I want to break the link, leaving FILE_B, except now it is a copy of FILE_A:

    Code:
    dstar@space:~$ ??? FILE_B
    dstar@space:~$ cat FILE_A
    Hello
    dstar@space:~$ cat FILE_B
    Hello
    dstar@space:~$ file FILE_A FILE_B
    FILE_A: ASCII text
    FILE_B: ASCII text
    dstar@space:~$
    Is there a simple command that does what ??? does? A single command that does "cp FILE_B xxx && rm FILE_B && mv xxx FILE_B"?

  2. #2
    Join Date
    Mar 2006
    Location
    Williams Lake
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: Breaking symlinks

    You could try:

    Code:
    ln -i
    From man ln:

    -i, --interactive
    prompt whether to remove destinations

  3. #3
    Join Date
    Dec 2007
    Location
    Idaho
    Beans
    4,976
    Distro
    Ubuntu 20.04 Focal Fossa

    Re: Breaking symlinks

    Well hard links will act like that by default.

    For example
    Code:
    jeremy@psion:~/test$ ln -s fileA fileB
    jeremy@psion:~/test$ ln fileA fileC
    jeremy@psion:~/test$ ls -l
    total 8
    -rw-r--r-- 2 jeremy jeremy 6 2009-07-17 11:16 fileA
    lrwxrwxrwx 1 jeremy jeremy 5 2009-07-17 11:16 fileB -> fileA
    -rw-r--r-- 2 jeremy jeremy 6 2009-07-17 11:16 fileC
    jeremy@psion:~/test$ rm fileA
    jeremy@psion:~/test$ ls -l
    total 4
    lrwxrwxrwx 1 jeremy jeremy 5 2009-07-17 11:16 fileB -> fileA
    -rw-r--r-- 1 jeremy jeremy 6 2009-07-17 11:16 fileC
    jeremy@psion:~/test$ cat fileC      # the hard link still works
    hello
    jeremy@psion:~/test$ cat fileB      # while the symlink doesn't
    cat: fileB: No such file or directory
    "You can't expect to hold supreme executive power just because some watery tart lobbed a sword at you"

    "Don't let your mind wander -- it's too little to be let out alone."

  4. #4
    Join Date
    Nov 2007
    Beans
    19
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Breaking symlinks

    No, i guess i didn't explain it clearly enough. i don't want to remove the link target. i want to replace the link with a copy of the target.

    If fileA has the contents "Hello", and then i created links like this:

    Code:
    dstar@space:~$ ln -s fileA fileB
    dstar@space:~$ ln fileA fileC
    dstar@space:~$ ls -l
    ... fileA
    ... fileB -> fileA
    ... fileC
    dstar@space:~$
    Then i did something:

    Code:
    dstar@space:~$ something fileB
    dstar@space:~$ something fileC
    dstar@space:~$ ls -l
    ... fileA
    ... fileB
    ... fileC
    dstar@space:~$
    See that fileB is no longer a link. And then if i edit fileA to say "Goodbye", neither fileB nor fileC are affected, because they're not links anymore:

    Code:
    dstar@space:~$ cat fileA
    Goodbye
    dstar@space:~$ cat fileB
    Hello
    dstar@space:~$ cat fileC
    Hello
    dstar@space:~$
    i want to know what that "something" command would be. Whatever it is, "something <file>" is functionally the same as "cp <file> xxx && rm <file> && mv xxx <file>". Not "rm <whatever file links to>".

  5. #5
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    5,263
    Distro
    Ubuntu

    Re: Breaking symlinks

    To the best of my knowledge, there is no such command. Whether the link is hard or soft, the link needs to be severed and the file copied.

    Therefore, the only option is
    Code:
    rm fileB && cp fileA fileB
    You can, of course, create your own command to do this.
    Always make regular backups of your data (and test them).
    Visit Full Circle Magazine for beginners and seasoned Linux enthusiasts.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •