From f7fd45eb25b9addbcfd80c70a981be5d725554cf Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Thu, 8 Dec 2011 06:47:11 -0500 Subject: [PATCH] Add readlink post. --- posts/readlink.mdwn | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 posts/readlink.mdwn 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]] -- 2.26.2