Skip to content

Instantly share code, notes, and snippets.

@modpluz
Created July 6, 2017 18:28
Show Gist options
  • Save modpluz/6890f56dca1e5ec79b86174bb4739853 to your computer and use it in GitHub Desktop.
Save modpluz/6890f56dca1e5ec79b86174bb4739853 to your computer and use it in GitHub Desktop.
Copy media files and create directory if necessary
#!/usr/bin/env bash
shopt -s globstar
numberRegex='^[0-9]+$'
for d in static/* static/**/* ; do
if [ -d ${d} ]; then
bn=$(basename $d)
# We only care about media resources
if [[ "$bn" = "images" || ("$bn" = "videos" || "$bn" = "sounds") ]]; then
for filepath in $(ls $d) ; do
mediaId="${filepath%.*}"
if ! [[ $mediaId =~ $numberRegex ]] ; then
mediaDir="/media/$mediaId"
# create directory if it doesn't exist
if [ ! -d "$mediaDir" ]; then
mkdir "$mediaDir"
fi
# copy file
cp "$d/$filepath" "$mediaDir/$filepath"
fi
done
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment