UNIX Tools (and their alternatives)
- Coreutils manual • GNU 📚
- bat • A
cat
clone with with syntax highlighting and Git integration 🛠️ - exa • A modern replacement for
ls
🛠️ - fd • A simple, fast and user-friendly alternative to
find
🛠️ - fzf • A command-line fuzzy finder 🛠️
- FZF Will CHANGE How You Work • Great ideas for how fzf can make common terminal workflows easier • DevOps Toolbox 📺
grep
- Everything you need to know about GREP! • 1 min video • DevOps Toolbox 📺
jobs
- Reasons to use your shell’s job control • Julia Evans 📖
- jq • A lightweight and flexible command-line JSON processor 🛠️
- The BEST CLI Tool • Examples of filtering and reshaping JSON in the terminal, in Vim, and with ChatGPT’s help • ThePrimeagen 📺
- Process JSON data from the command line with JQ • Harry Cresswell 📖
- Use npm query and jq to dig into your dependencies • Elijah Manor 📺
- Pretty Print JSON in Your Terminal with jq or Python • Nick Janetakis 📺
kill
• stop a running process by its PID- lsof • List open files 🛠️
- identify which process is running on port 5000:
lsof -i:5000
- identify the PID of the process on port 5000:
lsof -t -i:5000
- identify which process is running on port 5000:
ncdu
• list files by size 🛠️- LOW ON DISK SPACE? // TRY THIS! • DevOps Toolbox 📺
pbcopy
/pbpaste
- Copy and paste from the command line • Justin Joyce 📖
- ripgrep • ripgrep recursively searches directories for a regex pattern while respecting your gitignore 🛠️
- sed • A stream editor for filtering and transforming text 🛠️
- e.g.
sed -E s/pattern/substitution/g
- basic regex vs extended regex
- sed - An Introduction and Tutorial
- replace with sd?
- e.g.
tldr
- Command line manual pages and tldr • Justin Joyce 📖
- Command Line Cheat Sheets • Combine
tldr
withfzf
to search and preview all available cheatsheets before opening one • Elijah Manor 📺
top
- Linux Command Line playlist • The Frugal Computer Guy 📺
- Manipulating Files in Your Current Working Directory • Red Hat Enterprise Linux Step By Step Guide 📖
- 7 Essential Command Line Text Tools • grep, jq, yq, jqp, sed, bat (including as a less replacement), awk • DevOps Toolbox 📺
- These Modern Linux Tools Will BLOW YOUR MIND • DevOps Toolbox 📺
fd
=find
replacementsd
=sed
replacementexa
(noweza
) =ls
replacementdust
=du
replacementdog
=dig
replacementxh
=curl
replacementncdu
=du
replacementduf
=df
replacementbat
=cat
replacement
- Examples:
- Code With Me: Scraping Podcasts from RSS with Bash #code • Demonstrates how to use various UNIX tools to parse an RSS feed • Shane Lee 📺
Bash/Zsh Syntax
- Create and delete files and folders:
mkdir <folder-name>
- create directoryrm -rf <folder-name>
- delete directory and its contentstouch <file.ext>
- create filerm <file.ext>
- delete file
- Moving and copying files:
mv <file1.ext> <file2.ext> <target-folder>
- move individual file(s)mv * <target-folder>
- move all files in current foldermv <file.ext> <renamed.ext>
- rename filecp <file1.ext> <file2.ext> <target-folder>
- copy individual file(s)cp * <target-folder>
- copy all files in current foldercp <file.ext> <renamed.ext>
- copy and rename file
- Keyboard shortcuts:
⌃A
- jump to beginning of line⌃E
- jump to end of line⌃U
- clear from cursor position to beginning of line⌃R
- search command history (hitENTER
to run command)⌘K
- clear console
- Package website shortcuts:
npm home <repo name>
- opens repo’s homepagenpm repo <repo name>
- opens repo’s github pagenpm bugs <repo name>
- opens repo’s github issues
- The Bash String Operators | Kevin Sookocheff • Kevin Sookocheff 📖
- Single vs double quotes:
- Introduction to Bash Scripting • Bobby Iliev 📕
- Bash on Exercism • Get fluent in Bash by solving 89 exercises • Exercism 👩🎓
Environment variables
- PATH • Julia Evans 📖
Configuring Zsh
- powerlevel10k • Awesome Zsh prompt by Roman Perepelitsa 🛠️
- Lazy-load nvm to Reduce ZSH’s Startup Time • Armno Prommarak 📖
Configuring kitty
- Nerd Fonts • A collection of fonts patched with glyphs from several iconic fonts 🛠️
- Cheat Sheat | Nerd Fonts • Search all the glyphs in all the fonts 📚
- Programming Fonts - Test Drive • Test drive programming fonts online 📚
- MonoLisa • A font family designed for software developers. Font follows function. 🛠️
- how to open new windows/splits in cwd: https://sw.kovidgoyal.net/kitty/faq/#how-do-i-open-a-new-window-or-tab-with-the-same-working-directory-as-the-current-window
Searching Kitty Terminal Output with Vim
- use nvim as scrollback pager
- tl;dr
# kitty.conf
# update nvim -u path to point to your own config (if using a custom one)
scrollback_pager nvim --noplugin -u ~/.config/kitty/scrollback/init.lua -c "silent write! /tmp/kitty_scrollback_buffer | te cat /tmp/kitty_scrollback_buffer - "
# change cmd+f and cmd+o to anything you like
# search scrollback in a pager (mapped to nvim above)
# see: https://sw.kovidgoyal.net/kitty/actions/#action-show_scrollback
map cmd+f show_scrollback
map cmd+o show_last_command_output
- why use nvim to search command output?
- if you’re a vim user, you already know how to quickly jump to the beginning/end and up/down through text
- you also already know how to search and navigate back and forth through your results
- you also already know how to easily copy text
- compared to the default
less
pager or other options liketmux
’s copy mode,nvim
gives you the ability to actually alter the output by runningset modifiable
and then deleting any lines you don’t want to see - you also have full control over the configuration of your
nvim
instance to customize the appearance and behavior of your search environment however you like
- to keep this pager speedy, you can point to a simplified config that loads quickly and removes everything irrelevant if you like
- I added this to mine to allow deleting lines (to reduce noise) and starting at the bottom by default:
-- Allow deleting lines and start at the bottom
vim.api.nvim_create_autocmd('VimEnter', {
command = [[
setlocal modifiable
normal G
]]
})
- conveniently, there is a space before each prompt and generally no spaces in command output, so it’s easy to jump back and forth through prompts/commands with
shift-{}
- if you’d prefer to only inspect the output of the last command you ran, use your
show_last_command_output
shortcut instead ofshow_scrollback
- https://sw.kovidgoyal.net/kitty/conf/#shortcut-kitty.Browse-scrollback-buffer-in-pager
- https://sw.kovidgoyal.net/kitty/conf/#shortcut-kitty.Browse-output-of-the-last-shell-command-in-pager
- what’s the VS code version of this?
Filtering kitty terminal output with fzf
- tl;dr:
# kitty.conf map cmd+f launch --type=overlay --stdin-source=@screen_scrollback /bin/sh -c "/opt/homebrew/bin/fzf --no-sort --no-mouse --exact --tac | kitty +kitten clipboard"
- This command will open an
fzf
overlay that allows you to fuzzy find individual lines of output in your terminal scrollback - It works both for completed and running processes
- This command will open an
- How to set up:
- If you haven’t already, install
kitty
andfzf
(this workflow only works in kitty) - Add the
map
above to yourkitty.conf
, replacingcmd+f
with your preferred keyboard shortcut - Consider increasing the number of output lines
kitty
includes in its scrollback from2000
to something larger (e.g.10000
) by addingscrollback_lines 10000
(or whatever number you prefer) to yourkitty.conf
as well - Reload (cmd-option-, on mac) or restart kitty (i.e. just quit and reopen the app) to apply your configuration changes
- If you haven’t already, install
- How to use:
- Run a command that produces multiple lines of output (e.g.
top
) - Press your keymap, either after you’ve stopped the process or while its still running
- You should see your terminal output open inside an
fzf
overlay, where each output line represents onefzf
result - Enter a search pattern (see the
fzf
guide here) to filter the output to only includes lines that match your fuzzy - Press escape to exit fzf
- Run a command that produces multiple lines of output (e.g.
Navigating Kitty Terminal With Your Keyboard
- creating, resizing and navigating windows and tabs
- tl;dr:
# Create windows
map ctrl+\ new_window_with_cwd
# Navigate windows (i.e. splits/panes)
map ctrl+h neighboring_window left
map ctrl+l neighboring_window right
# map ctrl+j neighboring_window bottom # FIXME: interferes with menu up/down (e.g. for fzf)
# map ctrl+k neighboring_window top # FIXME: interferes with menu up/down (e.g. for fzf)
# Navigate tabs
map ctrl+] next_tab
map ctrl+[ previous_tab
# toggle zoom current window
map ctrl+m toggle_layout stack
- explain how layouts work
- unlike most terminals, most kitty layouts won’t let you decide if you want to split a window vertically or horizontally
- the new window will position itself automatically based on the active layout
- (examples)
- if you want more control over which direction your next split will occur in, you need to activate the
splits
layout
- zoom a single window by activating the
stacks
layout- add keymap to toggle the zooming
- keymaps to quickly move your cursor between windows
- I prefer the following
- watch out for conflicts with similar navigation keymaps in running terminal apps (e.g. fzf)
- how to use ctrl+j/k in both…?
- Add resize mappings too
- Add layout swap mappings too?
- Inbox:
- add maps for navigating back and forth to previous command prompts
- terminal shortcut to scroll to the beginning of the output for the last command?
- how to scroll with the keyboard (no mouse)? - Overview - kitty: https://sw.kovidgoyal.net/kitty/overview/#scrolling
- shortcuts: add same split creation + navigation ones as in tmux
- best prefix for custom maps? command-key?
- define better auto splits and manual split mappings (via the “splits” layout?): https://sw.kovidgoyal.net/kitty/layouts/
- add maps for navigating back and forth to previous command prompts
Stopping the process running on a port
- Problem:
- you try to start a process in your terminal (e.g. start a local dev server) and see an error that the port is already in use
- you check all your open terminal windows and don’t see a process to stop
- Identify what the process is like this:
lsof -i -tcp:<port>
- If that process is safe to stop, use the “PID” value from the output above or isolate that value like this:
lsof -t -i:<port>
- Then kill that process using its PID like this:
kill -9 <PID>
- To make it easier to free up a port in the future, you could also add a zsh function like this to your
.zshrc
(or equivalent terminal config file):
# kill process running on given port
k() { lsof -t -i:$1 | xargs kill -9; }
- Links:
- How to kill a process on a port on ubuntu • Stack Overflow 👨💻
- Classic SysAdmin: How to Kill a Process from the Linux Command Line • Linux Foundation 📖
- How to Terminate a Process (kill) • Oracle 📖
Switch to bash on Mac to get access to shellcheck
?
- zsh is great
- comes by default
- cool features
- oh-my-zsh
- but it’s missing a linter!
- bash has shellcheck
- the linter’s assistance while writing makes the switch worthwhile
- Upgrading Bash on macOS • Daniel Weibel 📖
- How to Change the Default Shell to Bash on macOS • Chris Hoffman & Nick Lewis 📖
Creating CLI tools
- ink • React for interactive command-line apps 🛠️
- What Makes A Good Cli Tool | Prime Explains • The Primeagen 📺
Make a file executable
- How to make a file (e.g. a .sh script) executable, so it can be run from a terminal • Ask Ubuntu 👨💻
Generate any number of test files in a directory
- e.g.
touch test-{01-10}.txt
- Creating multiple files with content from shell • Stack Overflow 👨💻
Searching command history
- Save your shell history to log files • To make it easier to find useful commands you ran awhile ago • Justin Joyce 📖
Inbox
-
explainshell.com • match command-line arguments to their help text
-
Comics • Julia Evans 📖
-
broot • A new way to see and navigate directory trees 🛠️
-
Navigating
less
pager (e.g. during agit log
): -
terminal: How To Append Current Date To Filename in Bash Shell - nixCraft -
today=$(date +"%Y-%m-%d")
-
terminal: explainshell.com - match command-line arguments to their help text - get great explanations of all options for common shell commands
-
terminal: bash: Propagate all arguments in a Bash shell script - Stack Overflow - refer to all args passed to a bash function
-
terminal: search - grep: show lines surrounding each match - Stack Overflow - add context lines to grep with -B, -A, and -C
-
terminal: How to Exclude Files and Directories When Creating a tar.gz File | Baeldung on Linux
-
terminal: tr: tr - Translate characters - IBM Documentation - translate characters in a string (e.g. edit the result of a grep search)
-
terminal: tr: linux - Removing characters from grep output - Stack Overflow
-
terminal: bash: How to Check if a File or Directory Exists in Bash | Linuxize
-
rsync: Output a List of Changed Files From rsync | Baeldung on Linux - various options for outputting more or less info about a transfer
-
bash: bash - Disable functions/aliases in a sourced script - Stack Overflow - if you have conflicting command names, they win in this descending order: alias, function, built-in, PATH executable
-
curl: curl - includes a 3 hr tutorial video
-
post: list the various version manager tools (nvm, pyenv, tfvm, etc - find the rest!)
-
symlink - How can I derefence symbolic links in bash? - Stack Overflow - follow a symbolink link with
readlink -f <path/to/symlink>
-
kitty: Overview - kitty - ctrl+shift+z to go to previous shell prompt, ctrl+shift+x for next shell prompt, ctrl+shift+g to browse last command output in less, ctrl+shift+h to browse full scrollback in less
-
5 ways to navigate the Linux terminal faster | Enable Sysadmin
-
kitty:
- kitty: make last prompt sticky at top of screen? (“context”, “sticky”)
- kitty vs tmux vs wexterm for copy mode?
-
kitty: add diff kitten? https://sw.kovidgoyal.net/kitty/kittens/diff/
-
kitty: can’t use ssh kitten inside of a tmux session (works outside of tmux); fixable?
-
kitty: how do I scroll/jump using the keyboard?
-
The Set Builtin (Bash Reference Manual) - what the various
set -[*]
options at the top of scripts mean -
bash: shell - Getting “numeric argument required” when running a script with arithmetic operations - Stack Overflow - don’t
return
values from functions,echo
them instead;echo
is the equivalent of a JSreturn
statement; in bash,return
is an int between 0 and 255 (I think representing the status of how successful the function run was) -
shellcheck: ShellCheck – shell script analysis tool - in-browser version
-
bash: linux - How do I syntax check a Bash script without running it? - Stack Overflow -
bash -n <scriptname>
to check the syntax of a script file without running it -
bash: In a bash script, using the conditional “or” in an “if” statement - Unix & Linux Stack Exchange - to use
OR
in anif
statement, wrap each condition in its own[[ ]]
and put a||
between them -
starship: Your shell prompt, ON STEROIDS // Starship - try again? I do miss the config being short and only containing lines you’ve added…
-
Assess Disk Usage from the Terminal • How to use
du
anddust
commands to view the size of a folder and its subfolders • Elijah Manor 📺 -
warp: IDE-style Warp Terminal Update • Coding in Public 📺
-
Bash for Beginners • Microsoft Developer 📺
-
More fun in the terminal with Wezterm! • Defining Wezterm’s config with lua makes it easy to dynamically compute settings (e.g. pick a random option) • Josh Medeski 📺
-
Prompt:
- Your shell prompt, ON STEROIDS // Starship • How to customize starship • DevOps Toolbox 📺
-
Using JavaScript to Write a Custom CLI • Demo of Clack • Coding in Public 📺
-
Parse Command Line Positional Arguments and Flags with Bash - How to parse arguments to a bash script - Nick Janetakis
-
Nushell - Alternative to bash/zsh that treats inputs as structured data instead of strings
-
TUI Terminal Tools - Terminal Trove - Directory of TUIs
-
ASCII art diagrams - useful ascii symbols for drawing diagrams and borders 📚
-
Yazi - terminal file manager written in rust 🛠️
-
dsherret/dax • Cross-platform shell tools for Deno and Node.js inspired by
zx
🛠️ -
matklad/xshell • Making Rust a better Bash 🛠️
-
rothgar/awesome-tuis: List of projects that provide terminal user interfaces
-
wezterm: How To Create An Epic, Simple & Fast Terminal Setup With Wezterm • Josean Martinez 📺
-
Linux commands you NEED TO KNOW fzf history • Optimize zsh history settings and use fzf to search • typecraft 📺
-
Lazygit Turns 5: Musings on Git, TUIs, and Open Source – Pursuit Of Laziness – A blog by Jesse Duffield • Some interesting opinions about how much more information TUIs can convey compared to CLIs • Jessie Duffield 📖
-
Nix in 100 Seconds • Fireship 📺