dir is indeed superfluous, i thought i will need it, but then i didn't and forgot to remove it
I am curious as to what you meant by transforming paths.
I mean string operations on paths to get a rock solid transformation from src_path to dest_path so you can perform the desired operation in an unambiguous way, not caring about your current position in the dir tree. Why jump (which can fail BTW and then you are not where you think you are) if you can simply calculate destination path?
For example cd-ing up and down becomes a massive PITA in case you are trying to do something like "find all files in X subtree and then do something with them exporting them to Y in a way that mirrors the initial layout" because you now have to track how deep you are.
But if your problem can be described as "given paths like these
/A/B/C/some/stuff.txt
/A/B/C/test1.txt
/A/B/C/1/2/3/4/5/music.mp3
replace /A/B/C with /D/E/F while preserving the structure" you can easily do something like
Code:
src_dir=/A/B/C
dest_dir=/D/E/F
f=/A/B/C/1/2/3/4/5/music.mp3
echo "${f/#$src_dir/$dest_dir}" # replace $src with $dest but only if it starts the string
/D/E/F/1/2/3/4/5/music.mp3
shopt -s globstar
for f in "$src_dir"/**/*.* # **=any depth, requires shopt -s globstar
do
dest_f=${f/#$src_dir/$dest_dir}
done
# or with find
while read f
do
dest_f=${f/#$src_dir/$dest_dir}
done < <(find "$src_dir" -type f)
(note that in case of while read used to read lists of files, null delimiter should be used because reasons, while read -rd $'\0' f + find -print0)
Bookmarks