Skip to content

Instantly share code, notes, and snippets.

@hamidzr
Last active January 8, 2019 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hamidzr/4d83cd940622a348705d64793634bde8 to your computer and use it in GitHub Desktop.
Save hamidzr/4d83cd940622a348705d64793634bde8 to your computer and use it in GitHub Desktop.
distribute files in a directory to multiple subdirectories (sortof a simple dht)
#!/bin/bash
# migrates a single file to a proper sub directory. "parallel" safe.
# run it in the target directory using parallel: `ls . | parallel PATH_TO_THIS_SCRIPT`
fname=$1
# hashes the project name and retuns the first n chars
# each char 36 diff values => divides to 16^n sub directories
function dirName()
{
local id=$1
local dir_name=$(echo $id | md5sum | sed 's/\([a-z0-9]\{2\}\).*/\1/')
[ ! -d $dir_name ] && mkdir $dir_name
echo $dir_name
}
# if it is a valid file
if [ -f $fname ]; then
echo moving $fname
dir_name=$(dirName $fname)
mv $id.project $dir_name/$fname
else
echo $fname is directory or doesnt exist
fi
@hamidzr
Copy link
Author

hamidzr commented Jan 8, 2019

to see the final results

for dir in `ls .`; do                                                                                                            
  ls $dir | wc -l
done

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