Search code examples
bashsymlink

replace all symlinks with original


I have the following directory structure

/symdir
  sym1 -> ../dir1
  sym2 -> ../dir2
  hello.txt

And then

/dir1
  some
  files
  here
/dir2
  more
  files

I would like to replace the symlinks in symdir (sym1, sym2) with the originals. I.e.

some_awesome_bash_func symdir symdir_output

Would create

/symdir_output
  /dir1
    some
    files
    here
  /dir2
    more
    files
  hello.txt

How would I accomplish this?


Solution

  • Probably not the best way, but it works:

    #!/usr/bin/bash
    
    for link in $(find /symdir -type l)
    do
      loc="$(dirname "$link")"
      dir="$(readlink "$link")"
      mv "$dir" "$loc"
      rm "$link"
    done