Full Stack Web Developer

Quick Reference – Bash

A collection of favorite bash commands.

It's like a local version of Command Line Fu

The find command

## List every MP3 in this folder and its subfolders.
find . -name "*.mp3"
## List all files starting with the word "Zero".
##   (-name is case sensitive)
##   (the lack of a leading wildcard means the file
##    called "Absolute Zero.docx" will not be found.)
find . -name "Zero*"
## List all files containing the word "remix".
##   (-iname is case insensitive)
##   (the wildcards both preceding and following
##    the word means any part of the word)
find . -iname "*remix*"

The grep command

## Find all instances of "_extra_headers",
##   in all files within this folder
##   and all subfolders
grep -rn "_extra_headers" .
## Show which files contain "_extra_headers".
grep -rl "_extra_headers" .

## This also works, saving keystrokes otherwise spent
##   changing "-rn" to "-rl".
grep -rn "_extra_headers" . -l
## Show all recent ssh commands.
history | grep "ssh"
## List all files containing unicode characters.
grep -rl --color='auto' -P "[\x86]" .

The rename command

## Add "Project Name" to the start of each DOCX file.
##   (The regex string says "find the first zero-length
##    character and replace it with [Project Name].)
rename -n "//Project Name - /" *.docx

Finding and killing a greedy process

## Get a list of all Firefox processes.
ps -e | grep firefox

## Kill the process with ID of [pid]
## -9 = I'm done being polite. Shut this down *now*.
su kill -9 [pid]