Last active
August 29, 2015 14:00
-
-
Save njvack/11155311 to your computer and use it in GitHub Desktop.
Make symlinks from a directory's file structure out on the filesystem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
in_path=$1 | |
if [[ "$in_path" == "" ]] || [[ "$in_path" == "-h" ]]; then | |
cat <<USAGE | |
Usage: link_tree PATH [--dry-run] | |
Finds every symbolic link in PATH/subdirectory** and creates links to them in | |
/subdirectory** | |
For example, if PATH is /home/foo/links, and that contains files (or links): | |
/home/foo/links/usr/local/bin/goat | |
/home/foo/links/usr/local/etc/attacks | |
linkify will create: | |
/usr/local/bin/goat -> /home/foo/links/usr/local/bin/goat | |
/usr/local/etc/attacks -> /home/foo/links/usr/local/etc/attacks | |
Options: | |
--dry-run Don't actually make links. | |
USAGE | |
exit 1 | |
fi | |
dry_run=$2 | |
if [[ "$dry_run" != "" ]] && [[ "$dry_run" != "--dry-run" ]]; then | |
echo "Unknown option $2" | |
exit 2 | |
fi | |
pushd $in_path > /dev/null | |
for symlink in $(find "$PWD" -type l -or -type f); do | |
target=$(echo "$symlink" | sed "s|^$PWD||") | |
cmd="ln -s \"$symlink\" \"$target\"" | |
echo $cmd | |
if [[ "$dry_run" == "" ]]; then | |
# We already checked to make sure it was --dry-run | |
eval $cmd | |
fi | |
done | |
popd > /dev/null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment