Skip to content

Instantly share code, notes, and snippets.

@charlieegan3
Created April 11, 2020 10:28
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 charlieegan3/f8e7d83603d37f3ce68c1dad1a1b3733 to your computer and use it in GitHub Desktop.
Save charlieegan3/f8e7d83603d37f3ce68c1dad1a1b3733 to your computer and use it in GitHub Desktop.
safe_flatten
#!/usr/bin/env bash
# run safe_flatten for each in folder
set -eo pipefail
root_dir=$1
if [ -z "$root_dir" ]
then
echo missing root dir
exit
fi
find "$root_dir" -mindepth 1 -maxdepth 1 -type d | while read dir; do
echo $dir
./safe_flatten.sh $dir
done
#!/usr/bin/env bash
# move all files in folder to the top level,
# rename as sum of file contents to handle duplicates,
# remove all empty folders
set -eo pipefail
root_dir=$1
if [ -z "$root_dir" ]
then
echo missing root dir
exit
fi
echo dir: $root_dir
find $root_dir -type f | while read file; do
new_file_name=$(md5sum "$file" | awk '{print $1}')
filename=$(basename -- "$file")
extension="${filename##*.}"
mv "$file" "$root_dir/$new_file_name.$extension"
done
find $root_dir -type d -empty -delete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment