Skip to content

Instantly share code, notes, and snippets.

@scottstanfield
Created October 22, 2019 05:07
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 scottstanfield/cc93a6ecd1b8579b8c33fad95f3a4e64 to your computer and use it in GitHub Desktop.
Save scottstanfield/cc93a6ecd1b8579b8c33fad95f3a4e64 to your computer and use it in GitHub Desktop.
Lowerspace: remove spaces and lowercase all files and folders in argument
#!/usr/bin/env bash
##
## lowerspace.sh directory
## Remove spaces and lower case all files and folders in `directory`
##
set -o xtrace
set -o errexit
set -o nounset
die() {
ret=$?
printf "%s\n" "$@" >&2
exit "$ret"
}
ren() {
local newname
newname=${1// /_}
newname=${newname,,}
echo "$1" "$newname"
[[ $1 != "$newname" ]] && mv -- "$1" "${newname}"
}
traverse() {
local file
cd -- "$1" || exit
for file in *; do
[[ -d $file ]] && traverse "$file"
ren "$file"
done
cd .. || exit
}
shopt -s nullglob dotglob
[[ $# -ne 1 ]] && die "Usage: $0 folder_name"
SRC=$1
[[ -d "$"SRC ]] && die "$SRC does not exist"
traverse $SRC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment