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.
- add a folder to the pathLet the shell find programs in a folder, for good, and reload the settings without logging out.
- add a userCreate an account with a home folder and a password, and let it use sudo if it should.
- add to the end of a fileAdd lines to a file without replacing it, also to a file only root can write.
- add up numbers in a fileSum 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 scriptAsk yes or no, read a password without showing it, or wait for an answer with a time limit.
- back up and restore postgresqlRun a .sql file, dump a PostgreSQL database and load it back, from the terminal.
- change permissions of a folder and everything in itGive a folder, its subfolders and its files the permissions or the owner they need, without making them open to everyone.
- check a certificateSee a site's TLS certificate, when it expires and who issued it, or the same for a .pem file.
- check a file's permissionsSee who may read, write and run a file or a folder, and why a path is refused.
- check if a file or folder existsTest in a script whether a path exists, is a file, a folder, empty or runnable.
- check if a program is installedFind whether a command exists, where it is, and which package it came from.
- check if a string contains a textTest in a script whether a string contains, starts with or matches something.
- command not found after installingYou installed a program but the shell says "command not found": find where it went and let the shell see it.
- compare two foldersFind which files differ between two folders, or are only in one of them.
- compress a folderPack a folder into one compressed file, look inside it, and unpack it again.
- copy a folderCopy a folder with everything inside it, keeping its dates and permissions if needed.
- copy files to and from a serverCopy a file or a whole folder between this machine and another one over ssh.
- copy to the clipboard from the terminalSend a command's output or a file to the clipboard, and paste from it.
- copy without some foldersCopy a folder but leave out some of what is inside, like node_modules or .git.
- copy, overwriting or notCopy without being asked each time, or without ever replacing a file that is there.
- count lines of codeHow many lines of code a project has, in total and by language.
- count lines, words and filesCount the lines of a file or of a command's output, the words, and the files in a folder.
- count the processorsHow many processors and cores this machine has, and what they are.
- create a large fileMake a file of a given size, for testing disks, uploads or swap.
- create an empty fileMake a new empty file, empty an existing one, or make a file with a few lines in it.
- create nested foldersMake a folder and every folder above it that is missing, and copy a file into a path that does not exist yet.
- dates and timestampsPrint dates in any format, turn epoch seconds into dates and back, use other time zones.
- delete empty linesTake out the empty lines of a file, also the ones with only spaces.
- delete files and foldersRemove files and folders, ask before each, or remove huge folders quickly.
- delete lines containing a textRemove 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 websiteFetch every file under a web folder, without climbing to the folders above it.
- environment variablesList, read, set and remove environment variables, for a command or for the session.
- exclude a folder from findSearch with find without going into folders like .git or node_modules.
- find a fileFind a file by its name, by when it changed, or by what is written in it.
- find a file's encodingSee which character encoding a text file is in, and convert it to UTF-8.
- find big filesFind the files that take the most room, on a whole disk or in a folder.
- find files containing a textSearch every file under a folder for a word or a phrase, and show where it is.
- find recently changed filesThe newest files in a folder, or those changed in the last minutes or days.
- find the script's own folderInside a script, find the folder the script is in, to reach files next to it.
- find this machine's ip addressThis machine's address on the local network, and the one the internet sees.
- find what killed a processA 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 spaceFind what fills a disk, then clear what is safe to clear: old package files, logs, caches.
- get a file's name, folder and extensionTake a path apart in a script: the folder, the name, the name without its extension, the extension.
- get inside a docker containerOpen 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 fileTurn a file's name into its whole path from /, through links if you want.
- import and export a mysql databaseLoad a .sql file into MySQL or MariaDB, dump a database to one, and connect from the terminal.
- keep a command running after logoutStart a long job so it survives closing the terminal or the ssh session, and come back to it.
- list and close tmux or screen sessionsSee the terminal sessions left running, join one, or end them.
- list only foldersList the folders here and not the files, or only the files.
- list users and groupsSee the accounts on this machine, the groups, and who is in each.
- loop over files, numbers and listsRepeat commands for every file of a kind, a range of numbers, or the items of an array.
- make a script take optionsRead a script's arguments and options, and stop with a usage line when they are wrong.
- make a symbolic linkMake a name that points to another file or folder, see where a link points, and change or remove it.
- make web requests with curlSend GET and POST requests, JSON and files, see headers and status codes, go through a proxy.
- merge pdf filesJoin several PDF files into one, and take pages out of one.
- pack a folder without some filesMake a tar archive of a folder, leaving out folders like node_modules and files like logs.
- pretty-print jsonShow JSON indented and coloured, pick values out of it, from a file or a web request.
- print colors and new linesPrint text in color, and print new lines from a string, which plain echo may not do.
- print or remove lines of a fileShow one line by its number, a range, skip the first ones, or take off the last.
- read a file line by lineGo through a file's lines in a script, also split into fields, without losing spaces or backslashes.
- read the system logsFind what went wrong: the errors since boot, a service's log, the kernel's messages.
- rename many filesChange the ending of many files, replace spaces in names, or number them.
- replace text in a fileChange every place a word or a phrase appears in a file, keeping a copy if you like.
- replace text in many filesChange a word or a phrase in every file under a folder that contains it.
- run a scriptMake a script runnable and run it, or run it in the shell you are in.
- run commands on a server over sshRun one command or a whole local script on another machine, without logging in by hand.
- save a command's output in a variableKeep what a command prints in a variable, or each line in an array, to use later in a script.
- schedule a command with cronRun a command every day, every hour or every few minutes, and see what is scheduled.
- search but skip some filesgrep through a project without its build folders, minified files or logs.
- see a program's memory useHow much memory a program really uses, and which programs use the most.
- see each command a script runsDebug a script: print every command as it runs, check its syntax, find common mistakes.
- see the size of files and foldersHow much room each file and folder takes, here or anywhere, biggest last.
- send output and errors to a fileKeep what a command prints, its errors too, in a file, or throw them away.
- set up an ssh keyMake a key, put it on a server to log in without a password, use a given key, change its passphrase.
- show a folder treeShow folders and files as a tree, to see how a project is laid out.
- sort and remove duplicate linesSort lines, keep each once, count how often each appears.
- split a big fileCut a big file into smaller pieces, by lines or by size, and join them again.
- split a string in bashCut a string at a delimiter into parts or an array.
- stop a frozen programFind a program that stopped answering and end it: politely first, by force if it must.
- stop a script on errorsMake a script stop at the first failing command, say where, and still allow the ones that may fail.
- sync two foldersMake one folder a copy of another, here or on a server, copying only what changed.
- the internet does not workGo step by step from this machine out: its address, the router, the internet by number, then by name.
- time a commandHow long a command takes, and how much memory and processor it used.
- undo changes in gitThrow away changes not committed yet, unstage files, or put work aside for later.
- undo the last git commitTake 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 archiveOpen any common archive: tar, zip, gz, xz, 7z, rar.
- what is slowing the machineSee whether the processor, the memory or a disk is the bottleneck, and which program is behind it.
- what is using a portFind 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 thisThe distribution, its version, the kernel and the hardware it runs on.
- which package a file came fromFind 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 runningFind out which shell runs now, which one is your default, and change it.
- who is logged inSee who is on this machine now, who was before, and the failed attempts.
- why won't a service startRead what a systemd service said when it failed, check its unit file and its configuration, then start it again.
- work with strings in bashJoin, cut, replace, change the case of and trim strings in a script, without other programs.
No recipe matches that. Try a shorter word, or ask for a new one.