OSDN Git Service

Add the lndir script
authorChih-Wei Huang <cwhuang@linux.org.tw>
Sun, 26 Jul 2015 11:29:53 +0000 (19:29 +0800)
committerChih-Wei Huang <cwhuang@linux.org.tw>
Sun, 26 Jul 2015 11:29:53 +0000 (19:29 +0800)
The script can make hard links of files in a directory
to another one.

initrd/bin/lndir [new file with mode: 0755]

diff --git a/initrd/bin/lndir b/initrd/bin/lndir
new file mode 100755 (executable)
index 0000000..7511132
--- /dev/null
@@ -0,0 +1,37 @@
+#!/bin/sh
+
+[ "$1" = "-v" ] && verbose=-v && shift
+
+src=$1
+dst=${2:-.}
+
+usage()
+{
+       echo "Usage: $(basename $0) SRC_DIR [DST_DIR]"
+       echo
+       exit 1
+}
+
+linkdir()
+{
+       local odir="$PWD"
+       [ -d "$2" ] || mkdir -p "$2"
+       cd "$2"
+       local d="$PWD"
+       cd "$odir" && cd "$1"
+       for f in * .*; do
+               [ "$f" = "." -o "$f" = ".." ] && continue
+               if [ -d "$f" ]; then
+                       linkdir "$f" "$d/$f"
+               elif [ -e "$f" ]; then
+                       ln $verbose "$f" "$d"
+               fi
+       done
+       cd "$odir"
+}
+
+[ -z "$src" ] && usage
+
+echo "$src" | grep -q /$ || dst=$dst/$(basename "$src")
+
+linkdir "$src" "$dst"