As this question is tagged bash
Introduction
I was searching for window's registry file after mounted NTFS partition in a temporary mount point mnt=$(mktemp -d)
Then, if the command:
regfile=$(find $mnt -iwholename "$mnt/window*/system32/config/software")
do the job, as this command is based on $mnt, find
will stupidely scan the whole filesystem and take a lot of time.
My pure bash function
For this, I wrote this ipath
function:
ipath() {
if [[ $1 == -v ]]; then local -n _res=$2; shift 2; else local _res; fi
local _path _i _chr
_res=
for ((_i=0;_i<${#1};_i++));do
_chr=${1:_i:1} _chr=${_chr,}
[[ $_chr != ${_chr^} ]] && _path+="[$_chr${_chr^}]" ||
_path+=$_chr
done
_path=($_path)
[[ -e ${_path[0]} ]] || return 1
if [[ ${_res@A} == _res=* ]] ;then
printf "%s\n" "${_path[@]}"
else
_res=("${_path[@]}")
fi
}
Then for testing:
$ ipath "$mnt/window*/system32/config/software"
/tmp/tmp.Q2czvPDs5m/Windows/System32/config/SOFTWARE
$ ipath ~/desktop
/home/user/Desktop
for use as a variable:
$ ipath -v regfile "$mnt/window*/system32/config/software"
$ echo $regfile
/tmp/tmp.Q2czvPDs5m/Windows/System32/config/SOFTWARE
$ ipath -v DeskDir ~/desktop
echo $DeskDir
/home/user/Desktop
Another test with multiple answer and spaced path:
$ ipath $mnt/program\*/int\*er
/tmp/tmp.jVs5mPGbJm/Program Files/Internet Explorer
/tmp/tmp.jVs5mPGbJm/Program Files (x86)/Internet Explorer
$ ipath -v iedirs "$mnt/program*/int*er"
$ declare -p iedirs
declare -a iedirs=([0]="/tmp/tmp.jVs5mPGbJm/Program Files/Internet Explorer" [1]="/tmp/tmp.jVs5mPGbJm/Program Files (x86)/Internet Explorer")
Note, tested with unicode accented characters: résumé.pdf
become [rR][éÉ][sS][uU][mM][éÉ].[pP][dD][fF]
.