Skip to content

Instantly share code, notes, and snippets.

@1kko
Last active May 14, 2024 09:58
Show Gist options
  • Save 1kko/c1ef1afc7cbfb8712146bbe3a767d395 to your computer and use it in GitHub Desktop.
Save 1kko/c1ef1afc7cbfb8712146bbe3a767d395 to your computer and use it in GitHub Desktop.
Bash - performs search of specific directory up to 2 depth and change directory into it
# This script performs search of specific directory up to 2 depth and change directory into it.
# This is helpful for cases like having many repositories and need to cd into it.
# Install
# 1. Install fzf(https://github.com/junegunn/fzf#related-projects). eg: sudo apt install fzf (
# 2. Copy this source and paste into paste into your: nano ~/.bashrc
# 3. Edit the srchDir variable for your environment
# 4. Save and start new session of terminal
# Usage
# $ repo term1 term2
# Script
# Copy from bottom line to end and paste in the end of ~/.bashrc
repo() {
# This will list up to 2 depth of given directory. Add/remove '*/' to your preferences.
# The path should be absolute path.
# Note that too many depth will slow-down the script, less depth will show fewer results.
local srchDir="/home/your/repo/*/*/"
# if any arguments arn't given, just list up everything else use search query
[ "$1" = false ] && cd "$(ls -d $srchDir | fzf)" || local qTerm="${@}"; cd "$(ls -d $srchDir | fzf -q "$qTerm")"
}
@1kko
Copy link
Author

1kko commented Nov 18, 2021

This is how it works
repo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment