LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-29-2011, 12:35 PM   #1
Drigo
Member
 
Registered: May 2009
Posts: 111

Rep: Reputation: 17
Angry Get last subfolder on a path...


So lets say I create a bash script and I assign a folder variable like:


#!/bin/bash
....
Myvariable_FOLDER_PATH=/PATH/TO/SPECIFIC/SUBFOLDER


is there any way to make another variable with just the specific SUBFOLDER

(?some command?)----> will make My_variable_subfolder = SUBFOLDER

so if I do
echo $Myvariable_FOLDER_PATH

i get: /PATH/TO/SPECIFIC/SUBFOLDER


and echo $My_variable_subfolder

i get: /SUBFOLDER


The tree and path names will change so I jjust want the last subfolder that I will access to access its files.
Thanks...
 
Old 07-29-2011, 12:38 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Just use parameter substitution:
Code:
Myvariable_FOLDER_PATH=/PATH/TO/SPECIFIC/SUBFOLDER
My_variable_subfolder=${Myvariable_FOLDER_PATH/*\//}
or
Code:
My_variable_subfolder=${Myvariable_FOLDER_PATH##*/}

Last edited by colucix; 07-29-2011 at 12:44 PM.
 
Old 07-29-2011, 12:41 PM   #3
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
basename

The basename command always gives you the last component of a fully qualified path. This can be a directory or a file.

e.g. "basename /var/named/chroot/var/named" returns "named".
 
Old 07-29-2011, 03:25 PM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Even though using the basename command probably makes the script more readable, using parameter substitution is probably faster since you don't start another process.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to upload file to the subfolder nwpuhlh Linux - Newbie 2 03-21-2011 01:52 AM
Directing a domain to a subfolder kaligula1234 Linux - Server 3 11-05-2009 04:57 PM
lighttpd mod_redirect to subfolder crispyleif Linux - Server 1 04-24-2009 04:04 AM
mount a subfolder of /hda2 hypoglycemic Linux - Software 2 11-16-2004 05:27 AM
Getting mail to subfolder pembo13 Linux - Software 0 11-22-2003 05:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration