Skip to content

Instantly share code, notes, and snippets.

@gelbermann
Last active January 17, 2023 10:50
Show Gist options
  • Save gelbermann/05080bbde20c96ece43e50b58b9e36b3 to your computer and use it in GitHub Desktop.
Save gelbermann/05080bbde20c96ece43e50b58b9e36b3 to your computer and use it in GitHub Desktop.
extended-locate.sh
#!/bin/bash
### Requirements:
### - exa
### - ripgrep
### - column
### - updated db for `locate` command (best set up with a cron task)
function main() {
locations=( $(locate $1) )
for line in ${locations[@]}; do
print_line $line
done
}
## set up env
OLD_IFS=$IFS
IFS=$'\n'
curr_dir=""
new_directory_seperator=$'\n'
## ==== functions ====
function print_line() {
location=$1
[ -d $location ] && print_dir $location
[ -f $location ] && print_file $location
}
function print_dir() {
dir_path=$1
curr_dir=$1
# NOTE next part might need performance tuning
printf "$new_directory_seperator"
exa --long --only-dirs --all --classify --icons --color=always --time-style=long-iso $dir_path/../$(basename $dir_path) | awk -F ' ' -v var=$dir_path '{$7=var; print}'
}
function print_file() {
file_path=$1
file_dir=$( dirname $file_path )
if [[ "$curr_dir" == "" ]]; then
curr_dir=$file_dir
fi
if [[ "$curr_dir" != "$file_dir" ]]; then
curr_dir=$file_dir
printf "$new_directory_seperator"
fi
exa --long --all --classify --icons --color=always --time-style=long-iso $file_path
}
## ==================
## ==== execute =====
# main "$@" | column -te # TODO add this option as a command line argument. In the meantime, use it in an alias.
main "$@"
## ==================
## restore env
IFS=$OLD_IFS
@gelbermann
Copy link
Author

If you want the most barebones version of this, you can replace the dependencies like so:

  • rggrep
  • exa ...ls -l

I don't know about any alternative for unbuffer, but you can remove it (and lose the color-coded results)
The same goes for column

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