Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix File Names for Unix Environment #1322

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .local/bin/fixnames
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/dash

[ -z "${1}" ] && {
printf "%s\n" "Specify a file path as an argument. e.g., fixnames <path>" >&2
exit "1"
}

[ "$(id -u)" = "0" ] && printf "%s\n" "This script should not be run as root" >&2 && exit "1"

find "${1}" -depth \( -path '*/.*' -o -path '*/.*/*' -o -path '.' \) -prune -o \( -type f -o -type d \) -print0 | sort -zr | xargs -0 -P "0" -I {} dash -c '

base="${1##*/}"
path="${1%/*}"

pattern="s/[^a-zA-Z0-9 ._-]//g; s/[ .-]/_/g; s/_+/_/g; s/^_+//; s/_+$//; s/[A-Z]/\L&/g"

[ -f "${1}" ] && pattern="${pattern}; s/_([^_]+)$/.\\1/"

new_name="$(printf "%s\n" "${base}" | sed -E "${pattern}")"

[ "${base}" != "${new_name}" ] && [ -e "${path}/${new_name}" ] && new_name="${$}_${new_name}"
[ "${base}" != "${new_name}" ] && mv "${1}" "${path}/${new_name}"
' _ {}