replace all symlinks with original
Asked Answered
P

9

44

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?

Philippa answered 23/8, 2011 at 20:55 Comment(1)
script it with cp, mkdir and readlinkLattermost
D
16

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
Dickinson answered 23/8, 2011 at 21:13 Comment(3)
readlink "$link" gives the next link... readlink -e "$link" gives the final target of a chain of links.Kickstand
Be aware that you must not have any file name with spaces; otherwise the mv command breakesThissa
This doesn't "work" - it will simply move the linked files to the directory in which the link was found. What if the linked files are named differently to their links?Zonked
E
73

My very personal trick for files (not directories):

sed -i '' **/*

Note that I'm using ** which uses the bash globstar option, you may have to enable it beforehand:

shopt -s globstar

How it works

I trick sed to do the job, by using an implementation detail of the sed inplace mode.

sed is a tool to edit streams of text. The -i option of sed means inplace, the empty string '' is the instruction set: so there's no instruction, sed will do nothing. **/* is a bash globstar pattern meaning "all files and all folders, at all depth, from here".

The algorithm sed uses to edit a file inplace is:

  • Create a temporary file as the output file,
  • for each line in the input file:
    • apply the transformation, write to the output file.
  • Move the output file over the input file.

As I'm asking no transformations (the empty string), the algorithm can be simplified as:

  • Create a temporary file,
  • copy the content of the original file to the temporary file
  • move the temporary file over the original file.

The temporary file is a real file, sed completly ignores that the input file was a symlink, it just reads it. So at the last step, when sed moves the temporary file over the real file, it "overwrite" the symlink with a real file, that's what we wanted.

This also explains why it won't work to transform a "symlink to a directory" to a real directory: sed works on file contents.

Ender answered 1/10, 2012 at 12:45 Comment(12)
What does it do ? How could sed possibly copy files ?Chattanooga
Very interesting. I'm a bit surprised this works. (cc @LeVieuxGildas - it does work)Aldosterone
@nikana-reklawyks sed -i FILE work like 'TMP=$(mktemp); cat $FILE | sed ... > $TMP; mv $TMP $FILE;' so without the --follow-symlinks option it will effectively do the trick :)Ender
@ZacharyVance If you have a better / cleaner / whatever solution, feel free to post it ;-)Ender
sed -i '' **/* by itself did not work but specifying the files more explicitly it worked: sed -i '' *.txtMedamedal
@GeorgePligor You're probably missing the globstar option, I specify this in my answer.Ender
ELI5: **/* expands all files in directory to the command sed -i '' changes nothing in the content of the file This takes advantage of the fact sed uses temp files and replace the origin, looks like it works well for directories too. source: reddit.com/r/programming/comments/9ktb2o/…Brought
but sadly it has a problem with directories that are symbolic links... sed: couldn't edit <filename>: not a regular fileEmitter
@Emitter you're right, never had the issue but worth mentionning.Ender
This is a cool hack, but such an answer should really, really contain an explanation for how/why/when it works.Brave
@Brave good idea, is it better? Anything I can enhance?Ender
Note that you need GNU sed for this to work. This does not work with FreeBSD sed such as the one shipped with macOS.Khudari
N
34

You can do this easily with rsync:

rsync symdir/ symdir_output/ -a --copy-links -v

(-a means preserve basically every detail about the files, --copy-links overrides -a to turn symlinks into the real files/directories, and -v is for verbose)

Edit:

Sorry, my solution doesn't do exactly what you asked for. It will preserve the symlink's names instead of using the destination names. symdir_output would have sym1 and sym2 instead of dir1 and dir2 (though sym1 and sym2 would be a real copy of dir1 and dir2). Hope it still works for you.

Newhouse answered 23/8, 2011 at 21:7 Comment(1)
I had a similar issue and this was the exact solution I needed. Thanks!Bulb
D
17

a related answer, this solution keeps the file at it's original place and creates a copy in place of the symlink

#!/bin/bash

for f in $(find . -maxdepth 1 -type l)
do
    cp --remove-destination $(readlink -e $f) $f
done
Deberadeberry answered 26/7, 2013 at 13:18 Comment(3)
This solution (replacing 'cp' with 'mv' and removing '--remove-destination') was the only one that worked exactly as I expected. It replaces the symlinks with the original files and deletes the old location.Indiscreet
You'll want to double-quote those parameter expansions; also use readlink -f to if the directory is anything other than ./: for f in $(find . -maxdepth 1 -type l) do cp --remove-destination "$(readlink -f "$f")" "$f" done. And be aware that splitting of the loop arguments is suspect...Spradlin
I'd recommend using readlink -e rather than plain readlink. Plain readlink will return the path the symlink points to, even if there's no file there. Better to fail on the readlink step than the cp step.Gilletta
D
16

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
Dickinson answered 23/8, 2011 at 21:13 Comment(3)
readlink "$link" gives the next link... readlink -e "$link" gives the final target of a chain of links.Kickstand
Be aware that you must not have any file name with spaces; otherwise the mv command breakesThissa
This doesn't "work" - it will simply move the linked files to the directory in which the link was found. What if the linked files are named differently to their links?Zonked
S
7

tl;dr: much more general, much more reliable answer:

find -type l -exec sh -c 'PREV=$(realpath -- "$1") && rm -- "$1" && cp -ar -- "$PREV" "$1"' resolver {} \;

The "rsync-to-other-destination" approach is strictly superior, and usually leads to better design.

The answer by @PinkFloyd doesn't quite work with unusual filenames, "buried" symlinks, or symlinked directories. I came here because I wanted to resolve directory-symlinks, so I expect others to find this question for this reason, too. Also, my version of cp (GNU coreutils 8.25) doesn't properly handle --remove-destinationfor @PinkFloyd's answer to work with directories. So this answer uses manual rm.

Also note:

  • absence of -rf. That's because a symlink is not a directory, and should not need -r. And unless you have symlinks with restricted permissions (why would you ever want that?!), you don't need -f either.
  • realpath is perfectly fine in this context, because it allows us to find out the actual location in the current system, in the current context, and nothing else matters. This path won't be written to disk, so this is not an error.
  • the resolver string is for sh. See man sh.
  • the double-dashes everywhere are necessary, in case some symlink is called --version or similar.
  • due to the preorder-guarantee of find ("parent directory is listed at somewhere before its content"), this would first replace the parent directory, and then any symlinks within the symlinked directory. So it would work perfectly fine with "stacked" symlinks.
Softball answered 5/10, 2016 at 18:56 Comment(1)
@Madeo I guess you're using some weird implementation of "find" that doesn't implement "-type"; perhaps you're using MacOS? Use one of the plenty other answers, for example the rsync one as I recommend. Or install GNU find, because you will run into similar problems again. "macos homebrew gnu find" seems to be a good search string.Softball
L
3

Extending Julien's suggestion. This command recursively replaces all symbolic links in the current directory with it's target.

find . -type l -exec sed -i '' {} \;
  • find .: Search in the current directory, recursively
  • -type l: Finds symlinks only
  • -exec sed -i '' {} \;: Run sed -i '' PATH for each symlink. Please see Julien's answer for an excellent explanation of why we use sed here.
Lowborn answered 3/8, 2021 at 17:52 Comment(0)
D
0

I did it this way:

ls -la | awk '/-\>/{system("rm "$10); system("cp "$12" .")}'

How it works:

ls -la outputs something like this:

lrwxr-xr-x  1 username  groupname   44 10 Oct 12:17 Queue.swift -> ../../../Platform/DataStructures/Queue.swift

Column 10 is Queue.swift which is the name of the local file.
Column 12 is ../../../Platform/DataStructures/Queue.swift which is the name of the link target

The first part of the awk command is '/-\>/' which means "match lines which contain -> using a regex

The next part of the awk command is two calls to system

First system("rm "$10) which expands to system("rm Queue.swift").
This will cause the original file (the symlink) to get deleted

Second is system("cp "$12" .") which expands to system("cp ../../../Platform/DataStructures/Queue.swift .")

Putting it all together, what happens is for each file (which is a symlink), first we delete the symlink, then we copy the target file in it's place.


Although it's not part of the original question, I was using this in conjunction with git. If you happen to be doing that too, you can run git status . afterwards and you should see a bunch of type changes (and nothing else), like this:

typechange: Queue.swift
Deragon answered 10/10, 2017 at 0:7 Comment(1)
-1: That's a very fragile solution using several bad practices. Never ever parse the output ls, it fails for unusual filenames (in your case even spaces will break it). Use find for that. And don't trust ls -l columns: the date/time format for example depends on locale and is user-configurable, so it might contain fewer or more spaces. Use readlink to get a link's target. Also, don't use awk to issue system commands, use command substitution and/or while read loops (with < <(...), $(...), etc)Mediatory
T
0

here is a slightly more general solution, based on @Daniel Haley

it also preserves the symlinks for reference and asks the user to select a directory to edit.

ls
read -p 'Which directory do you want to update?: ' linkdir

pushd $linkdir

for linkname in $(find ./ -type l)
do
  orig=$(readlink $linkname)
  mv $linkname ${linkname}.linkbak
  cp $orig $linkname
done

popd
Trout answered 1/9, 2018 at 4:45 Comment(0)
K
0

This works for directories as well. Replace dir with your desired local directory:

mv ./dir ./dir.sym; cp -r $(readlink ./dir.sym) ./dir; rm -r ./dir.sym

This moves the symbolic directory dir to dir.sym. It then reads the source path of the just moved dir.sym, which still points to the same source as dir, and uses it to recursively copy all directory content to the new local dir. Finally, it removes the working copy dir.sym.

Kos answered 19/11, 2023 at 10:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.