#TIL #bash 찾은 파일 이름에 공백이 있어도 잘 처리하기

$ find . -type f -print0 -name '*.sh' | xargs -0 dos2unix
dos2unix: converting file ./hello world.sh to Unix format...
dos2unix: converting file ./sub_test/hello bash.sh to Unix format...

구분 문자(delimiter)를 공백이 아닌 null character로 바꾸면 된다. find -print0 옵션을 사용해 줄 바꾼 대신 null character로 파일 이름을 구분해서 출력하고 xargs -0 옵션을 사용해 null character로 구분해서 실행하게 한다.

$ find . -type f -name '*.sh' | xargs dos2unix
dos2unix: ./hello: No such file or directory
dos2unix: Skipping ./hello, not a regular file.
dos2unix: world.sh: No such file or directory
dos2unix: Skipping world.sh, not a regular file.
dos2unix: ./sub_test/hello: No such file or directory
dos2unix: Skipping ./sub_test/hello, not a regular file.
dos2unix: bash.sh: No such file or directory
dos2unix: Skipping bash.sh, not a regular file.

-print0, -0 옵션을 안 쓰면 whitespace로 구분하기 때문에 파일 이름에 공백이 있으면 실패한다.

$ find . -type f -name '*.sh'
./hello world.sh
./sub_test/hello bash.sh

테스트에 사용한 파일 목록이다.

참고 - How can I run dos2unix on an entire directory? - stackoverflow.com

Feedback plz <3 @ohyecloudy, ohyecloudy@gmail.com

A Random Post