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 see energy-related info
  • battery-charge as a percentage
  • whether laptop is charging or not
  • internal temperature
awk '$0 != ""' ignore empty lines you can follow it with a list of filenames
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' concatenate table files sharing the same header (column names) without repeating the header line after the 1st time useful when followed by several filenames
awk -F '\t' -v OFS='\t' run AWK for tab-separated values follow this with an AWK command/script
awk '{ gsub(/\x1b\[[0-9;]+m/, ""); print }' ignore ANSI-terminal escape codes, which are used for colors and styles you can follow it with a list of filenames
bat -A browse text showing all normally-invisible characters like newline, tab, space, carriage return, etc. slow when dealing with 100+ MB data
bat -l sql browse text with line counts and SQL-syntax highlighting
  • slow when dealing with 100+ MB data: when that’s the case, command less -NRS is way more efficient, but lacks syntax highlighting
  • dozens of syntax-highlighters available (html, css, sql, py, js, awk, json, xml, etc.)
bat data.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 1995 find which day of the week any day from 1995 was
curl -s 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/delhi get weather forecast for Delhi same as above
curl wttr.in/delhi?m get weather forecast for Delhi using metric units when you’re in the US same as above
dos2unix -r 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 result.mp3 convert the audio file infile into the MP3-encoded result.mp3
ffmpeg -i infile -codec:a aac result.m4a convert the audio file infile into the AAC-encoded result.m4a
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 filelist.txt -c copy result.m4a if filelist.txt
  • 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 into result.m4a
  • 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 show autodetected mime-type for each filename given
find some-folder -maxdepth 1 -type f show full filenames only for files (and not folders) inside the folder named some-folder, without digging into any of its subfolders
find some-folder -type f -not -name '*.*' show full filenames only for files without extension inside the folder named some-folder
  • 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 piped data
  • approximate/case-insensitive matches by default
  • big time-saver when wading thru mountains of text data
  • searches 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 browse files or program results
  • showing line numbers
  • with full scrolling, even horizontal
  • allowing ANSI colors/styles, if part of the input data
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 crazy-fast regular-expression folder-digging searcher 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 export a wordprocessor file as a PDF one
  • needs a filename to convert from
  • you need LibreOffice or OpenOffice installed
  • result is saved in the current working folder as a .pdf file with the rest of the original filename
soffice --convert-to txt export a wordprocessor file as a plain-text one
  • needs a filename to convert from
  • you need LibreOffice or OpenOffice installed
  • you’ll lose all formatting
  • you may lose indentations and other fine adjustments
  • result is saved in the current working folder as a .txt file with the rest of the original filename
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' 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 show all formats available for the YouTube URI given, along with their numeric codes
youtube-dl -f 140 download audio file in AAC format (.m4a file) for the YouTube URI given seems to be always available
youtube-dl -f 251 download audio file in Opus (160k) format for the YouTube URI given not always available