How to delete the last directory with sed, for example:
echo "/dir1/dir2/dir3/dir4" | sed .....
So, I would get /dir1/dir2/dir3 .
/dir1/dir2/dir3
sed 's,/*[^/]\+/*$,,'
If this is part of a shell script, then dirname will definitely be more understandable.
dirname
you do not need to use external tools
$ a="/dir1/dir2/dir3/dir4"
$ echo ${a%/*}
echo "/ etc1 / etc2 / etc3 / etc" | sed -e "s // [^ /] * $ //"
produces
/ ETC1 / ETC2 / etc3
Basically, cross out anything at the end after the last slash that does not contain another slash.
you can use the dirname shell command:
dirname /dir2/dir3/dir4