Skip to content

Instantly share code, notes, and snippets.

@jamestomasino
Last active September 13, 2017 13:19
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 jamestomasino/6345336521004401486d93624b2667ab to your computer and use it in GitHub Desktop.
Save jamestomasino/6345336521004401486d93624b2667ab to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -e
ROOT="/home/tomasino/movies"
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
# First loop to rename files
for f in $(find "$ROOT" -mindepth 1 -maxdepth 1 -type f -not -path '**/batchrename.sh' -not -path '**/.DS_Store'); do
# Wrap dates in parens if needed
rename -e 's/(?<!\()(\d{4})(?!\))/($1)/s' "$f"
done
# Second loop to move files
for f in $(find "$ROOT" -mindepth 1 -maxdepth 1 -type f -not -path '**/batchrename.sh' -not -path '**/.DS_Store'); do
# Get just filename
t=$(basename $f)
# Get filename without extension
p="${t%%)*})"
# Create directory, if needed
if [ ! -d "$p" ]; then
echo "Create Directory: $p"
cd "$ROOT" && mkdir -p "$p" && cd -
fi
# Move the file to the new folder
mv "$f" "$ROOT/$p"
done
IFS=$SAVEIFS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment