Extract directory path
Use sed
sed (stream editor) is a Unix utility that parses and transforms text
| |
About sed
sed (stream editor) is a Unix utility that parses and transforms text, using a simple, compact programming language. sed was developed from 1973 to 1974 by Lee E. McMahon of Bell Labs,[1] and is available today for most operating systems.[2] sed was based on the scripting features of the interactive editor ed (“editor”, 1971) and the earlier qed (“quick editor”, 1965?66). sed was one of the earliest tools to support regular expressions, and remains in use for text processing, most notably with the substitution command. Popular alternative tools for plaintext string manipulation and “stream editing” include AWK and Perl.
Extract file name
Use sed
| |
Use basename
Use the basename command to extract the filename from the path
| |
About basename
basename is a standard computer program on Unix and Unix-like operating systems. When basename is given a pathname, it will delete any prefix up to the last slash (’/’) character and return the result. basename is described in the Single UNIX Specification and is primarily used in shell scripts.
Usage
The Single UNIX Specification specification for basename is.
| |
- string : A pathname
- suffix : If specified, basename will also delete the suffix.
Examples
basename will retrieve the last name from a pathname ignoring any trailing slashes
| |
basename can also be used to remove the end of the base name, but not the complete base name
| |
![Featured image of post [Shell] Extract directory path and file name from file path](/post/2019-02-13-extrac-directory-path-and-file-name/tmp_wordcloud_hu_92a39963e876a997.png)
![[Shell] 명령어의 입력과 출력을 다루는 방법(I/O Redirection)](/post/2024-09-10-shell-redirection/tmp_wordcloud_hu_ab0cff18f0685a23.png)
![[Shell] 서브 프로세스의 실행 종료를 대기하는 wait 커맨드](/post/2021-08-05-shell-wait/tmp_wordcloud_hu_335bbf237d3c0591.png)
![[Shell] 파일 이름에서 경로와 확장자를 추출하는 법](/post/2021-07-09-get-file-name-in-shell/tmp_wordcloud_hu_f5cd3c15afd48af0.png)
![[Shell] 날짜로 정렬해서 파일 목록 뽑기](/post/2020-05-12-listing-files-in-order/tmp_wordcloud_hu_e9fca0ad3b863191.png)
![[Shell] 명령어 반복 실행 (watch, while)](/post/2022-06-27-linux-command-loop-execute/tmp_wordcloud_hu_26ed660c21c5ba5d.png)