Useful bash tips and tricks

Updated on: Thu Mar 24 2022

Hey! Bash tips which I’m using daily or once in my life but seems interesting to me all gathered here. All recipes here are for zsh shell.

To kill processes bounded to port and proxied from it you can use command like this for example:

copied bash

lsof -Pi :1234 | awk 'NR>1 { print $2 }' | xargs kill -9

If you are using zsh than it is ok to create new function in your .zshrc file.

copied bash

function killonport() {
  lsof -Pi :$1 | awk 'NR>1 { print $2 }' | xargs kill -9
}

And than we can use it.

copied bash

killonport 1234

To make a kinda tail -f but to track some command output we can use watch util:

copied bash

watch 'docker ps | grep tags'

To find directory by name:

copied bash

find . -type d -name "src" -print

To find and delete files recursively in some dir:

copied bash

find . -name "filename.tmp" -type f | xargs rm

To find and delete directories -type d should be specified. For example I used following command to delete all node_modules directories in one of mine monorepos:

copied bash

find . -name "node_modules" -type d -maxdepth 3 | xargs rm -rf