You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求编写Bash脚本:为目录树中MP3文件创建无重复symlink

Hey there! Since you're setting up LibreTime for automated radio and haven't worked with Bash scripts before, I'll make this super straightforward. This script will scan all your MP3s in directory X (and its subfolders), create soft links to them in directory Y, keep everything flat (no nested folders), and skip any duplicates so you don't end up with conflicting files.

The Complete Script

#!/bin/bash

# Replace these paths with your actual directories
SOURCE_DIR="/path/to/your/mp3/library"  # This is your directory X
TARGET_DIR="/path/to/libretime/media"   # This is your directory Y

# Check if source directory exists
if [ ! -d "$SOURCE_DIR" ]; then
    echo "Error: Source directory $SOURCE_DIR doesn't exist!"
    exit 1
fi

# Create target directory if it doesn't exist
mkdir -p "$TARGET_DIR"

# Find all MP3 files and process each one
find "$SOURCE_DIR" -type f -iname "*.mp3" | while read -r file; do
    # Get just the filename (without the path)
    filename=$(basename "$file")
    
    # Check if a file/link with this name already exists in target directory
    if [ -e "$TARGET_DIR/$filename" ]; then
        echo "Skipping duplicate: $filename (already exists in $TARGET_DIR)"
    else
        # Create the symbolic link
        ln -s "$file" "$TARGET_DIR/$filename"
        echo "Created link: $TARGET_DIR/$filename -> $file"
    fi
done

echo "Done! All non-duplicate MP3 links are in $TARGET_DIR"

Let's Break This Down (So You Understand What's Happening)

  • First two lines: We define variables for your source (X) and target (Y) directories. Just swap out the example paths with your actual folder locations.
  • Source directory check: Makes sure the script doesn't run if your MP3 library doesn't exist—no point in wasting time!
  • Create target directory: The mkdir -p command creates the target folder and any parent folders if they don't exist already.
  • Find MP3 files: The find command scans every subfolder in your source directory for files ending in .mp3 (case-insensitive, so .MP3 works too—remove -i from -iname if you want strict lowercase only).
  • Loop through each file: The while read -r loop handles each MP3 file one by one, even if filenames have spaces or special characters.
  • Get filename: basename strips off the full path, so /home/you/music/rock/song.mp3 becomes just song.mp3.
  • Check for duplicates: If a file or link with the same name is already in the target folder, we skip it and print a message.
  • Create symlink: If no duplicate exists, ln -s creates a soft link pointing back to the original MP3 file.

How to Use This Script

  1. Open a terminal and type nano create_mp3_symlinks.sh to open a text editor.
  2. Copy and paste the script above, then replace SOURCE_DIR and TARGET_DIR with your actual folder paths.
  3. Save the file by pressing Ctrl+O, hit Enter, then exit with Ctrl+X.
  4. Make the script executable: chmod +x create_mp3_symlinks.sh.
  5. Run it: ./create_mp3_symlinks.sh.

Quick Notes for LibreTime

  • Since LibreTime reads media from a specified folder, keeping all your MP3 links flat in Y makes it easy for the system to index everything without nested folders.
  • To keep your LibreTime library updated automatically, you can set up a cron job to run this script daily/weekly. Just type crontab -e in the terminal and add a line like 0 2 * * * /path/to/create_mp3_symlinks.sh (this runs it at 2 AM every day).
  • Soft links don't take up extra space—they just point to your original files, so don't delete the original MP3s in directory X!

内容的提问来源于stack exchange,提问作者ANIMATEK

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:32:42