LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Get last subfolder on a path... (https://www.linuxquestions.org/questions/linux-newbie-8/get-last-subfolder-on-a-path-894506/)

Drigo 07-29-2011 12:35 PM

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...

colucix 07-29-2011 12:38 PM

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##*/}

MensaWater 07-29-2011 12:41 PM

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".

MTK358 07-29-2011 03:25 PM

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.


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