I would like to do this in Bash:
I've been playing around with the find command, but a little confused. Any suggestions?
In order to check if a directory exists in Bash, you have to use the “-d” option and specify the directory name to be checked.
To access the first character of a string, we can use the (substring) parameter expansion syntax ${str:position:length} in the Bash shell. position: The starting position of a string extraction.
Finding text strings within files using grep-r – Recursive search. -R – Read all files under each directory, recursively. Follow all symbolic links, unlike -r grep option. -n – Display line number of each matched line.
You can use the -quit
option of find
:
find <dir> -maxdepth 1 -type d -name '*foo*' -print -quit
pattern="foo" for _dir in *"${pattern}"*; do [ -d "${_dir}" ] && dir="${_dir}" && break done echo "${dir}"
This is better than the other shell solution provided because
${dir}
will be empty)=~
operator (if you need this depends on your pattern)find
)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With