hosted by liquidweb

Go Back   Web Hosting Talk : Web Hosting Main Forums : Web Hosting Talk Tutorials : Programming Tutorials : Shell Script: Return Last Directory In A Path

Forum Jump

Shell Script: Return Last Directory In A Path

Reply Post New Thread In Programming Tutorials Subscription
Send news tip View All Posts Thread Tools Search this Thread Display Modes
Old 01-05-2007, 11:33 PM
mrbt mrbt is offline
Junior Guru Wannabe
Join Date: Jun 2006
Posts: 87

Shell Script: Return Last Directory In A Path

I have a list of paths, e.g.


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


Sponsored Links
Old 01-06-2007, 02:38 PM
mrbt mrbt is offline
Junior Guru Wannabe
Join Date: Jun 2006
Posts: 87
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

Old 01-27-2007, 03:36 PM
hkrental hkrental is offline
Join Date: Sep 2006
Location: Hong Kong
Posts: 17
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]

Server Rental and Co-location in Hong Kong
All Questions or Suggestions are Welcome.
99% Uptime Guarantee.

Sponsored Links
Old 06-24-2008, 04:37 AM
focusinterview focusinterview is offline
Join Date: Jun 2008
Location: India
Posts: 12
And I thought you posted a tutorial on SSH

Old 06-06-2009, 01:52 AM
txspaderz txspaderz is offline
Junior Guru Wannabe
Join Date: May 2008
Location: Houston
Posts: 43

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


Related posts from
Title Type Date Posted
Ads Available for the Second Printing of WHIR’s Annual Hottest Hosts Directory Blog 2014-04-08 16:52:36
Linux Malware Operation Windigo Infects 25,000 Web Servers Web Hosting News 2014-03-19 11:44:53
Hostway Corporation Listing 2014-04-17 19:32:40
Newtek Web Hosting Listing 2014-04-17 19:32:29
OpenStack and the AWS API Debate Web Hosting News 2013-07-30 14:21:54

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Log in with your username and password

Forgot Password?
Web Hosting News:



Welcome to

Create your username to jump into the discussion! is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.

(4 digit year)

Already a member?