Skip to content

Instantly share code, notes, and snippets.

@allenk
Created January 29, 2024 02:16
Show Gist options
  • Save allenk/276aea7c8de35a724867bdf33c45032e to your computer and use it in GitHub Desktop.
Save allenk/276aea7c8de35a724867bdf33c45032e to your computer and use it in GitHub Desktop.
Recognize file headers and rename file extensions
#!/bin/bash
# test all files
for file in *; do
if [ -f "$file" ]; then
# read first 3 bytes
bytes=$(head -c 3 "$file" | od -An -t x1 | tr -d ' \n')
# test file bytes
case $bytes in
504b03)
# ZIP
mv "$file" "${file}.zip"
;;
ffd8ff)
# JPG
mv "$file" "${file}.jpg"
;;
255044)
# PDF
mv "$file" "${file}.pdf"
;;
524946)
# WEBM
mv "$file" "${file}.webm"
;;
# Others
*)
# not match
;;
esac
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment