less than 1 minute read

bash shell script 에서 file path 에서 확장자나 Path 를 제거하고 파일명만 뽑아내는 방법(bash shell 에서만 동작함)

Path 제거하고 file 명만 추출

1
2
3
$ s=/the/path/foo.txt
$ echo ${s##*/}
foo.txt

파일명에서 확장자 제거(확장자가 .txt 에만 동작함. “basename foo.txt .txt” 와 동일)

1
2
3
$ s=foo.txt
$ echo ${s%.txt}
foo

. 뒤에 붙은 임의의 확장자 제거

1
2
3
$ s=foo.txt
$ echo ${s%.*}
foo

Tags: , , , ,

Categories:

Source File: 2021-07-09-get-file-name-in-shell.md

Updated:

Comments