简体   繁体   中英

How to get only end base file name in a directory structure using shell script(bash) or Linux command

I have directory structure like this

  • MainDIR/DIr2/b.txt

  • MainDIR/DIr1/a.txt

I want to get only base file names, like a.txt and b.txt

To get the basename of a file path, which is stored in a shell variable MYFILE , you can do it like this:

In Zsh it is easiest, because you don't need to create a separate process:

$MYFILE:t

in bash (and, I think, ksh), the only method I know is

$(basename $MYFILE)

This works in Zsh too, but has the drawback that a sub-process is needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM