Fast Change Directory

While searching for comfortable ways of working with a terminal or the KDE konsole, I came across a script that allows me to quickly search my computer for a specific folder and switch to it. If there are several directories with the same name, I get an overview and can select the one I am looking for.
The source of it is https://demystifikation.wordpress.com/2021/03/03/fcd-fast-cd-version-1-1/
(Pardon, the layout options of this forum are a horrortrip for me. What you see bold is in reality a line starting with #) The code is

#!/bin/bash
# fcd.sh:
#
# Fast change directory. Should be sourced from ~/.bashrc file, since it is and
# needs to be a function.
#
#	Update Ver.1.1, 2021-03-03
#		Bugfix for missing results when fast changing to "bin", thanks to m_bostaurus.
#	Update Ver.1.0, 2021-02-07
#		changed from egrep to mapfile, see code. Handles blanks in paths now flawlessly.
#   Update Ver.0.9, 2013-08-21, comments, publication
#
#   Prototyp 2010
#
# This program depends on locate/updatedb, which aren't always installed/activated.
# If you use the shell on a regular basis, you should have them installed, though.
#
#	(c) GPLv3 (2010, 2021)
#
fcd ()
{
	suchdirname=$1
	# list=$(locate $1 | egrep "/$1$"); # Version 1
    # update 12.02.2021
    # mapfile -d '' list < <(locate -b -0 -r "$suchdirname$")
    # update 19.02.2021
    mapfile -d '' list < <(locate -b -0 -r "^$suchdirname$")
    # count=$(echo $list | wc -w );
    # update 19.02.2021
    count=${#list[@]}
    case $count in
        0)
            echo "unknown directory: "$1 && return
            # could search for partial matches Doc => Documentation
        ;
        1)
            if [[ -d "${list[0]}" ]]; then
                echo "1) match: $list";
                cd "$list";
            else
                echo "not a directory: $1";
            fi
        ;
        *)
            select directory in "${list[@]}" "/exit/";
            do
                if [[ "$directory" = "/exit/" ]]; then
                    break;
                fi;
                if [[ -d "$directory" ]]; then
                    echo "multi) $directory";
                    cd "$directory";
                    break;
                else
                    echo "not a directory: "$1;
                fi;
            done
        ;
    esac
}

To set it up, I went through the following steps:

a) I wrote the code in a file named fcd.sh in ~/.local/bin/ and made it executable.
b) In the file ~/.bashrc I added the line source /location/bin/fcd.sh. The change to this file will only take effect after the user logs off and logs on.
c) I installed m-locate sudo pacman -S m-locate
d) I changed the file /etc/updatedb.conf created by the installation; it contains a line starting with PRUNEPATH. It specifies where not to search for the database creation. In it is regularly written e.g. /media or also /mnt. After deleting this specification, saving and a following sudo updatedb, folders are reliably found at these locations.
On p. 47 in LinuxWelt 1/2021 I found a hint for those who also want to scan USB drives. In the mentioned file there is also a line starting with PRUNEFS. In this one you have to delete usbfs. After that USB drives will also be scanned for database creation.
My thanks go to user unknown from https://demystifikation.wordpress.com :clap:
This hint is published by me on the following forums: https://forum.manjaro.org/ (english), https://www.manjaro-forum.de/ and https://de.manjaro.org/.

Translated with www.DeepL.com/Translator (free version)
@admin If you can better my layout, please do!

1 Like

A post was merged into an existing topic: Favorite Lesser-Known Commands?

A post was merged into an existing topic: Favorite Lesser-Known Commands?