Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- exec 2>/dev/null
- root_dir=$(realpath $1)
- meta_dir="$root_dir/!meta"
- for blog in $root_dir/*; do
- if [ ! -d $blog ]; then
- continue
- fi
- if [ $blog = $meta_dir ]; then
- echo 'Meta dir found, skipping...'
- continue
- fi
- for dir in $(find $blog -d 1 -type d -not -name 'json' -not -name 'media'); do
- rm -rf "$dir"
- done
- json_dir="$blog/json"
- if [ -d $json_dir ]; then
- find $json_dir -d 1 -type f -not -name '*.json' -exec rm -f "{}" \;
- else
- if [ -e $json_dir ]; then
- rm -rf "$json_dir"
- fi
- mkdir "$json_dir"
- fi
- for file in $(find $blog -d 1 -type f -name '*.json' | sort -r); do
- file_name=$(basename $file)
- hash=$(md5 -q $file)
- if [ $(find $json_dir -d 1 -type f -name '*json' | xargs md5 -q | grep -c "$hash") -lt 1 ]; then
- mv "$file" "$json_dir/$file_name"
- continue
- fi
- rm -f "$file"
- done
- media_dir="$blog/media"
- if [ -d $media_dir ]; then
- for image in $(find $media_dir -d 1 -type f); do
- image_type=$(identify -format '%m\n' "$image" | awk 'NR==1 { print tolower($1) } NR > 1 { exit }')
- # Not an image and not JSON since we've already removed them
- if [ -z "$image_type" ]; then
- rm -f "$image"
- continue
- fi
- # Image, need to move it to media_dir
- image_name="$(basename $image | awk -F '.' 'NR==1 { print $1 } NR > 1 { exit }').$image_type"
- if [ "$image_name" = "$(basename $image)" ]; then
- continue
- fi
- mv "$image" "$media_dir/$image_name"
- done
- else
- if [ -e $media_dir ]; then
- rm -rf "$media_dir"
- fi
- mkdir "$media_dir"
- fi
- for image in $(find $blog -d 1 -type f); do
- image_type=$(identify -format '%m\n' "$image" | awk 'NR==1 { print tolower($1) } NR > 1 { exit }')
- # Not an image and not JSON since we've already removed them
- if [ -z "$image_type" ]; then
- rm -f "$image"
- continue
- fi
- # Image, need to move it to media_dir
- image_name="$(basename $image | awk -F '.' 'NR==1 { print $1 } NR > 1 { exit }').$image_type"
- mv "$image" "$media_dir/$image_name"
- done
- if [ $(ls $media_dir | wc -l) -lt 1 ]; then
- rm -rf $blog
- printf 'Removed empty blog %s\n' $(basename $blog)
- continue
- fi
- printf 'Trimmed %s\n' $(basename $blog)
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement