Linux Cookbook

Examples of useful commands which can be both easy to forget for experts and hard to guess for Linux novices. Almost all of these examples should work identically on MacOS and the Windows Subsystem for Linux (WSL).

Tools such as rg, bat, fzf, ffmpeg, soffice (LibreOffice), and youtube-dl may need to be installed explicitly.

command result notes
acpi -b -t show energy-related info
  • battery-charge as a percentage
  • whether laptop is charging or not
  • internal temperature
awk '$0 != ""' ignore empty lines
awk '!c[$0]++' avoid duplicate lines
  • like sort -u without the sorting part
  • correct alternative to the misleadingly-named uniq
awk 'NR == 1 || FNR != 1' filenames… concatenate table files sharing the same header (column names) without repeating the header line after the 1st time only makes sense when given multiple filenames
awk -F '\t' -v OFS='\t' script… filenames… run AWK for tab-separated values filenames are optional
awk '{ gsub(/\x1b\[[0-9;]+m/, ""); print }' filenames… ignore ANSI-terminal escape codes, which are used for colors and styles filenames are optional
bat -A infile browse text showing all normally-invisible characters like newline, tab, space, carriage return, etc. very slow when dealing with 100+ MB data
bat -l sql infile browse text with line counts and SQL-syntax highlighting
  • very slow when dealing with 100+ MB data: when that’s the case, command less -NRS is way quicker, but lacks syntax highlighting
  • dozens of syntax-highlighters available (html, css, sql, py, js, ts, awk, json, xml, etc.)
bat infile.sql browse file with line counts and SQL-syntax highlighting file extensions act as automatic syntax-coloring hints
cal show a calendar for the current month useful to see the days of the week for the whole month
cal year find which day of the week any day from the year given was
curl -s uri quiet/silent curl shows only received data, unless there are errors
curl wttr.in get local weather forecast
  • needs internet connection
  • result is ~ 120 characters wide
  • uses unicode symbols and ANSI-terminal colors
curl wttr.in/city get weather forecast for the city given
  • needs internet connection
  • result is ~ 120 characters wide
  • uses unicode symbols and ANSI-terminal colors
curl wttr.in/city?m get weather forecast for the city given using metric units when you’re in the US
  • needs internet connection
  • result is ~ 120 characters wide
  • uses unicode symbols and ANSI-terminal colors
dos2unix -r filenames… removes
  • starting byte-order-marks (BOMs)
  • all carriage returns
from the filename(s) given: all changes to the files given are saved
makes Windows-specific plain-text data compatible with modern Windows, Linux, and Mac
ffmpeg -i infile -acodec libmp3lame outfile.mp3 convert the audio file given into another MP3-encoded file
ffmpeg -i infile -codec:a aac outfile.m4a convert the audio file given into another AAC-encoded file
ffmpeg -i song.m4a -ss 00:00:06 -to 00:03:58 -c copy cropped.m4a crop media file song.m4a from 6 seconds in to 3 minutes and 58 seconds in, leaving song.m4a the same and saving result into cropped.m4a to re-encode media data, don’t use option -c copy
ffmpeg -f concat -safe 0 -i listfile -c copy outfile.m4a if the file given after option -i
  • is a plain-text file
  • has 1 media filename per line
  • lists all your media files in the order you want
this command glues them all in order, saving the result as the other file given
  • works both for audio and video files: just change the file extensions accordingly
  • especially useful when an audiobook is split by chapter (or compact disc audio track), but you prefer it as a single file
file -i filenames… show autodetected mime-type for each filename given
find folder -maxdepth 1 -type f show full filenames only for files (and not folders) inside the folder given, without digging into any of its subfolders
find folder -type f -not -name '*.*' show full filenames only for files without extension inside the folder given
  • useful to focus on binary files and special configuration files
  • you can also use option -maxdepth 1 after the folder’s name
fzf fuzzy-find items out of plain-text data
  • approximate/case-insensitive matches by default
  • big time-saver when wading thru mountains of text
  • lists your files when not run at the end of a pipe
go build -ldflags="-s -w" build smaller-sized programs using Go
ip -4 addr show wifi0 | awk 'NR == 2 { print $2 }' | sed 's/\/[0-9]*//' show your LAN IP address
less -NRS filenames… browse files or program results
  • showing line numbers
  • with full scrolling, even horizontal
  • allowing ANSI colors/styles, if part of the input data
  • filenames are optional
  • an improvement over the default behavior of less when dealing with styled and/or wide content
openssl req -x509 -nodes rsa:4096 -keyout key.pem -out cert.pem -days 365 create SSL public/private-key files named key.pem and cert.pem which are valid for 365 days
rg options pattern ripgrep (rg) is a crazy-fast regular-expression folder-digging searcher with sensible default settings almost always preferable to grep
sed 's/^ *//' ignore spaces at the beginning of lines
sed 's/ *$//' ignore spaces at the end of lines
sed 's/ +/ /g; s/ *\t */\t/g; s/^ *//; s/ *\$//' ignore spaces
  • at the beginning of lines
  • at the end of lines
  • near tabs
and turn all runs of multiple spaces into single spaces
useful to clean/improve tab-separated data
soffice --convert-to pdf:writer_pdf_Export infile export a wordprocessor file as a PDF one you need LibreOffice or OpenOffice installed
soffice --convert-to txt infile export a wordprocessor file as a plain-text one
  • you need LibreOffice or OpenOffice installed
  • you may lose indentations
sort -k 3 sort lines using only the 3rd field in each line
sort -n sort lines, using number-ordering (instead of ASCII-ordering) you can also add the -k option from above
sort -rn sort lines in reverse order, using number-ordering you can also add the -k option from above
sort -u sort lines, then ignore duplicate lines
  • option -u stands for unique
  • alternative to the misleadingly-named uniq
tr -d '\r' ignore all carriage returns
xargs -d '\n' command options… run a command using the options given explicitly, followed by each line taken from the standard input, each of which will be added to the list of options by default xargs splits its standard input with spaces into multiple arguments: this is wrong and annoying when dealing with filenames, since these can include spaces
xclip copy text to clipboard use with files or at the end of a pipeline chain
xclip -o emit text from clipboard
youtube-dl -F youtube-uri show all formats available for the YouTube URI given, along with their numeric codes
youtube-dl -f 140 youtube-uri download audio file in AAC format (.m4a file) for the YouTube URI given AAC format seems to be always available
youtube-dl -f 251 youtube-uri download audio file in Opus (160k) format for the YouTube URI given Opus format not always available