⌨️ how-to linux
or, the “the wisdom of the tee” part.
tl;dr
- absolute basics:
cd,ls,rm,mv,vim - shortcuts: [ctrl] + [a|e|c|k|z]
- intermediate:
|,history,grep(ripgrep),cat,less,man,find(fd),df,du(dust),sudo,tail,touch,alias,sh,exit - processes:
&,>,top,ps,kill,killall,which,locate,fg - network:
curl,whois,traceroute,ping,lsof,scp - handy commands: SSH key generation, handling archives, replacing en masse, permissions, JSON parsing, shell script checks,
zx
learning how to use the terminal of your computer is a console without any buttons. you are expected almost to know which “buttons” are hidden there below the surface. no one ever gave me a primer and it was painful to learn over time through trial and error what those hidden commands are. here’s a compilation of some useful commands to get you through most basic functions and some more interesting ones.
btw, god help you if you need to write shell scripts. ugh, i think they’re the worst syntactically. my tendency is to write node or python scripts instead because at least they make bore sense to me.
absolute basics
cd [name]— change directory~is shorthand for your home directory..is shorthand for the parent directory.is shorthand for the current directory-is shorthand for the previous directory you were in (like your browser’s “back” button)- e.g.
cd ..will take you to the parent directory
ls— list contents of the directory you’re inls -lahis useful to list hidden files as well
rm [file]— remove a filerm -r [file]— remove a directory
mv \[origin\] [destination]— move a file somewhere elseclear— also, clears screen
vim [file]— edit a fileito go to insert modeescto go back to command-mode/to search in the file:w(in command mode) to save:q(in command mode) to quit
shortcuts
this is a pet peeve of mine that no one ever taught me these super handy shortcuts and i didn’t know about them for many years.
[ctrl] + a — go to beginning of the line [ctrl] + e — go to the end of the line [ctrl] + c — kill the currently running program [ctrl] + k — clear screen [ctrl] + z — pause the currently running program
intermediate
|— the “pipe”. pass the output from one process to another, e.g.history | grep cdhistory— see the list of commands you’ve tried in the pastgrep [pattern] *— search the files in the current directory for the pattern- tip: easier, faster alternative is
ripgrep, you can install viabrew install ripgrep
- tip: easier, faster alternative is
grep [pattern] * -rsiI— search recursively, case-insensitive, ignoring binary fileshistory | grep [pattern]— find relevant commands in your history you’ve used beforecat [file]— output the contents of a fileless [file]— scroll through the contents of a file (spaceto page through,qto quit)man [command]— show help file for a command,[cmd] -hor[cmd] --helpalso can workfind . -name '[file]'— find a file by a specific name- tip: easier alternative is
fd, you can install viabrew install fd
- tip: easier alternative is
df -h— show local hard disk spacedu -sch * |sort -hr— show the biggest files under a particular subdirectory- tip: easier alternative is
dust, you can install viabrew install dust
- tip: easier alternative is
sudo [command]— run command with elevated privilegestail -f [file]— see real-time updates to a file, used especially with a log file on a servertouch [file]— creates file if doesn’t exist, or updates its timestamp- useful to kick off a process that might be waiting for a file to change before running
alias [shortcut]='[command to run]'— take a long command that’s hard to remember and create a shortcut for it, e.g.alias v='ls -lah'sh— open up another shell (zshif you’re using that)exit— to exit the shell
processes
[cmd] &— adding the ampersand at the end of a command will run it in the background[cmd] > output.txt— adding a>will save output of the program to a particular filetop— find out what’s current running on your machineps aux— see the processes currently running on your machineps aux | grep [name]— super common way to find the process you’re looking forkill [process id]— you can grab a process id from theps auxlist and kill itkillall [name]— kill process(es) matching the namewhich [command]— find out the directory where a program liveslocate [pattern]— find files on your local machine matching the pattern (macOS only)fg— start a paused program again (via [ctrl] + z), or, usebgto run it in the background
network
curl -O [url]— retrieve a webpage and save it diskwhois [domain]— find out info about a particular websitetraceroute [domain]— show computer hops from router to router to a destinationping [domain]— simple test to see if a website is up or notping6 [domain]— same as above, but for IPv6
lsof -i :[port]— see which process is using which port.lsof -t -i tcp:[port] | xargs kill -9— to easily kill whatever’s on a port.
scp username@remotehost:/path/to/file /path/to/save/locally— copy a file from a remote server
handy commands
ssh-keygen -t rsa -b 4096 -C "[example@email.com]"— generate SSH keyssh-add -K ~/.ssh/id_rsa— add password to your keychain to save timetar -czvf \[name\] [file]— compress a file/directory into an archivetar -xzvf [file]— uncompress an archivegrep [pattern] * -rsiIl | xargs sed -i '' s/[pattern]/[replacewith]/g—- replace a pattern in all the files found recursively within a particular directory
sudo chown -R $USER:$USER file— transfer ownership of a directory and its files to yourselfjq '[selector]' < example.json— if your server logs are in JSON format you can query them using thejqcommandfc— fix command. open up the last command you tried to run in an editor.<( somecommand )— treat a command output as a file. e.g. trydiff <(ls) <(ls -a)cmatrix— make it look like you’re doing something important 😉shellcheck— check your shell script for errors before running it.zx— a handy JS tool around shell scripting that makes running multiple commands a little more intuitive for those of us that don’t live on the command line day-to-day (i.e. most of us).- and even more handy commands can be found in this list.