请求编写Bash脚本:为目录树中MP3文件创建无重复symlink
Bash Script to Create Flat MP3 Symlinks (Skip Duplicates) for LibreTime
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 -pcommand creates the target folder and any parent folders if they don't exist already. - Find MP3 files: The
findcommand scans every subfolder in your source directory for files ending in .mp3 (case-insensitive, so .MP3 works too—remove-ifrom-inameif you want strict lowercase only). - Loop through each file: The
while read -rloop handles each MP3 file one by one, even if filenames have spaces or special characters. - Get filename:
basenamestrips off the full path, so/home/you/music/rock/song.mp3becomes justsong.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 -screates a soft link pointing back to the original MP3 file.
How to Use This Script
- Open a terminal and type
nano create_mp3_symlinks.shto open a text editor. - Copy and paste the script above, then replace
SOURCE_DIRandTARGET_DIRwith your actual folder paths. - Save the file by pressing
Ctrl+O, hit Enter, then exit withCtrl+X. - Make the script executable:
chmod +x create_mp3_symlinks.sh. - 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 -ein the terminal and add a line like0 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
相关产品推荐
相关产品推荐

