Skip to content

Instantly share code, notes, and snippets.

@9had
Last active August 25, 2019 18:41
Show Gist options
  • Save 9had/ee2136df8e97533110e2e53c65197dee to your computer and use it in GitHub Desktop.
Save 9had/ee2136df8e97533110e2e53c65197dee to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Directory variables
# Where to start, in what directory.
start="/Users/ne/tmp/macnyt/music/"
# Subdir to look for.
sf="Soundfiles"
sb="soundbites"
# Let's move to working directory and get some work done.
cd ${start}
# find all files that end with /. Esentially this would give us directories.
for d in */; do
# incremental indicator for files, being reset to 1 for each directory
i=1
# Let's test that we actually do have directory and not something else.
if [ -d "$d" ]; then
# Get catalog no from directory name
# catno=$(echo ${d} | grep -oE '[0-9]+(?:-\d{2})?') # This regexp is not perfect yet...
catno=$(echo ${d} | grep -oE '\d{3}\-\d{2}|\d{3}') # this regexp is not pretty but it works.
# VERBOSE output for catalogID
echo "Katalognummer:" "${catno}"
# Catalog with multiple subfolders
if [[ $catno == *"-"* ]]; then
catsubStart=${catno:0:3}
catsubEnd=${catno:0:1}${catno:4:2}
while [ $catsubStart -le $catsubEnd ]; do
echo " Underkatalog" ${catsubStart}
cf=${start}${d}${sf}"/"${catsubStart}"/"${sb}
# ls -lh "${cf}"
j=1 # reset j to 1
for x in "${cf}"/*; do
if [ ${j} -lt 10 ] # if incremental indicator is less than 10 put leading zero in front the number.
then
sli="0"${j}
else
sli=${j} # otherwise keep it as it was.
fi
mv "${x}" "${start}${d}${sf}"/"$catsubStart"/"${sb}"/"$catsubStart"-"${sli}".mp3""
((j++))
done
((catsubStart++))
done
# figure a way to parse all subfolders between catno (eg: 611-12 = 611,612)
else
# Catalog with single folder
# Find all files in subdir
for f in "${d}"${sf}"/"${sb}/*; do
# Let's make incremental a little bit more user friendly with leading zeroes, for sorting purposes.
if [ ${i} -lt 10 ] # if incremental indicator is less than 10 put leading zero in front the number.
then
li="0"${i}
else
li=${i} # otherwise keep it as it was.
fi
# Let's do the magic now.
# Rename files so they are "catalogID-incrimental.mp3" and keep the structure.
# echo "${start}${f}" "${start}${d}${sf}"/"${sb}"/"${catno}"-"${li}".mp3""
mv "${start}${f}" "${start}${d}${sf}"/"${sb}"/"${catno}"-"${li}".mp3""
# VERBOSE just to indicate how many files this catalog directory had renamed. Every "dot" is a single file.
echo -n "."
# increment indicators for each file found.
((i++))
done
fi
# VERBOSE create new line for new catalog output.
echo ""
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment