This repository has been archived on 2024-03-04. You can view files and clone it, but cannot push or open issues or pull requests.
linux/.config/bash/module/cd.sh

38 lines
724 B
Bash
Raw Normal View History

# CD (back) to directory.
# Usage: cdd <DIR>
cdd()
{
local target="${1}"
local array
local result
IFS='/' read -r -a array <<< "${PWD}"
array=("${array[@]:1}")
# Make search case-insensitive.
shopt -s nocasematch
# Find desired dir.
2023-11-26 02:31:08 +03:00
# for dir in "${array[@]}"; do
# result="${result}/${dir}"
# [[ "${dir}" =~ "${target}" ]] && break
# done
local found=1
for (( idx=${#array[@]}-2 ; idx>=0 ; idx-- )); do
dir="${array[idx]}"
[[ "${dir}" =~ "${target}" ]] && found=0
[[ ${found} = 0 ]] && result="/${dir}${result}"
done
# Clean-up???
2023-11-25 21:22:53 +03:00
shopt -u nocasematch
# Go there!
2023-11-26 02:31:08 +03:00
if [[ "${result}" != "" ]]; then
echo "${result}"
cd "${result}"
else
return 1
fi
}