Extract directory path and filename

后端 未结 7 909
遇见更好的自我
遇见更好的自我 2021-01-31 03:06

I have a variable which has the directory path, along with the file name. I want to extract the filename alone from the Unix directory path and store it in a variable.



        
7条回答
  •  旧时难觅i
    2021-01-31 03:24

    bash to get file name

    fspec="/exp/home1/abc.txt" 
    filename="${fspec##*/}"  # get filename
    dirname="${fspec%/*}" # get directory/path name
    

    other ways

    awk

    $ echo $fspec | awk -F"/" '{print $NF}'
    abc.txt
    

    sed

    $ echo $fspec | sed 's/.*\///'
    abc.txt
    

    using IFS

    $ IFS="/"
    $ set -- $fspec
    $ eval echo \${${#@}}
    abc.txt
    

提交回复
热议问题