Last active
June 25, 2023 21:04
-
-
Save RadicalZephyr/811883ae2b154c03a5bd to your computer and use it in GitHub Desktop.
Copy image files from a folder and rename based on type.
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
#!/usr/bin/env bash | |
set -e | |
if [ $# -lt 1 ] | |
then | |
echo "You must enter the name of a folder." | |
exit 1 | |
fi | |
extract_and_rename_files() { | |
FOLDER="$1" | |
FILETYPES="$2" | |
EXTENSION="$3" | |
DEST_FOLDER="${FOLDER}/${EXTENSION}" | |
mkdir -p "$DEST_FOLDER" | |
grep -i "$EXTENSION" "$FILETYPES" | cut -d: -f1 | xargs -I '{}' cp '{}' "$DEST_FOLDER" | |
shopt -s nullglob | |
for f in "$DEST_FOLDER"/* | |
do | |
mv $f $f.${EXTENSION} | |
done | |
shopt -u nullglob | |
} | |
TOPROCESS="$1" | |
FOLDER=$(mktemp -d image-search-XXXXX) | |
FILETYPES="$FOLDER/types" | |
echo "Finding all file type information..." | |
find ${TOPROCESS%/} -type f | xargs file | grep image > $FILETYPES | |
IMG_EXTENSIONS="jpeg png gif tiff" | |
for ext in $IMG_EXTENSIONS | |
do | |
echo "Copying out and renaming $ext images" | |
extract_and_rename_files $FOLDER $FILETYPES $ext | |
done | |
rm $FILETYPES | |
echo "Now look in the subfolders of $FOLDER for your image." | |
echo "There are:" | |
for ext in $IMG_EXTENSIONS | |
do | |
echo -en "\t" $(ls "$FOLDER/${ext}" | wc -l) "${ext} files" | |
echo " in $FOLDER/${ext}" | |
done | |
echo "You can delete $FOLDER when you've found the correct image." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For the non-programers out there, here is a brief run down of what this script is doing. I'm going to gloss over some details so I won't comment on every line of the script.
First, line 3 ensures that if anything goes wrong, the script stops immediately.
Lines 5-9 ensure that you've told the script the name of a folder to process, otherwise it prints a message and then quits.
Lines 11-24 define the process of extracting and renaming all of the files of a given extension type.
Lines 12-16 are just setup, they don't actually do anything in your computer.