Last active
February 3, 2025 11:09
-
-
Save vietvudanh/fc5c45ee22324d5f8f782bf0ac87a078 to your computer and use it in GitHub Desktop.
lm-symlink.sh
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: ./lm-symlink.sh <target_directory> | |
# Example: ./lm-symlink.sh ~/.cache/lm-studio/models | |
# Check if the target directory is provided | |
if [ -z "$1" ]; then | |
echo "Usage: $0 <target_directory>" | |
echo " <target_directory> is the path to the LM Studio models folder." | |
exit 1 | |
fi | |
TARGET_DIR="$1" | |
# Check if the target directory exists and is a directory | |
if [ ! -d "$TARGET_DIR" ]; then | |
echo "Error: Target directory '$TARGET_DIR' does not exist or is not a directory." | |
exit 1 | |
fi | |
# Source directory (Hugging Face Hub cache) | |
SOURCE_DIR="$HOME/.cache/huggingface/hub" | |
# Check if the source directory exists | |
if [ ! -d "$SOURCE_DIR" ]; then | |
echo "Error: Source directory '$SOURCE_DIR' does not exist." | |
exit 1 | |
fi | |
echo "Scanning Hugging Face Hub cache: $SOURCE_DIR" | |
# Loop through all subdirectories in the Hugging Face Hub cache | |
find "$SOURCE_DIR" -maxdepth 1 -type d -print0 | while IFS= read -r -d $'\0' repo_dir; do | |
# Skip if it's the root directory | |
if [ "$repo_dir" = "$SOURCE_DIR" ]; then | |
continue | |
fi | |
repo_name=$(basename "$repo_dir") | |
# Split the repo_name by "--" | |
parts=(${repo_name//--/ }) | |
# Extract the last part as model name | |
target_parent_dir="$TARGET_DIR/${parts[1]}" | |
model_name="${parts[-1]}" | |
echo "Processing model: $model_name, @ $repo_name" | |
# Navigate to the snapshots directory | |
snapshots_dir="$repo_dir/snapshots" | |
if [ ! -d "$snapshots_dir" ]; then | |
echo "Warning: No 'snapshots' directory found in $repo_dir, skipping." | |
continue | |
fi | |
# Loop through each snapshot directory (hash) within snapshots | |
for snapshot_dir_hash in "$snapshots_dir/"*; do | |
mkdir -p "$target_parent_dir/$model_name" | |
for link in "$snapshot_dir_hash/"*; do | |
real_target=$(readlink -f "$link") | |
link_basename=$(basename "$link") | |
target_link="$target_parent_dir/$model_name/$link_basename" | |
# Create the symlink | |
if [ -e "$target_link" ]; then | |
echo "Warning: '$target_link' already exists. Skipping." | |
continue | |
fi | |
ln -s "$real_target" "$target_link" | |
if [ $? -eq 0 ]; then | |
echo "Created symlink: $target_link -> $real_target" | |
else | |
echo "Error creating symlink: $target_link -> $real_target" | |
fi | |
done | |
done | |
echo "--" | |
done | |
echo "Done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment