Find a store in a variable the full path of a folder if exist in BASH -
i need obtain full path of folder (if exists) match specific names. there 1 folder matches name. e.g: code must find, if exists, folder these possible names:
/home/user/myfolder /home/user/myfolder_aaa /home/user/myfolder_bbb /home/user/myfolder_ccc
but it must not match other "similar" folder, like
/home/user/myfolder_xxx
and if folder exists need save in variable full path matching unwanted cases , not retry full path:
path=`ls /home/user/myfolder*`
you can use find
regex:
find ./home/user -regextype posix-extended -type f -regex '.*/myfolder(_(aaa|bbb|ccc))?$'
to store results in array (as don't appear have whitespace in these folder names):
arr=() while ifs= read -r f; arr+=( "$f" ) done < <(find /home/user -regextype posix-extended -type f -regex '.*/myfolder(_(aaa|bbb|ccc))?$' -print0) # check array contents declare -p arr
Comments
Post a Comment