Finding stuff – find, grep, silver searcher, ripgrep

Here’s how to find stuff in a directory:

grep -r <keyword> .

or

find . -type f -exec grep <keyword> {} \; -print

or, to exclude a directory:

grep -r --exclude-dir=<directory> <keyword> .

 

And silver searcher is pretty neat. https://github.com/ggreer/the_silver_searcher

e.g. ag <keyword> .

Respects your .gitignore!

 

Having said that, it doesn’t recognise ** patterns in your .gitignore.

https://github.com/ggreer/the_silver_searcher/issues/530

If that’s an issue you might want to try ripgrep.

https://github.com/BurntSushi/ripgrep#installation

Incredibly, ripgrep posts timings that are 5x faster than Silver Searcher.

 

Bash shortcuts

This avoids having to take your hands off the keyboard to hit the Up arrow to get the last command from the command line (Linux, Mac):

Ctrl P Ctrl J

Ctrl P gets your last command

Ctrl J hits Enter

https://unix.stackexchange.com/a/147575/198669

 

Delete to end of line: Ctrl K

Delete to beginning of line: Ctrl U

My mnemonic? Brexit – the UK leaves Europe…

https://askubuntu.com/questions/269046/bash-delete-from-cursor-till-end-of-line-with-a-keyboard-shortcut

tree – only show files with matching file name but not directories that don’t contain the file

I want to use tree to only show files that match a specific file name. E.g. filename.

The man page says:

However, I don’t want to show  directories that don’t contain that file.

Here’s how:

tree . -P filename --prune

(if you don’t have tree then: brew install tree)

and if you want to match on directories too then use:

tree . -P *match string* --matchdirs --prune

 

Making this an alias:

alias treefind="tree . --prune --matchdirs -P" so you can use it like this:

treefind some_file

 

and if you’re wanting to use a regex then:

treefind 'some_*'

https://stackoverflow.com/questions/53100998/the-p-switch-does-not-seem-to-work-for-the-tree-command/