Created
April 30, 2024 15:46
-
-
Save RyanZurrin/0b2a2f97c78db13a7523f1e4f0bf5cbd to your computer and use it in GitHub Desktop.
symlink target directory to source directory
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 | |
# Usage: ./symlink_full_dir.sh /path/to/source /path/to/target | |
SOURCE_DIR=$1 | |
TARGET_DIR=$2 | |
# Check if the source directory exists | |
if [ ! -d "$SOURCE_DIR" ]; then | |
echo "Source directory does not exist: $SOURCE_DIR" | |
exit 1 | |
fi | |
# Create the target directory if it does not exist | |
mkdir -p "$TARGET_DIR" | |
# Function to create symlinks | |
create_symlinks() { | |
local src_path=$1 | |
local tgt_path=$2 | |
# Find all directories and files in the source path | |
find "$src_path" -mindepth 1 -type d -printf "%P\n" | while read dir; do | |
# Create corresponding directory in the target path | |
mkdir -p "$tgt_path/$dir" | |
done | |
find "$src_path" -type f -printf "%P\n" | while read file; do | |
# Create a symlink for each file | |
ln -s "$src_path/$file" "$tgt_path/$file" | |
done | |
} | |
# Create symlinks, keeping the same structure | |
create_symlinks "$SOURCE_DIR" "$TARGET_DIR" | |
echo "Symlinks created successfully." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment