Search code examples
sortingls

Sort directories by name's suffix


I have the following list of directories:

arquitectura-2col-layouttpl
bb-wap-portlet
bb-web-theme
bbwf2-portlet
canales-theme
columns_121_aeropuertos-layouttpl

Now I need to sort them by the name's suffix (portlet, theme or layouttpl) how can I accomplish that? The expected result would be:

arquitectura-2col-layouttpl
columns_121_aeropuertos-layouttpl
bb-wap-portlet
bbwf2-portlet
canales-theme
bb-web-theme

Thanks


Solution

  • The directories have variable number of - else you can use -t option in sort to specify delimiter...

    Here's a workaround, provided directory names do not contain spaces in them. Assumes directory names are stored in a file called ip.txt, or pipe the output of ls directly. Also, see perils of parsing ls

    $ sed 's/.*-/& /' ip.txt | sort -k2,2 | sed 's/ //'
    arquitectura-2col-layouttpl
    columns_121_aeropuertos-layouttpl
    bb-wap-portlet
    bbwf2-portlet
    bb-web-theme
    canales-theme