파일 이름을 숫자+알파벳만 남기기

출처 : http://www.linuxquestions.org/questions/programming-9/need-bash-script-to-remove-spaces-and-non-alpha-chars-from-folders-files-4175412667/

/fix_filename.sh

#!/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

다음과 같이 실행한다.

find /path/to -depth -print0 | xargs -r0 /fix_filename.sh
2016/04/17 23:41 2016/04/17 23:41

덧글을 달아 주세요