Created
March 2, 2025 18:59
-
-
Save zeemyself/c50582f57ba24c037cde34b2131a6977 to your computer and use it in GitHub Desktop.
Image mover v2
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 | |
# Define extensions | |
image_extensions=("png" "jpg" "jpeg" "gif" "webp") | |
video_extensions=("mp4" "mkv" "avi" "mov" "wmv" "mpg") | |
# Parse command line arguments | |
include_videos=false | |
while getopts "v" opt; do | |
case $opt in | |
v) include_videos=true ;; | |
*) echo "Usage: $0 [-v]" >&2 | |
exit 1 ;; | |
esac | |
done | |
# Prepare extensions array based on parameters | |
extensions=("${image_extensions[@]}") | |
if [ "$include_videos" = true ]; then | |
extensions+=("${video_extensions[@]}") | |
fi | |
# Create the find command extension pattern | |
find_pattern="" | |
for ext in "${extensions[@]}"; do | |
find_pattern="$find_pattern -o -iname '*.$ext'" | |
done | |
find_pattern=${find_pattern:4} # Remove the first " -o " | |
# Iterate over each subfolder in the main directory | |
find . -type d -mindepth 1 -maxdepth 1 | while read -r subfolder; do | |
subfolder_name=$(basename "$subfolder") | |
# Use eval to properly interpret the find command pattern | |
eval "find \"$subfolder\" -type f \( $find_pattern \)" | while read -r image; do | |
stripped_path=$(echo "$image" | cut -d'/' -f3- | tr '/' '_') | |
# echo "$image ----> $stripped_path" | |
base_name=$(basename "$image") | |
extension="${base_name##*.}" | |
name_no_ext="${base_name%.*}" | |
# Define the new filename with the specified format | |
# new_name="${subfolder_name}_${name_no_ext}.${extension}" | |
new_name="${subfolder_name}_${stripped_path}" | |
# Uncomment the following lines when ready to perform the move | |
mv "$image" "./$new_name" | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
curl -sSL https://gist.githubusercontent.com/zeemyself/c50582f57ba24c037cde34b2131a6977/raw/cb4960f6abea1aedd12224fd6eedb671dd904d7d/image_mover_v2.sh | bash -s -- -v