Terminal recipes

100 short tasks that chain commands, most of them the terminal questions with the most views on Stack Overflow. Linux first, with macOS in the notes. Every step was checked by hand.

filter

categories

add a folder to the path

Let the shell find programs in a folder, for good, and reload the settings without logging out.

add a user

Create an account with a home folder and a password, and let it use sudo if it should.

add to the end of a file

Add lines to a file without replacing it, also to a file only root can write.

add up numbers in a file

Sum a column of numbers, from a file or a command's output.

argument list too long

"Argument list too long" from rm, cp or mv: too many files for one command line. Hand them over in batches.

ask for input in a script

Ask yes or no, read a password without showing it, or wait for an answer with a time limit.

back up and restore postgresql

Run a .sql file, dump a PostgreSQL database and load it back, from the terminal.

change permissions of a folder and everything in it

Give a folder, its subfolders and its files the permissions or the owner they need, without making them open to everyone.

check a certificate

See a site's TLS certificate, when it expires and who issued it, or the same for a .pem file.

check a file's permissions

See who may read, write and run a file or a folder, and why a path is refused.

check if a file or folder exists

Test in a script whether a path exists, is a file, a folder, empty or runnable.

check if a program is installed

Find whether a command exists, where it is, and which package it came from.

check if a string contains a text

Test in a script whether a string contains, starts with or matches something.

command not found after installing

You installed a program but the shell says "command not found": find where it went and let the shell see it.

compare two folders

Find which files differ between two folders, or are only in one of them.

compress a folder

Pack a folder into one compressed file, look inside it, and unpack it again.

copy a folder

Copy a folder with everything inside it, keeping its dates and permissions if needed.

copy files to and from a server

Copy a file or a whole folder between this machine and another one over ssh.

copy to the clipboard from the terminal

Send a command's output or a file to the clipboard, and paste from it.

copy without some folders

Copy a folder but leave out some of what is inside, like node_modules or .git.

copy, overwriting or not

Copy without being asked each time, or without ever replacing a file that is there.

count lines of code

How many lines of code a project has, in total and by language.

count lines, words and files

Count the lines of a file or of a command's output, the words, and the files in a folder.

count the processors

How many processors and cores this machine has, and what they are.

create a large file

Make a file of a given size, for testing disks, uploads or swap.

create an empty file

Make a new empty file, empty an existing one, or make a file with a few lines in it.

create nested folders

Make a folder and every folder above it that is missing, and copy a file into a path that does not exist yet.

dates and timestamps

Print dates in any format, turn epoch seconds into dates and back, use other time zones.

delete empty lines

Take out the empty lines of a file, also the ones with only spaces.

delete files and folders

Remove files and folders, ask before each, or remove huge folders quickly.

delete lines containing a text

Remove every line of a file that contains a word, or keep only those lines.

disk full but df says there is room

"No space left on device" with free gigabytes: the disk is out of inodes, the slots for files. Find the folder with too many small files.

docker cannot connect to the daemon

"Cannot connect to the Docker daemon" or "permission denied" on docker.sock: start the service, or let your user use it.

download a whole folder from a website

Fetch every file under a web folder, without climbing to the folders above it.

environment variables

List, read, set and remove environment variables, for a command or for the session.

exclude a folder from find

Search with find without going into folders like .git or node_modules.

find a file

Find a file by its name, by when it changed, or by what is written in it.

find a file's encoding

See which character encoding a text file is in, and convert it to UTF-8.

find big files

Find the files that take the most room, on a whole disk or in a folder.

find files containing a text

Search every file under a folder for a word or a phrase, and show where it is.

find recently changed files

The newest files in a folder, or those changed in the last minutes or days.

find the script's own folder

Inside a script, find the folder the script is in, to reach files next to it.

find this machine's ip address

This machine's address on the local network, and the one the internet sees.

find what killed a process

A program vanished or says "Killed": usually the kernel ran out of memory and stopped it. Find out.

fix error while loading shared libraries

"cannot open shared object file": a program needs a library that is missing or not where it looks.

fix windows line endings

"bad interpreter: ^M" and odd characters: the file has Windows line endings (CRLF). Find them and change them to Unix ones (LF).

free disk space

Find what fills a disk, then clear what is safe to clear: old package files, logs, caches.

get a file's name, folder and extension

Take a path apart in a script: the folder, the name, the name without its extension, the extension.

get inside a docker container

Open a shell in a running container, copy files in and out, read its logs, share a folder with it.

get the full path of a file

Turn a file's name into its whole path from /, through links if you want.

import and export a mysql database

Load a .sql file into MySQL or MariaDB, dump a database to one, and connect from the terminal.

keep a command running after logout

Start a long job so it survives closing the terminal or the ssh session, and come back to it.

list and close tmux or screen sessions

See the terminal sessions left running, join one, or end them.

list only folders

List the folders here and not the files, or only the files.

list users and groups

See the accounts on this machine, the groups, and who is in each.

loop over files, numbers and lists

Repeat commands for every file of a kind, a range of numbers, or the items of an array.

make a script take options

Read a script's arguments and options, and stop with a usage line when they are wrong.

make a symbolic link

Make a name that points to another file or folder, see where a link points, and change or remove it.

make web requests with curl

Send GET and POST requests, JSON and files, see headers and status codes, go through a proxy.

merge pdf files

Join several PDF files into one, and take pages out of one.

pack a folder without some files

Make a tar archive of a folder, leaving out folders like node_modules and files like logs.

pretty-print json

Show JSON indented and coloured, pick values out of it, from a file or a web request.

print colors and new lines

Print text in color, and print new lines from a string, which plain echo may not do.

print or remove lines of a file

Show one line by its number, a range, skip the first ones, or take off the last.

read a file line by line

Go through a file's lines in a script, also split into fields, without losing spaces or backslashes.

read the system logs

Find what went wrong: the errors since boot, a service's log, the kernel's messages.

rename many files

Change the ending of many files, replace spaces in names, or number them.

replace text in a file

Change every place a word or a phrase appears in a file, keeping a copy if you like.

replace text in many files

Change a word or a phrase in every file under a folder that contains it.

run a script

Make a script runnable and run it, or run it in the shell you are in.

run commands on a server over ssh

Run one command or a whole local script on another machine, without logging in by hand.

save a command's output in a variable

Keep what a command prints in a variable, or each line in an array, to use later in a script.

schedule a command with cron

Run a command every day, every hour or every few minutes, and see what is scheduled.

search but skip some files

grep through a project without its build folders, minified files or logs.

see a program's memory use

How much memory a program really uses, and which programs use the most.

see each command a script runs

Debug a script: print every command as it runs, check its syntax, find common mistakes.

see the size of files and folders

How much room each file and folder takes, here or anywhere, biggest last.

send output and errors to a file

Keep what a command prints, its errors too, in a file, or throw them away.

set up an ssh key

Make a key, put it on a server to log in without a password, use a given key, change its passphrase.

show a folder tree

Show folders and files as a tree, to see how a project is laid out.

sort and remove duplicate lines

Sort lines, keep each once, count how often each appears.

split a big file

Cut a big file into smaller pieces, by lines or by size, and join them again.

split a string in bash

Cut a string at a delimiter into parts or an array.

stop a frozen program

Find a program that stopped answering and end it: politely first, by force if it must.

stop a script on errors

Make a script stop at the first failing command, say where, and still allow the ones that may fail.

sync two folders

Make one folder a copy of another, here or on a server, copying only what changed.

the internet does not work

Go step by step from this machine out: its address, the router, the internet by number, then by name.

time a command

How long a command takes, and how much memory and processor it used.

undo changes in git

Throw away changes not committed yet, unstage files, or put work aside for later.

undo the last git commit

Take back a commit, keep its changes or not, fix its message, or undo one already pushed.

unmount a busy drive

"target is busy": something still uses the drive. Find it, stop it, then unmount.

unpack an archive

Open any common archive: tar, zip, gz, xz, 7z, rar.

what is slowing the machine

See whether the processor, the memory or a disk is the bottleneck, and which program is behind it.

what is using a port

Find the program listening on a port (port 80 here), see what it is, and stop it if it should not be there.

which linux version is this

The distribution, its version, the kernel and the hardware it runs on.

which package a file came from

Find the package that installed a file or a command, or the one that would install a file you do not have yet.

which shell is running

Find out which shell runs now, which one is your default, and change it.

who is logged in

See who is on this machine now, who was before, and the failed attempts.

why won't a service start

Read what a systemd service said when it failed, check its unit file and its configuration, then start it again.

work with strings in bash

Join, cut, replace, change the case of and trim strings in a script, without other programs.