Cutting all the characters after the last /

If you're cutting off the ends of the strings,dirname might fit the bill:

$ dirname xxxx/x/xx/xx/xxxx/x/yyyyy
xxxx/x/xx/xx/xxxx/x
$ _

If you're trying to isolate the last part of the string, use echo /$(basename "$str").

$ str=xxxx/x/xx/xx/xxxx/x/yyyyy
$ echo /$(basename "$str")
/yyyyy
$ _

If you want to get the "cutted part"

yyy
yyyyy

You can use

sed 's|.*/||' 

eg.

echo "xxx/xxxx/xxxxx/yyy" | sed 's|.*/||'
echo "xxxx/x/xx/xx/xxxx/x/yyyyy" | sed 's|.*\/||'

output

yyy
yyyyy

(Note: This uses the ability of sed to use a separator other than /, in this case |, in the s command)


If you want to get the begining of the string :

xxx/xxxx/xxxxx
xxxx/x/xx/xx/xxxx/x

You can use

sed 's|\(.*\)/.*|\1|'

eg.

echo "xxx/xxxx/xxxxx/yyy" | sed 's|\(.*\)/.*|\1|'
echo "xxxx/x/xx/xx/xxxx/x/yyyyy" | sed 's|\(.*\)/.*|\1|'

output

xxx/xxxx/xxxxx
xxxx/x/xx/xx/xxxx/x

Parameter expansion in bash

You can use parameter expansion in bash, in this case

  • ${parameter%word} where word is /*
  • ${parameter##word} where word is */

Examples:

Remove the last part

$ asdf="xxx/xxxx/xxxxx/yyy"
$ echo ${asdf%/*}
xxx/xxxx/xxxxx

This is described in man bash:

${parameter%word}
${parameter%%word}
      Remove matching suffix pattern.  The word is expanded to produce
      a pattern just as in pathname expansion.  If the pattern matches
      a  trailing portion of the expanded value of parameter, then the
      result of the expansion is the expanded value of parameter  with
      the  shortest  matching  pattern (the ``%'' case) or the longest
      matching pattern (the ``%%'' case) deleted.  If parameter  is  @
      or  *,  the  pattern  removal operation is applied to each posi‐
      tional parameter in turn, and the  expansion  is  the  resultant
      list.   If  parameter is an array variable subscripted with @ or
      *, the pattern removal operation is applied to  each  member  of
      the array in turn, and the expansion is the resultant list.

Remove all except the last part

$ asdf="xxx/xxxx/xxxxx/yyy"
$ echo ${asdf##*/}
yyy

You can add a slash like so

$ echo /${asdf##*/}
/yyy

to get exactly what you wanted at one particular instance according to the edited question. But the question has been edited by several people after that and it is not easy to know what you want now.

This is described in man bash:

${parameter#word}
${parameter##word}
      Remove matching prefix pattern.  The word is expanded to produce
      a pattern just as in pathname expansion.  If the pattern matches
      the  beginning of the value of parameter, then the result of the
      expansion is the expanded value of parameter with  the  shortest
      matching  pattern  (the ``#'' case) or the longest matching pat‐
      tern (the ``##'' case) deleted.  If parameter is  @  or  *,  the
      pattern  removal operation is applied to each positional parame‐
      ter in turn, and the expansion is the resultant list.  If param‐
      eter  is  an array variable subscripted with @ or *, the pattern
      removal operation is applied to each  member  of  the  array  in
      turn, and the expansion is the resultant list.