Created
January 29, 2024 02:16
-
-
Save allenk/276aea7c8de35a724867bdf33c45032e to your computer and use it in GitHub Desktop.
Recognize file headers and rename file extensions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) | |
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