A small interactive file selector for Bash using fzf.
It lets you browse directories, open files with your preferred editor, and visually distinguish folders from files using colors.
/always shown first to jump to the filesystem root..always shown second to go up one directory- Remaining files and directories sorted alphabetically
- Directories shown in color
- Files opened with
$EDITOR - Optional starting directory as argument
- Lightweight and easy to modify
- Bash
fzffindsort
Clone the repository or copy the script locally:
chmod +x fsel.shMake sure fzf is installed.
sudo apt install fzfRun it with:
./fsel.shOr start from a specific directory:
./fsel.sh /path/to/start- Selecting
/moves to the root directory - Selecting
..goes to the parent directory - Selecting a directory enters it
- Selecting a file opens it with
$EDITOR
Example:
EDITOR=micro ./fsel.shIf EDITOR is not set, the script uses nano by default.
This is a standalone Bash script, not a shell function.
Because of that, when you change directory inside the script, the directory change only affects the script itself, not the parent shell.
So this works:
- browse directories inside the selector
- open files from the selected location
But this does not persist after the script exits:
- changing the current directory of your terminal session
If you want persistent cd behavior, use a shell function instead of a normal executable script.
#!/usr/bin/env bash
set -euo pipefail
EDITOR_CMD="${EDITOR:-nano}"
if [[ $# -gt 0 ]]; then
cd -- "$1" || exit 1
fi
while true; do
row="$(
{
printf '\033[1;35m/\033[0m\t/\n'
printf '\033[1;35m..\033[0m\t..\n'
find . -maxdepth 1 -mindepth 1 -printf '%P\n' | sort | while IFS= read -r item; do
if [[ -d "$item" ]]; then
printf '\033[1;34m%s/\033[0m\t%s\n' "$item" "$item"
else
printf '%s\t%s\n' "$item" "$item"
fi
done
} | fzf --ansi --delimiter=$'\t' --with-nth=1 --height 40% --reverse --prompt="$(pwd) > "
)"
[[ -n "$row" ]] || exit 0
sel="${row#*$'\t'}"
case "$sel" in
/)
cd / || exit 1
;;
..)
cd .. || exit 1
;;
*)
if [[ -d "$sel" ]]; then
cd -- "$sel" || exit 1
else
"$EDITOR_CMD" "$sel"
exit 0
fi
;;
esac
doneYou can easily extend it to:
- color executable files differently
- color symbolic links differently
- preview files with
fzf --preview - show directories before files
- make sorting case-insensitive
MIT