/fix_filename.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #!/bin/bash for ARG in "$@" ; do # Separate the directory part into DIR. DIR= "${ARG%/*}" [ "$DIR" = "$ARG" ] && DIR= "./" # Separate the file name part into OLD. OLD= "${ARG##*/}" # Copy OLD to NEW, replacing everything except # - . 0-9 A-Z a-z with a single _ # Also make sure NEW starts with 0-9 A-Z a-z NEW= "${OLD//[^-0-9A-Za-z.]/_}" TMP= "" while [ "$TMP" != "$NEW" ]; do TMP= "$NEW" NEW= "${NEW//__/_}" NEW= "${NEW#[^0-9A-Za-z]}" NEW= "${NEW%[^0-9A-Za-z]}" done # No fix necessary? [ "$OLD" = "$NEW" ] && continue # Is the new string empty? if [ -z "$NEW" ]; then printf '%s: Cannot fix file name.\n' "$ARG" >&2 continue fi # Rename file, but ask before overwriting. mv -i "$DIR/$OLD" "$DIR/$NEW" || exit $? done |
다음과 같이 실행한다.
1 | find /path/to -depth -print0 | xargs -r0 /fix_filename .sh |
덧글을 달아 주세요