PDA

View Full Version : [SOLVED] Extract folder name from folder path in BASh



jamesisin
April 1st, 2011, 11:09 PM
I am writing a script for bash wherein I need to use the containing folder name from a path:


/path/to/folder
extract "folder"

I did something similar when I needed to extract the folder path from a file path using this bit of code:


for (( i=0 ; i < ${#albumfind[@]} ; i++ )) ; do
# path is cue iteration less file name
albumfolder="${cuefind[i]%/*.*}"

(Where albumfind and cuefind are arrays containing paths.)

This problem is a little different and I'm a little stumped (read: stupid) today.

MadCow108
April 1st, 2011, 11:30 PM
$ basename /path/to/folder
folder
$ dirname /path/to/folder
/path/to
$ fold=`basename /path/to/folder`
$ echo $fold
folder

jamesisin
April 2nd, 2011, 12:36 AM
Hmmm... I was making this really hard. Thanks.

jamesisin
April 2nd, 2011, 06:47 AM
Recursive Prepending Script

http://soundunreason.com/inkwell/index.php/2011/04/recursive-prepending-script/

... for what it's worth.