Last active
January 8, 2019 19:14
-
-
Save hamidzr/4d83cd940622a348705d64793634bde8 to your computer and use it in GitHub Desktop.
distribute files in a directory to multiple subdirectories (sortof a simple dht)
This file contains 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 | |
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
to see the final results