If you're a Linux user and use the command line a lot for navigating the filesystem, this might save you some typing. If you have one terminal open and you want to cd into the current working directory of another terminal that you have open, you might to do quite a bit of typing. This is especially true for very long paths such as /home/user/projects/dev/games/example/src/graphics/renderer/player/.
Here's a function that I have in my .bashrc which overrides the default behaviour of cd:
function cd {
if [ $# -eq 0 ]; then
local IFS=$'\n'
select dir in $(pgrep bash -u $(whoami) | awk '{print "/proc/"$1"/cwd"}' | xargs realpath 2> /dev/null | sort -u); do
cd $dir
break
done
else
builtin cd "$@" && ls
fi
}
With this function you can run cd without any arguments and get a numbered list of the current working directories of all running Bash shells, letting you easily select one to cd into without much typing.
#linux #bash #shell #terminal