When using a shallow tree structure to manage project directories, it is sometimes necessary to link directories in different branches of the tree. That is, the directories are not nested one in the other. It is a bit of a hassle to create a link in one directory, then navigate to the other directory and create the reciprocal link. I often have trouble remembering the order of the arguments for the link command. I get the order wrong half of the time. This friction inhibits me from creating the mutuall links The bash function below solves these problems.
linkDirs() {
# Check if exactly two arguments are provided
if [ "$#" -ne 2 ]; then
echo "Usage: link_dirs /path/to/dir1 /path/to/dir2"
return 1
fi
# Get absolute paths to avoid broken links
local DIR1=$(realpath "$1")
local DIR2=$(realpath "$2")
# Verify both paths are actually directories
if [[ ! -d "$DIR1" || ! -d "$DIR2" ]]; then
echo "Error: Both arguments must be valid directories."
return 1
fi
# Get the folder names to use as link names
local NAME1=$(basename "$DIR1")
local NAME2=$(basename "$DIR2")
# Create reciprocal links
# Link to DIR2 inside DIR1
ln -s "$DIR2" "$DIR1/$NAME2"
# Link to DIR1 inside DIR2
ln -s "$DIR1" "$DIR2/$NAME1"
echo "Success: Links created."
echo " $DIR1/$NAME2 -> $DIR2"
echo " $DIR2/$NAME1 -> $DIR1"
}- NIH: R01 CA242845.
- NIH: R01 AI088011.
- NIH: P30 CA225520 (PI: R. Mannel).
- NIH: P20 GM103640 and P30 GM145423 (PI: A. West).