Skip to content

Instantly share code, notes, and snippets.

@jlmitch5
Created November 20, 2025 03:10
Show Gist options
  • Select an option

  • Save jlmitch5/1f4d9cea9efbac31210231a9c1d465dd to your computer and use it in GitHub Desktop.

Select an option

Save jlmitch5/1f4d9cea9efbac31210231a9c1d465dd to your computer and use it in GitHub Desktop.
For loading samples into octatrack. This script crawls a dir and updates all alias files to their actual files, as well as deletes any sort of macos metadata files
#!/bin/sh
################################################################################
# cleanUpSampleDir.sh
#
# Description:
# Cleans up audio sample directories by:
# 1. Removing macOS metadata files (.DS_Store, ._* resource forks, etc.)
# 2. Replacing macOS alias files with their original source files
#
# Usage:
# ./cleanUpSampleDir.sh <directory>
#
# Arguments:
# <directory> - Path to the directory containing sample files to clean up
#
# Example:
# ./cleanUpSampleDir.sh ~/Desktop/Samples
#
# Notes:
# - The script searches recursively through all subdirectories
# - Alias files are identified by their small file size (<5KB) and verified
# using AppleScript to check if they are true aliases
# - Original files are copied in place of aliases, preserving timestamps
# - Removes common macOS metadata: .DS_Store, ._*, .Spotlight-V100, .Trashes,
# .fseventsd, .TemporaryItems
#
# Based on: https://apple.stackexchange.com/questions/180762/how-to-go-to-alias-from-terminal
################################################################################
# Check if directory argument is provided
if [ -z "$1" ]; then
echo "Usage: $0 <directory>"
exit 1
fi
# Get the target directory
TARGET_DIR="$1"
# Check if target directory exists
if [ ! -d "$TARGET_DIR" ]; then
echo "Error: Directory '$TARGET_DIR' does not exist"
exit 1
fi
# Remove macOS metadata files
echo "Removing macOS metadata files..."
find "$TARGET_DIR" -type f \( -name ".DS_Store" -o -name "._*" -o -name ".Spotlight-V100" -o -name ".Trashes" -o -name ".fseventsd" -o -name ".TemporaryItems" \) -delete
echo "Metadata files removed"
# Remove files with " alias" suffix in their name
echo "Removing alias files..."
find "$TARGET_DIR" -type f -name "* alias" -delete
find "$TARGET_DIR" -type f -name "*alias*" | while read -r file; do
if [[ "$file" == *" alias"* ]]; then
echo "Deleting: $(basename "$file")"
rm "$file"
fi
done
echo "Alias files removed"
# Process all alias files recursively in the target directory
# Find files smaller than 5KB (likely aliases) and process them
find "$TARGET_DIR" -type f -size -5k | while read -r file; do
# Get the file size to check if it's likely an alias
filesize=$(stat -f%z "$file" 2>/dev/null)
# Skip if file is too large to be an alias
[ "$filesize" -gt 5000 ] && continue
# Get relative path for display
relpath="${file#$TARGET_DIR/}"
# Get the original path using AppleScript
thePath=$(osascript <<EOD
set toPath to ""
tell application "Finder"
set toPath to (POSIX file "$file") as alias
set theKind to kind of toPath
if theKind is "Alias" then
set toPath to ((original item of toPath) as alias)
return posix path of (toPath)
else
return ""
end if
end tell
EOD
)
# Replace the alias with the original file if it was an alias
if [ -n "$thePath" ]; then
echo "Replacing alias $relpath with original $(basename "$thePath")"
rm "$file"
cp -p "$thePath" "$file"
fi
done
echo "Done! All aliases replaced with original files and metadata cleaned up"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment