Created some bash-script for adding an extension to files where it is missing.
Based the code on provided answer, but with some addition way to make an educated guess for files where the response would be ???.
These are the important parts of the script:
# First try to guess the extension using `file --extension`
extension=$(file -b --extension "$file" | awk -F'/' '{print $1}')
# If the extension is "???" or similar (invalid), try the second method
if [[ "$extension" == "???" || -z "$extension" ]]; then
# Guess the file type description using `file`
file_type=$(file -b "$file")
# Use the function to get a better extension based on the description
extension=$(get_extension_by_description "$file_type")
fi
# Function to map file descriptions to appropriate extensions
get_extension_by_description() {
local file_type="$1"
case "$file_type" in
*PDF*) echo "pdf" ;;
*EPUB*) echo "epub" ;;
*HTML*) echo "html" ;;
*ASCII*) echo "txt" ;;
*Zip*) echo "zip" ;;
*RAR*) echo "rar" ;;
*JPEG*) echo "jpg" ;;
*PNG*) echo "png" ;;
*DjVu*) echo "djvu" ;;
*Mobipocket\ E-book*) echo "mobi" ;;
*Microsoft\ Word*) echo "docx" ;;
*OpenDocument\ Text*) echo "odt" ;;
*Microsoft\ Excel*) echo "xlsx" ;;
*OpenDocument\ Spreadsheet*) echo "ods" ;;
*MP3*) echo "mp3" ;;
*MPEG*) echo "mpg" ;;
*Matroska*) echo "mkv" ;;
*Video*) echo "mp4" ;;
*Audio*) echo "wav" ;;
*) echo "unknown" ;;
esac
}
Full script can be found here: https://gist.github.com/Aldo-f/eefbc893dd7f2403953e7cd01e3c8848