Skip to content

Instantly share code, notes, and snippets.

@zeemyself
Created September 11, 2024 14:19
Show Gist options
  • Save zeemyself/bff57a016a352b2d996620f37db979da to your computer and use it in GitHub Desktop.
Save zeemyself/bff57a016a352b2d996620f37db979da to your computer and use it in GitHub Desktop.
Move all images outside subdirectory
#!/bin/bash
# Iterate over each subfolder in the main directory
find . -type d -mindepth 1 -maxdepth 1 | while read -r subfolder; do
# Remove the leading './' from the subfolder path
subfolder_name=$(basename "$subfolder")
echo "Processing subfolder: $subfolder_name full path: $subfolder"
# Iterate over each image file in the subfolder
find "$subfolder" -type f \( -name '*.png' -o -name '*.jpg' -o -name '*.jpeg' \) | while read -r image; do
# Extract the base name and file extension of the image (e.g., image.png or image.jpg)
# echo "Processing image: $image in subfolder: $subfolder_name"
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}"
# new_name="${stripped_path}"
# echo "Moving $base_name to $new_name"
# Move and rename the image to the parent directory
mv "$image" "./$new_name"
done
done
@zeemyself
Copy link
Author

curl -sSL https://gist.github.com/zeemyself/bff57a016a352b2d996620f37db979da/raw/2ac92449f236cef2fa03f47e1ee81fd7f1b85f1c/image_mover.sh | bash

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment