From: W. Trevor King Date: Thu, 8 Dec 2011 11:47:11 +0000 (-0500) Subject: Add readlink post. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=f7fd45eb25b9addbcfd80c70a981be5d725554cf;p=blog.git Add readlink post. --- diff --git a/posts/readlink.mdwn b/posts/readlink.mdwn new file mode 100644 index 0000000..d960b19 --- /dev/null +++ b/posts/readlink.mdwn @@ -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]]