FLAC Test Shell Script

Now that I am running raidrive under Linux, I converted my Windows PowerShell script to a Bash shell script. This is super convenient, and I am posting it here. I have been using this under Debian. It has two dependencies:

FLAC command-line tools:

apt install flac

bc command-line calculator:

apt install bc

Above is how these two get installed under Debian. Your distribution may be different. Some distros may even include bc by default. Debian does not.

Below is the complete script. I have been using this on all Debian installs. It is super handy. In the case of RaiDrive, this script will populate the local cache completely if you execute it at your mount point.

To use the script, pass the path as a parameter to start all FLAC tests using the FLAC command-line tool. It will report the total count of files, a color-code of PASS or FAIL, and the current percentage of completion. At the end, it will report all files that failed testing. In many cases, simply refreshing the mount point and re-running the script, you will find that the failed files then pass. This most likely happens due to a change of the file on the server, and the cached data is then stale. A re-execution will update the cache with the changed file.

In my case, I run it against all files like this:

flac_test_all_files.sh "/mnt/sftp"

Below is the complete script:

#!/bin/bash

# FLAC File Tester Script for Debian Linux Trixie
# Tests all FLAC files in a directory tree and reports results
# MODIFIED: Files are now processed in sorted (lexicographical) order.

# Function to display usage
usage() {
    echo "Usage: $0 <directory_path>"
    echo "Example: $0 /home/user/music"
    exit 1
}

# Check if directory argument is provided
if [ $# -ne 1 ]; then
    usage
fi

# Check if the provided path exists and is a directory
if [ ! -d "$1" ]; then
    echo "Error: '$1' is not a valid directory"
    exit 1
fi

# Check if flac command is available
if ! command -v flac &> /dev/null; then
    echo "Error: FLAC command not found in PATH"
    exit 1
fi

# Check if sort command is available
if ! command -v sort &> /dev/null; then
    echo "Error: sort command not found in PATH"
    exit 1
fi

# Initialize variables
START_DIR="$1"
TOTAL_FILES=0
PASSED_FILES=0
FAILED_FILES=0
CURRENT_FILE=0

# Array to store failed files
declare -a FAILED_FILE_LIST

echo "FLAC File Integrity Test"
echo "========================"
echo "Starting directory: $START_DIR"
echo ""

# Count total FLAC files first
echo "Counting FLAC files..."

while IFS= read -r -d '' file; do
    ((TOTAL_FILES++))
done < <(find "$START_DIR" -type f -iname "*.flac" -print0 | sort -z)

echo "Found $TOTAL_FILES FLAC files to test"
echo ""

# Test each FLAC file
while IFS= read -r -d '' file; do
    ((CURRENT_FILE++))
    
    # Calculate percentage with two decimal places using bc
    PERCENTAGE=$(echo "scale=2; ($CURRENT_FILE * 100) / $TOTAL_FILES" | bc -l)
    
    # Display current file and progress
    echo "[$CURRENT_FILE/$TOTAL_FILES] ($PERCENTAGE%) Testing: $file"
    
    # Test the FLAC file using flac -t (test mode)
    if flac -t "$file" &> /dev/null; then
        ((PASSED_FILES++))
        echo -e "  \033[32m✓ PASSED\033[0m"
    else
        ((FAILED_FILES++))
        FAILED_FILE_LIST+=("$file")
        echo -e "  \033[31m✗ FAILED\033[0m"
    fi
    
done < <(find "$START_DIR" -type f -iname "*.flac" -print0 | sort -z)

# Display summary
echo ""
echo "=============================================="
echo "FLAC FILE TEST SUMMARY"
echo "=============================================="
echo "Total files tested:  $TOTAL_FILES"
echo "Files passed:        $PASSED_FILES"
echo "Files failed:        $FAILED_FILES"
echo ""

# Show failed files if any
if [ $FAILED_FILES -gt 0 ]; then
    echo "FAILED FILES:"
    echo "-------------"
    for failed_file in "${FAILED_FILE_LIST[@]}"; do
        echo "  $failed_file"
    done
    echo ""
fi

# Calculate success rate with two decimal places
if [ $TOTAL_FILES -gt 0 ]; then
    SUCCESS_RATE=$(echo "scale=2; ($PASSED_FILES * 100) / $TOTAL_FILES" | bc -l)
    echo "Success rate: $SUCCESS_RATE%"
else
    echo "No FLAC files found to test"
fi

echo ""
echo "Test completed!"

# Exit with appropriate code
if [ $FAILED_FILES -eq 0 ]; then
    exit 0
else
    exit 1
fi

This topic was automatically closed after 30 days. New replies are no longer allowed.