📄 corrupted-files-finder | home

Corrupted Files Finder for Linux

guest@techdiy:~$

Media & Document File Corruption Checker Script

Overview

This Bash script is a handy tool for verifying the integrity of common media (images, videos, audio) and document files on Linux systems.

It’s designed for speed, accuracy, and safety, helping you find corrupted files without overloading your CPU.

Whether you’re a photographer with thousands of images, a video editor managing terabytes of footage, or just someone archiving documents — this tool helps keep your files healthy.

Features

○ Supports multiple file types:

○ Accurate verification:

○ Lists only corrupted files — no unnecessary noise.

Why Use This Script?

File corruption can happen for many reasons:

This script ensures you find the bad files before you need them — preventing nasty surprises.

Requirements

You’ll need the following tools installed:

imagemagick, ffmpeg, poppler-utils, unzip

Here's the script:

corrupted-files-finder.sh:
#!/usr/bin/env bash

### How To Use
# chmod +x this_script.sh
# ./this_script.sh /path/to/folder yes    # recursive (default)
# ./this_script.sh /path/to/folder no     # only current directory (--max-depth=1)
#
# NOTE: Monitor system resources (use `htop`, `btop` etc) if run recursively 
# ..to avoid system crashes! if the content is too large.
###

# Define supported file types and their categories
declare -A FILE_TYPES=(
    [mp3]="audio"
    [wav]="audio"
    [flac]="audio"
    [m4a]="audio"
    [wma]="audio"
    [mp4]="video"
    [mkv]="video"
    [mov]="video"
    [wav]="video"
    [avi]="video"
    [wmv]="video"
    [flv]="video"
    [webm]="video"
    [3gp]="video"
    [mpg]="video"
    [mpeg]="video"
    [vob]="video"
    [jpg]="image"
    [jpeg]="image"
    [png]="image"
    [gif]="image"
    [bmp]="image"
    [tiff]="image"
    [webp]="image"
    [svg]="image"
    [pdf]="doc"
    [docx]="doc"
    [odt]="doc"
)

# Get input arguments
SCAN_PATH="${1:-.}"
RECURSIVE="${2:-yes}"

# Determine maxdepth option for find
if [[ "$RECURSIVE" =~ ^[Yy](es)?$ ]]; then
    MAXDEPTH=""
else
    MAXDEPTH="-maxdepth 1"
fi

# Check required tools
for tool in identify ffprobe pdftotext unzip file; do
    if ! command -v "$tool" >/dev/null 2>&1; then
        echo "Error: '$tool' is required but not installed. Please install it and rerun."
        exit 1
    fi
done

# Count total files before scanning
TOTAL_FILES=0
for ext in "${!FILE_TYPES[@]}"; do
    count=$(find "$SCAN_PATH" $MAXDEPTH -type f -iname "*.$ext" | wc -l)
    TOTAL_FILES=$((TOTAL_FILES + count))
done

if (( TOTAL_FILES == 0 )); then
    echo "No supported files found to scan."
    exit 0
fi

echo "Found $TOTAL_FILES files to scan."
echo "Scanning folder: $SCAN_PATH"
echo "Recursive scan: $([[ -z "$MAXDEPTH" ]] && echo yes || echo no)"
echo

check_file() {
    local file="$1"
    local ext="${file##*.}"
    ext="${ext,,}"  # convert to lowercase

    case "${FILE_TYPES[$ext]}" in
        image)
            identify "$file" >/dev/null 2>&1
            ;;
        video|audio)
            ffprobe -v error -show_format -show_streams "$file" >/dev/null 2>&1
            ;;
        doc)
            case "$ext" in
                pdf)
                    pdftotext "$file" - >/dev/null 2>&1
                    ;;
                docx|odt)
                    unzip -tq "$file" >/dev/null 2>&1
                    ;;
                *)
                    file "$file" >/dev/null 2>&1
                    ;;
            esac
            ;;
        *)
            return 0 # Unknown type, skip
            ;;
    esac
}

CORRUPTED_FILES=()

echo "Starting corruption scan..."
scanned=0
for ext in "${!FILE_TYPES[@]}"; do
    while IFS= read -r -d '' file; do
        scanned=$((scanned + 1))
        printf "\rScanning files: %d/%d" "$scanned" "$TOTAL_FILES"

        if ! check_file "$file"; then
            CORRUPTED_FILES+=("$file")
        fi
    done < <(find "$SCAN_PATH" $MAXDEPTH -type f -iname "*.$ext" -print0)
done
echo # newline after progress

echo
if [[ ${#CORRUPTED_FILES[@]} -eq 0 ]]; then
    echo "✅ No corrupted files found."
else
    echo "⚠️ Corrupted files found:"
    for f in "${CORRUPTED_FILES[@]}"; do
        echo "  $f"
    done
fi

	

👣 Make the file executable:

chmod +x corrupted-files-finder.sh

👣 Run the script...

./corrupted-files-finder.sh /path/to/folder yes
  • First argument = folder to scan (. for current directory)
  • Second argument = yes for recursive, no for current folder only

** You can ignore the last yes as the recursive scan is the default behaviour anyway!

Tips

nice -n 19 ./corrupted-files-finder.sh /path/to/folder

💡 Notes:

This script is experimental and should not be considered as the complete solution for detecting the corrupted files. It may be extended in the future to improve its effectiveness.

⚠️ Disclaimer:

This script is provided as-is, without any warranties or guarantees of any kind. Use of this at your own risk.