Skip to content

Instantly share code, notes, and snippets.

@WebDevJL
Created December 20, 2020 09:57
Show Gist options
  • Save WebDevJL/69cee6132fa9793097f39033d3cbc656 to your computer and use it in GitHub Desktop.
Save WebDevJL/69cee6132fa9793097f39033d3cbc656 to your computer and use it in GitHub Desktop.
# List all folders in a given directory ($1)
# Source directory contains directories like "my dir - a description"
# We will remove it 2 steps:
# => Remove the spaces
# => Replace .-. in a simple dash.
# => See https://unix.stackexchange.com/questions/86722/how-do-i-loop-through-only-directories-in-bash
for d in $1*/ ; do
echo "current: $d"
#rename ' - ' '-' $d # doesn't work on windows, even with bash install. rename require to install through apt-get.
#rename ' ' '.' $d # doesn't work on windows, even with bash install. rename require to install through apt-get.
amendedDir="${d// /\\\ }"
echo "amendedDir: $amendedDir"
newDir="${d// /.}"
echo "newDir: $newDir"
# In general, it is a good idea to put double-quotes around every reference to a shell variable.
# ${f/ /} removes just the first occurrence of a space. To remove all spaces, use ${f// /}.
# => See https://stackoverflow.com/questions/26519301/bash-error-renaming-files-with-spaces-mv-target-is-not-a-directory
mv "$d" "${d// /.}"
finalDir="${newDir/\.\-\./\-}"
echo "finalDir: $finalDir"
mv "$newDir" "$finalDir"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment