Unix Shell
Searching
You can use grep to search in a file
grep "hello" ./hello.txt
This also works using a pipe:
cat hello.txt | grep hello
And it can take a regex using the -e flag:
cat hello.txt | grep -e `^hello`
grep can also be used to search recursively through a directory using the -R flag:
grep -R "hello" ./hello
And using a regex
grep -R ^hello
Or case inensitive with -i or for full words with -w
You can even search for something in the output of a command using a pipe (|)
echo "hello world" | grep "world"
Text Manipulation
Cutting Text (cut)
You can cut some text on a per-line basis using cut. You can view the help for cut using:
man cut
Cut has 3 main modes, namely:
-b which lets you specify a byte position or byte range to keep
cat file.txt | cut -b 3 # take the third byte of each line
cat file.txt | cut -b 3-5 # take bytes 3-5 of each line
cat file.txt | cut -b 3- # take bytes 3 to end of each line
cat file.txt | cut -b 1,5-7,9 # any mix of the above
-c which lets you specify a character position or character range to keep
cat file.txt | cut -c 3 # take the third character of each line
cat file.txt | cut -c 3-5 # take chars 1-5 of each line
cat file.txt | cut -c 3- # take char 3 to end of each line
cat file.txt | cut -c 1,5-7,9 # any mix of the above
-f which allows for splitting on a field name using -d as the delimiter (default delimiter is \t)
cat file.txt | cut -f 1 # take the first column of each line using the default delimter
cat file.txt | cut -d "," -f 1 # take the first column of each line
cat file.txt | cut -d "," -f 2-4 # take chars 1-5 of each line
cat file.txt | cut -d "," -f 2- # take char 3 to end of each line
cat file.txt | cut -d "," -f 1,5-7,9 # any mix of the above
Stream Editing (sed)
Sed enables stream editing using a specific set of commands
The s (substitute) command
The s command is the most commonoly used and runs a regex replacement and has the following structure:
sed `s/regexp/replacement/flags`
General Command Syntax
The syntax for some handy commands are:
- Substitute:
s/regexp/replacement/flags - Append text after a line:
a text - Insert text before a line:
i text - Change line to specific text:
c text - Delete until newline:
D - Print the current line:
n - Group many commands together:
{ cmd; cmd ... }
Commands can also be prefixed with a number to make them run multiple times. Multiple commands can be separated with a ; so commands can be tied together to do weird stuff like:
seq 20 | sed "n;s/./x/;"
Also useful to take a look at the Common Commands (sed, a stream editor) and the full sed commands list (sed, a stream editor)
Processes
List Processes
To kill a process you can first list running processes
lsof
To get the PID of a process you can use lsof along with grep, e.g. find a node process:
lsof | grep node
Find A Process on a Current Port
E.g for a processs running on port 4001
lsof -i:4001
You can also just get the port by adding -t:
lsof -t -t:4001
Kill a Process by PID
You can kill a process using it's PID using:
kill 1234
Or with -9 to force
kill -9 1234
Kill a Process by Port
You can kill a process that's listening on a port by first getting the PID of the process with lsof -t -i:<PORT NUMBER> and pass it into the kill command, e.g. for port 4000
kill $(lsof -t -i:4000)
Jobs/Background Processes
Start a process, e.g. ping
ping google.com
The use ctrl+z to suspend the task into the background
You can now use the terminal and start other jobs
Once jobs are running you can use jobs to view runnning jobs:
jobs
## which outputs
[1] suspended ping google.com
[2] - suspended ping other.com
[3] + suspended ping hello.com
Jobs can be resumed using fg for the most recent job, or fg %<JOB NUMBER> to resume a specific job
For example, resuming the ping hello.com can be done with:
fg %3
Navigating
You can use cd to move to specific folders relatively
cd ../other-folder/my-folder
Or even from the user home directory by prefixing with ~
cd ~/my-stuff
And you can use - to just swap back to the last directory
## before, in apps/stuff, now in apps/something-else
cd -
## after, now in apps/something-else
Tail/Watch a file as it changes
You can get the tail of a file and watch it as it changes using:
tail -f ./path/to/file
Copy to Clipboard
To copy contents to the clipboard you can simlpy pipe it to pbcopy like so:
cat hello.txt | pbcopy
Buiding Commands (xargs)
xargs basically takes in a list of strings and converts them to arguments, optionally they can be passed to another command, for example:
find **/*.go | xargs cat
It can also be used without a command to just print the "argumentified" string:
find **/*.go | xargs