Created
October 22, 2019 05:07
-
-
Save scottstanfield/cc93a6ecd1b8579b8c33fad95f3a4e64 to your computer and use it in GitHub Desktop.
Lowerspace: remove spaces and lowercase all files and folders in argument
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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