Skip to content

Instantly share code, notes, and snippets.

@xmedeko
Created September 21, 2016 09:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save xmedeko/afe62d3a9334730b87bf20833fe12617 to your computer and use it in GitHub Desktop.
Save xmedeko/afe62d3a9334730b87bf20833fe12617 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
@xmedeko
Copy link
Author

xmedeko commented Jul 22, 2019

@yalaudah The script needs a mounted by gcsfuse and working gsutil command. The mounted gcsfuse dir, where you want to created dirs is a first argument of the script. (The gs:// uri for gsutil is recognized from the output of mount command.)

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