Unix Shell
Updated: 21 April 2026
Searching
You can use grep to search in a file
1grep "hello" ./hello.txtThis also works using a pipe:
1cat hello.txt | grep helloAnd it can take a regex using the -e flag:
1cat hello.txt | grep -e `^hello`grep can also be used to search recursively through a directory using the -R flag:
1grep -R "hello" ./helloAnd using a regex
1grep -R ^helloOr 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 (|)
1echo "hello world" | grep "world"Text Manipulation
Cutting Text
You can cut some text on a per-line basis using cut. You can view the help for cut using:
1man cutCut has 3 main modes, namely:
-b which lets you specify a byte position or byte range to keep
1cat file.txt | cut -b 3 # take the third byte of each line2cat file.txt | cut -b 3-5 # take bytes 3-5 of each line3cat file.txt | cut -b 3- # take bytes 3 to end of each line4
5cat 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
1cat file.txt | cut -c 3 # take the third character of each line2cat file.txt | cut -c 3-5 # take chars 1-5 of each line3cat file.txt | cut -c 3- # take char 3 to end of each line4
5cat 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)
1cat file.txt | cut -f 1 # take the first column of each line using the default delimter2
3cat file.txt | cut -d "," -f 1 # take the first column of each line4cat file.txt | cut -d "," -f 2-4 # take chars 1-5 of each line5cat file.txt | cut -d "," -f 2- # take char 3 to end of each line6
7cat file.txt | cut -d "," -f 1,5-7,9 # any mix of the aboveProcesses
List Processes
To kill a process you can first list running processes
1lsofTo get the PID of a process you can use lsof along with grep, e.g. find a node process:
1lsof | grep nodeFind A Process on a Current Port
E.g for a processs running on port 4001
1lsof -i:4001You can also just get the port by adding -t:
1lsof -t -t:4001Kill a Process by PID
You can kill a process using it’s PID using:
1kill 1234Or with -9 to force
1kill -9 1234Kill 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
1kill $(lsof -t -i:4000)Jobs/Background Processes
Start a process, e.g. ping
1ping google.comThe 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:
1jobs2
3## which outputs4[1] suspended ping google.com5[2] - suspended ping other.com6[3] + suspended ping hello.comJobs 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:
1fg %3Navigating
You can use cd to move to specific folders relatively
1cd ../other-folder/my-folderOr even from the user home directory by prefixing with ~
1cd ~/my-stuffAnd you can use - to just swap back to the last directory
1## before, in apps/stuff, now in apps/something-else2cd -3## after, now in apps/something-elseTail/Watch a file as it changes
You can get the tail of a file and watch it as it changes using:
1tail -f ./path/to/fileCopy to Clipboard
To copy contents to the clipboard you can simlpy pipe it to pbcopy like so:
1cat hello.txt | pbcopy