[bash: basename] 디렉토리는 제외하고 파일명만 추출하기
특정 파일에 대해 작업을 할 때, 해당 파일의 디렉토리는 제외하고 파일명만 추출해야 할 필요가 있는 경우가 생기면 basename으로 간편하게 해결할 수 있다.
아래의 예시를 보자.
# /home/test/Documents/my_doc.txt에서 파일명만 추출
basename "/home/test/Documents/my_doc.txt"
my_doc.txt
만약 파일명 맨 뒤에 있는 확장자를 제거하고 싶다면 -s 옵션을 추가해 주자.
# .txt 확장자 제거
basename -s ".txt" "/home/test/Documents/my_doc.txt"
my_doc
여러개의 인자를 사용하고 싶다면 -a 옵션을 사용하면 된다.
basename -a "/home/test/Documents/my_doc.txt" "/home/test/Downloads/my_data.bin"
my_doc.txt
my_data.bin
예를 들어 디렉토리 정보가 포함된 특정 파일이 있을 때, 해당 파일을 임시 디렉토리에 복사하여 뭔가 작업을 하는 상황을 생각해 보자. 아래의 간단한 예시를 보면 금방 감이 잡힐 것이다.
# 임시 디렉토리를 지정하여 변수 tmp_dir에 저장
tmp_dir="/tmp/"
# 파일 정보를 사용자 입력으로 받아 변수 file_info에 저장
read -p "작업할 파일을 지정해 주세요: " file_info
# 임시 디렉토리에 파일 복사
cp "$file_info" "$tmp_dir"
# 파일명만 추출해서 변수 f_name에 저장
f_name=basename "$file_info"
# 복사된 파일에 대한 grep 명령을 실행, 문자열 "ABCD"가 있는지 확인
grep "ABCD" "$tmp_dir/$f_name"
✅ @pelican7, congratulations on making your first post! I gave you a $.05 vote!
Will you give me a follow? I'll follow you back in return!
Congratulations @pelican7! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here
If you no longer want to receive notifications, reply to this comment with the word
STOPCongratulations @pelican7! You received a personal award!
You can view your badges on your Steem Board and compare to others on the Steem Ranking
Vote for @Steemitboard as a witness to get one more award and increased upvotes!