Skip to content

Instantly share code, notes, and snippets.

@amerenda
Last active May 22, 2020 15:09
Show Gist options
  • Save amerenda/d7473ee4f4f8554845580e784a5cbeea to your computer and use it in GitHub Desktop.
Save amerenda/d7473ee4f4f8554845580e784a5cbeea to your computer and use it in GitHub Desktop.
Useful Bash (or zsh) oneliners
# Search for files containing a string in a directory and all subdirectories
grep -rl 'string' *
# Search for files and print out the text matching 'string' in those files
grep -r 'string' *
# Stop all docker containers running
docker ps | awk '{ print $1 }' | grep -v CONTAINER | xargs docker stop
# Remove ALL docker containers on host
docker ps -a | awk '{ print $1 }' | grep -v CONTAINER | xargs docker rm
grep
- searches for text
- '-r' means "search recursivly"
- '-l' means "display only the name of the file"
- '-v' means "do not display lines that have this string in them"
awk
- Text formatting tool
- Can do like, a million things
- '{ print $1 }' means print column number 1, where the column is seperated by a space.
- $0 means pring the entire line
xargs
- Useful command that takes input (stdin) and writes it to the end of the line
- 'docker ps | awk '{ print $1 }' | grep -v CONTAINER | xargs docker rm'
- 'docker ps' gets the running containers
- 'awk' prints the container ID (first column)
- 'grep -v' removes the top line of text
- The output is now a list of container IDs
- xargs "passes" those container IDs to 'docker rm'
- 'docker rm' will run n number of times where n is the number of lines from ''docker ps | awk '{ print $1 }' | grep -v CONTAINER '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment