Getting relative paths in BASH Getting relative paths in BASH bash bash

Getting relative paths in BASH


If your Cuesheets dir is always in the same directory as your Music, you can just remove root_dir from the path, and what is left is the relative path. If you have the path to your album.tta in album_path (album_path=/Volumes/External/Music/Just/Some/Dirs/Album.tta) and your root_dir set(root_dir=/Volumes/External), just do ${album_path#$root_dir}. This trims root_dir from the front of album_path, so you are left with album_path=Just/Some/Dirs/Album.tta.

See bash docs for more information on bash string manipulation

EDIT:// Changed ${$album_path#$root_dir} to ${album_path#$root_dir}


Okay so I've tackled this a couple of ways in the past. I don't recommend screwing with paths and pwd environment variables, I've seen some catastrophic events because of it.

Here's what I would do

CURRENTDIR=/Volumes/External/Music # make sure you check the existence in your script...SEDVAL=$(echo $CURRENTDIR | sed s/'\/'/'\\\/'/g)#run your loops for iterating through filesfor a in $(find ./ -name \*ogg); do  FILE=`echo $a | sed s/$SEDVAL/./g`  # strip the initial directory and replace it with .  convert_file $FILE # whatever action to be performeddone

If this is something you might do frequently I would actually just write a separate script just for this.