Web Hosting Talk







View Full Version : Shell Script: Return Last Directory In A Path


mrbt
01-05-2007, 11:33 PM
I have a list of paths, e.g.

/home/user/site/examplel.com/html/journal/
/home3/oser/site/example.org/html/

What I'd like to do is just pull out and off that last directory, leaving behind


/home/user/site/examplel.com/html/
/home3/oser/site/example.org/

mrbt
01-06-2007, 02:38 PM
I just relized this should not be in Tutorials, can someone delete it, I'll post in the discussions forum, sorry, I'm a moron

hkrental
01-27-2007, 03:36 PM
What I will do:

1) use split to separate "/" character, put them in a list. [using split]
2) counter the no. of element in the list, say n
3) loop n-1 times to concentrate the list into the directory again [in a for loop]

focusinterview
06-24-2008, 04:37 AM
And I thought you posted a tutorial on SSH :(

txspaderz
06-06-2009, 01:52 AM
I know this thread is old, but it might help someone else who might be looking for info.

One way of doing is below:

Put your list of paths in a file (in this case test):

root@server[~]# cat test | cut -d "/" -f1,2,3,4,5
/home/user/site/examplel.com
/home3/oser/site/example.org
root@server[~]#