LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 01:35 PM   #1
Drigo
Member
 
Registered: Jun 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, 01:38 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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 01:44 PM.
 
Old 07-29-2011, 01:41 PM   #3
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,580
Blog Entries: 14

Rep: Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969
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, 04:25 PM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 02:52 AM
Directing a domain to a subfolder kaligula1234 Linux - Server 3 11-05-2009 05:57 PM
lighttpd mod_redirect to subfolder crispyleif Linux - Server 1 04-24-2009 05:04 AM
mount a subfolder of /hda2 hypoglycemic Linux - Software 2 11-16-2004 06:27 AM
Getting mail to subfolder pembo13 Linux - Software 0 11-22-2003 06:38 AM


All times are GMT -5. The time now is 04:59 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration