From 87c6ed7fdd4e94c366203f20bf04641627c949e9 Mon Sep 17 00:00:00 2001 From: home Date: Sat, 25 Nov 2023 21:13:55 +0300 Subject: [PATCH] cdd : go to previous directory that matches. --- .README.md | 6 ++++++ .config/bash/module/cd.sh | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 .config/bash/module/cd.sh diff --git a/.README.md b/.README.md index 380273b..aab4d11 100644 --- a/.README.md +++ b/.README.md @@ -355,6 +355,12 @@ Command|Description `bootstrap_grub`|Install grub theme. Requires root. `bootstrap_ffmpeg`|Install ffmpeg in flatpak to make `~/app/bin/ffmpeg` work. +## Cd. + +Command|Description +---|--- +`cdd `|Cd back to previous directory by name. + ## Checksum. Command|Description diff --git a/.config/bash/module/cd.sh b/.config/bash/module/cd.sh new file mode 100644 index 0000000..a48d531 --- /dev/null +++ b/.config/bash/module/cd.sh @@ -0,0 +1,28 @@ +# CD (back) to directory. +# Usage: cdd +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. + for dir in "${array[@]}"; do + result="${result}/${dir}" + [[ "${dir}" =~ "${target}" ]] && break + done + + # Clean-up??? + shopt -n nocasematch + + # Say where we're going. + echo "${result}" + + # Go there! + cd "${result}" +}