Grab the filename in Unix out of full path

后端 未结 4 2031
离开以前
离开以前 2020-12-29 17:42

I am trying to get \"abc.txt\" out of /this/is/could/be/any/path/abc.txt using Unix command. Note that /this/is/could/be/any/path is d

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-29 18:41

    In bash:

    path=/this/is/could/be/any/path/abc.txt
    

    If your path has spaces in it, wrap it in "

    path="/this/is/could/be/any/path/a b c.txt"
    

    Then to extract the path, use the basename function

    file=$(basename "$path")
    

    or

    file=${path##*/}
    

提交回复
热议问题