Adrian Stobbe

Unix cheatsheet - Beyond the basics

Last updated:

4 minutes (1124 words)
Table of contents

We developers use the terminal a lot and there are a lot of tricks that can ease our work. So it’s worth looking beyond the essentials of getting stuff done in the shell! The quickest way to find what you need is probably to use tldr. But the frequently used hacks are best learned once to know about all the good stuff and work effectively. I recently found the great The Linux Command Handbook from Freecodecamp and went over it to extract the useful bites that I didn’t know.

So, here they are:

πŸ”—Files

πŸ”—cp

-v: verbose

πŸ”—mkdir

-p: create multiple nested folders

πŸ”—rmdir

delete empty dir. Saver than (rm -rf). But best to use trash

πŸ”—ls

-h: human readable file sizes (M,K…)

πŸ”—ln

the default is a hard link.

Hard link: points to content linked at time of linkage (does copy)

Soft link: pointer to the original file. Breaks when original is moved.

-s original symlink: soft link

πŸ”—gzip

compress SINGLE file. By default the original file is removed.

-k: keep original file -d: unzip or gunzip -v: verbose info on saved storage percentage

πŸ”—tar

gather multiple files (uncompressed)

-cf archive.tar FILES...: create archive

-czf archive.tar.gz: tar and zip

-xf archive.tar.gz -C directory: extract (potentially zipped) archive to specified directory.

-tf archive.tar: show files without extracting

πŸ”—xargs

take output and turn into arguments for other command. This is especially useful for commands that don’t accept stdin (e.g. touch,rm,ls)

Example:

cat listFiles.txt | xargs rm

For find use -print0 | xargs -0 to properly split the matching files

-p: print conformation prompt

sh -c "command1 && command2 | command3": run multiple commands

-I _ command _ optional_extra_arguments: put output into _ placeholder

πŸ”—View

πŸ”—cat

concatenate / print intput

-n: see line number

cat file1 file2 >> file3

πŸ”—tail

open the file at the end

-f: (follow) watch for changes

-n 10: print last 10 lines

πŸ”—less

view file inside interactive UI

space: navigate down one page

b: navigate up one page

navigation similar to vim

v: open in default system editor (vim)

F: follow mode (see changes live)

:n: open next file (for multiple input files)

:p: open previous file

πŸ”—nano

ctrl+s: save

ctrl+x: quit

for all the rest, use vim πŸ˜›

πŸ”—Extract

πŸ”—tr

translate / delete character in a file:

tr find_character replace_character < filename -d: delete occurences of character

πŸ”—cut

cut given character from file

-c 5-10: cut character nbr 5 to 10 from each line -d "," -f 2,6: extract the second and sixth , delimited field from each line

πŸ”—grep

extract information (supports regex)

-n: show line numbers

-C 2: show 2 lines before and after match

-i: ignore case

-v: invert search (exclude all matches)

Example:

grep -n document.getElementById index.md

πŸ”—find

find files relative to directory

. -name '*.js': find js files relative to current directory

-type: type can be f (file), l(link),d(dir)

-mtime -1: match all files modified within 1 day

find folder1 folder2: can provide mutiple search directories

-or: or condition

-delete: deletes matches

-exec: execute command on each result

πŸ”—wc

-l: just count lines

-w: just count words

πŸ”—uniq

output unique lines

-c: count occurences

often used with sort (sort -u also filters unique elements):

sort <input> | uniq

πŸ”—diff

compare two files

-y: compare next to each other, line by line

-u: show differences like in Git

-r: (recursively)

πŸ”—basename

get last segment of provided path:

basename /Users/adria/test.txt

returns test.txt

πŸ”—dirname

provides dirname of path (conjugate of basename)

πŸ”—Process

πŸ”—kill

kill signals (alternative after /) from softest:

-TERM / -15: terminate

-HUP / -1: (hang up) process before terminating

-KILL / -9: abruptly terminate

πŸ”—killall

killall NAME kill all processes matching NAME

πŸ”—jobs

find all running jobs (including suspended ones)

πŸ”—fg

ctrl+z: suspend process

If only 1 process is suspended, its enough to run fg, otherwise do:

fg 2: bring job 2 to foreground (get id from jobs)

πŸ”—bg

run job in background (like appended & to command). API is same as fg

πŸ”—ps

ax: a also shows processes of other users. x shows processes not linked to terminal

axww: to prevent cutting of command path

πŸ”—top

display real-time info on resource usage (CPU, memory…)

-o mem: sort by memory usage

πŸ”—nohup

let long-lived process run even after disconnect / logout

nohup <command>

πŸ”—env

run command with variable without setting it in the outer environment (current shell)

env USER=adria node app.js

-i: clear variables from outer environment

-u: clear specified variable from command environment

πŸ”—Privileges

πŸ”—su / sudo

su - <user>: login to other user’s home directory

sudo -i: start shell as root

sudo -u <user>: run command as other user

πŸ”—chown

change file ownership

chown -R <owner> <dir>: recursively change permissions of all subdirectories

chown <owner>:<group>: change group as weell

chgrp <group> <file>: change group

πŸ”—chmod

change read, write, execute permissions

-r: apply recursively

Symbolic usage

chmod a+rw filename #everyone can now read and write
chmod og-r filename #other and group can't read any more
```

Numerical usage

πŸ”—Single vs double quotes

"": resolved at definition time

'': resolved at invocation time An example makes it clearer:

alias lsstatic="ls $PWD"
alias lscurrent='ls $PWD'

lscurrent depends on the current directory, whereas static doesn’t.


Sources: Freecodecamp: The Linux Command Handbook, Unix Tutorial

Tags: #Tech