How to remove last directory from a path with sed?
sed 's,/*[^/]\+/*$,,'
If it's part of the shell script, then dirname
will be definitely more clear.
echo "/etc1/etc2/etc3/etc" | sed -e "s/\/[^\/]*$//"
produces
/etc1/etc2/etc3
Basically strip off anything at the end after the last slash that doesn't contain another slash.
you don't have to use external tools
$ a="/dir1/dir2/dir3/dir4"
$ echo ${a%/*}
you can use "dirname" shell command:
dirname /dir2/dir3/dir4