Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove last directory from a path with sed?

Tags:

shell

path

sed

How to remove the last dir with sed (not dirname), like this:

echo "/dir1/dir2/dir3/dir4" | sed .....

So I would get /dir1/dir2/dir3.

like image 416
yael Avatar asked Jul 04 '10 13:07

yael


People also ask

How do I remove part of a path?

Click on the path where you want the gap to start and again where you want it to stop. Then use the Direct Selection tool (A) to select the segment, and Delete to remove it. You could also use the Shape Builder Tool. First draw a shape like a circle over the path overlapping the segment you'd like removed.

How do I remove a path from a directory in Linux?

There are two Linux commands you can use to remove a directory from the terminal window or command line: The rm command removes complete directories, including subdirectories and files. The rmdir command removes empty directories.


1 Answers

you don't have to use external tools

$ a="/dir1/dir2/dir3/dir4"

$ echo ${a%/*}

like image 74
ghostdog74 Avatar answered Nov 08 '22 22:11

ghostdog74