Add readlink post.
authorW. Trevor King <wking@drexel.edu>
Thu, 8 Dec 2011 11:47:11 +0000 (06:47 -0500)
committerW. Trevor King <wking@drexel.edu>
Thu, 8 Dec 2011 11:47:11 +0000 (06:47 -0500)
posts/readlink.mdwn [new file with mode: 0644]

diff --git a/posts/readlink.mdwn b/posts/readlink.mdwn
new file mode 100644 (file)
index 0000000..d960b19
--- /dev/null
@@ -0,0 +1,26 @@
+I moved a directory containing a bunch of symbolic links today, and
+the links all broke.  I fixed the links with the following Bash
+oneliner (split across lines in this post for clarity):
+
+    $ for FILE in *; do
+          if [ -L "${FILE}" ]; then
+              TARGET=$(readlink "${FILE}");
+              rm -f "${FILE}";
+              ln -s "${TARGET/old-dir\//}" "${FILE}";
+          fi;
+      done
+
+`readlink` prints the value of a symbolic link and
+`${TARGET/old-dir\//}` creates the new link target by removing
+`old-dir/` from the original target.
+
+You may also find it useful to rename simlinks so that the link
+filename matches the target filename:
+
+    $ for x in *; do y=$(basename $(readlink -f "${x}")); mv "${x}" "${y}"; done
+
+This assumes that everything globbed into `x` is a link.  If not, you
+can add an `if [ -L "${x}" ]` block like I did in my
+target-translation example.
+
+[[!tag tags/linux]]