Skip to content

Instantly share code, notes, and snippets.

@stephenfeather
Last active December 10, 2023 21:14
Show Gist options
  • Save stephenfeather/3aa4a9dadf0d8f516c0dabade13390fd to your computer and use it in GitHub Desktop.
Save stephenfeather/3aa4a9dadf0d8f516c0dabade13390fd to your computer and use it in GitHub Desktop.
Creates a subdirecty tree based upon a file name to work around the 999 object list issues on S3
#!/bin/bash
#
# Designed to organize files into
# sub folders to a depth of X based
# upon the first X letters of the file name
# Currently --depth does not work
sourcepath=".";
targetpath=".";
depth=2;
extension=".jpg";
declare -i count;
declare -i i;
################################################################################
# Help #
################################################################################
Help()
{
# Display Help
echo "Add description of the script functions here."
echo
echo "Syntax: image-organize <options>"
echo "options:"
echo "-s --src Directory to scan"
echo "-t --target Directoy to place our newly created stucture into"
echo "-d --depth Depth of subfolders to create"
echo "-e --extension File extension to process"
echo "-h Print this Help."
echo "-v Verbose mode."
echo "-V Print software version and exit."
echo
}
while [[ "$#" -gt 0 ]]; do
case $1 in
-s|--src)
sourcepath="$2";
shift ;;
-t|--target)
targetpath="$2";
shift ;;
-d|--depth)
depth="$2";
shift ;;
-e|--extension)
extension="$2";
shift ;;
-h|--help)
Help;
exit 1 ;;
*)
echo "Unknown parameter passed: $1";
Help;
exit 1 ;;
esac
shift
done
function BuildNewPath()
{
# TODO: This can all be put into a loop to handle the --depth param
# Get First Letter of Filename in Lowercase
firstCharacter=$(echo "${filename:0:1}" | tr '[:upper:]' '[:lower:]');
#echo "$firstCharacter";
# Get Second Letter of Filename
secondCharacter=$(echo "${filename:1:1}" | tr '[:upper:]' '[:lower:]');
#echo "$secondCharacter";
if ([ $firstCharacter != "." ] && [ $secondCharacter != "." ]); then
thenewpath="$targetpath/$firstCharacter/$secondCharacter";
echo $thenewpath;
else
echo null;
fi
}
# Get our filelist
filelist=$(find $sourcepath -type f);
count=$(find . -type f | wc -l);
echo "Filecount: $count";
tput sc;
for file in $filelist;
do
i=$((i+1))
# Get Filename
filename=$(basename "$file");
tput rc;
tput el;
progress=$(bc <<<"scale=2; $i / $count * 100")
#progress=$((i/count*100))
echo -ne "Progress ($i/$count): $filename\r";
#sleep 1
# Create our new path
#$newpath=BuildNewPath $targetpath $filename;
newpath=$(BuildNewPath);
# Check if our chars are "."
if ( [ ! $newpath == "null" ]); then
# Make a directory with parents
mkdir -p $newpath;
#move file to newpath if it doesnt already exist
newfile="$newpath/$filename";
if [ ! -f "$newfile" ]; then
tput rc;
tput el;
mv $file $newpath;
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment