#!/bin/bash # Check if enough arguments are passed if [ "$#" -ne 1 ]; then echo "Usage: $0 " exit 1 fi # Function to truncate filename to a maximum length, preserving the extension truncate_filename() { local filename="$1" local max_length="$2" local extension="${filename##*.}" local base_name="${filename%.*}" if [ ${#filename} -gt $max_length ]; then local truncated_base="${base_name:0:($max_length - ${#extension} - 1)}" echo "${truncated_base}.${extension}" else echo "$filename" fi } # Read the file paths from the input file while IFS= read -r file_path; do # Extract the directory and filename dir_path=$(dirname "$file_path") file_name=$(basename "$file_path") # Define the new filename by replacing or removing illegal characters new_file_name=$(echo "$file_name" | tr -d '<>:"/\\|?*' | tr '[:cntrl:]' '_' | sed 's/[—·]/-/g' | sed 's/"/_/g' | sed "s/'/_/g") # Ensure filename does not end with an underscore new_file_name=$(echo "$new_file_name" | sed 's/_$//') # Truncate the filename if it is longer than 200 characters, preserving the extension new_file_name=$(truncate_filename "$new_file_name" 200) # Construct the new path new_path="$dir_path/$new_file_name" # Rename the file if necessary if [ "$new_file_name" != "$file_name" ]; then echo "Renaming '$file_path' to '$new_path'" mv "$file_path" "$new_path" fi done <"$1"