Skip to content

Instantly share code, notes, and snippets.

@peterrosell
Forked from xmedeko/gsmkdirs.sh
Created July 2, 2019 08:55
Show Gist options
  • Save peterrosell/7b766054ebe2852d7ee7e00c5c81af4f to your computer and use it in GitHub Desktop.
Save peterrosell/7b766054ebe2852d7ee7e00c5c81af4f to your computer and use it in GitHub Desktop.
Google Cloud Bucket: create explicit dirs
#!/bin/bash
##
## Creates explicit dirs on the bucket
##
function print_help() {
echo "Usage:" $(basename $0) bucket_mounted_dir
exit 1
}
if [[ -z $1 || ! -d $1 ]]; then
print_help
fi
# Expect mount dir is /*/*
path=$(echo $1|sed -rn 's|^/[^/]+/[^/]+/?||p')
mnt=${1%$path}
mnt=${mnt%/}
path=${path%/}
bucket=$(mount | grep -F "$mnt")
bucket=${bucket%% *}
if [[ -z "$bucket" || "$bucket" == /* ]]; then
echo No or wrong mount point for $mnt
exit 1
fi
bucket=gs://"$bucket"
echo Creating dirs for $bucket/$path in $mnt/$path ...
echo Press ENTER to continue ...
read
declare -A cdirs # created dirs
gsutil ls -r "$bucket/$path" | while IFS='' read -r line; do
if [[ -z "$line" || "$line" == */ || "$line" == *: ]]; then continue; fi
#echo Line $line
dir=${line#$bucket}
if [[ -z $dir ]]; then continue; fi
dir=${dir%/*} # dirname
#echo Examining $dir
if [[ -z $dir || ${cdirs[$dir]+1} ]]; then continue; fi
cdirs[$dir]=1
diskdir="$mnt$dir"
if [[ -e "$diskdir" ]]; then
echo Dir already exists $diskdir
continue
fi
echo Creating $diskdir
mkdir -p "$diskdir"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment