Search code examples
shellgrepcut

Getting last folder from a directory using grep or cut


i just want to have the last word I have tried reverse too still I am not able to get the last word.

../init.d/halt
../init.d/killall
../init.d/watchdog
../init.d/iptables
../init.d/network
../init.d/evlogrmt
../init.d/evlog
../init.d/rdisc
../init.d/syslog-ng
../init.d/portmap

Solution

  • As I don't think it is possible to do a one-liner with cut, let's propose a grep solution:

    $ grep -oE '[^/]+$' file
    halt
    killall
    watchdog
    iptables
    network
    evlogrmt
    evlog
    rdisc
    syslog-ng
    portmap
    

    Of course, with awk it is pretty simple: awk -F/ '{print $NF}' file.