Extract the last directory of a pwd output

后端 未结 4 700
难免孤独
难免孤独 2020-12-28 12:57

How do I extract the last directory of a pwd output? I don\'t want to use any knowledge of how many levels there are in the directory structure. If I wanted to use that, I c

相关标签:
4条回答
  • 2020-12-28 13:03

    Are you looking for basename or dirname?

    Something like

    basename "`pwd`"
    

    should be what you want to know.

    If you insist on using sed, you could also use

    pwd | sed 's#.*/##'
    
    0 讨论(0)
  • 2020-12-28 13:06

    Should work for you: pwd | rev | cut -f1 -d'/' - | rev

    Reference: https://stackoverflow.com/a/31728689/663058

    0 讨论(0)
  • 2020-12-28 13:17

    Using awk:

    pwd | awk -F/ '{print $NF}'
    
    0 讨论(0)
  • 2020-12-28 13:18

    If you want to do it completely within a bash script without running any external binaries, ${PWD##*/} should work.

    0 讨论(0)
提交回复
热议问题