# bash cheatsheet
# nav: win, vim, RegEx, python
# edit

Ctrl+A/E  # Jump to BOL/EOL
Alt+B/F   # Jump word Back/Fwd

Ctrl+U/K  # Kill to BOL/EOL
Ctrl+W    # Kill the previous word (Alt+D next)
Ctrl+Y    # Yank (retrieve) last item killed
Ctrl+X,E  # Edit the command

Ctrl+R    # Incremental back search (next item)
Ctrl+-    # Undo

Ctrl+L    # `clear`
Ctrl+Z    # Suspend a job (`fg`, `bg`, `jobs`)

!!    # The last command
!$    # The last argument (Alt+.)
!*    # All last command's arguments
cd    # cd ~
cd -  # Back
(cd .. && script)  # Run in another dir and return
pushd/popd         # Save/recover dir

cat << EOF > ./filename
multiline file content
EOF

trap 'echo "# $BASH_COMMAND";read' DEBUG

reset  # Recover terminal
script # Start logging the session to typescript file
tr     # Translate (character-wise replacer)
pv, rsync  # Progress

> [path]     # Clears the file contents
source or .  # executes the content of the file passed as argument in the current shell
zless, zcat, zgrep   # gzipped action
grep -r [pattern] [dir]   # search recursively for pattern in directory
cal           # shows the month's calendar
w             # displays whois online
last          # lists last logins
df -h         # shows disk usage
du -h --max-depth=4 /var | sort -rh | head -100  # who ate the disk
echo "!!" > last-command.sh

dig [domain]   # gets DNS information for domain
dig -x [host]  # reverses lookup host
curl ifconfig.me  # Get the external IP
ping -a  # Audible bell on packet reception
lsof -P -i -n  # Current network activity
nc -vlp 8000 < index.htm  # Serve a file on port 8000 with netcat
python3 -m http.server 8000 # Serve . on port 8000

diff -r [dir1] [dir2]  # diff between directories
diff <(sort [file1]) <(sort [file2])  # diff two unsorted files
mount -t tmpfs tmpfs /mnt/ramfs -o size=1024m  # Mount a ramfs
ls /etc/*.conf | xargs -i cp {} ~/out
cp /home/sample.txt{,-old}
rename -n 's/foo/bar/' *.txt
tar czf target.tgz path1 [pathN]...
tar xf source.tgz
wget --random-wait -r -p -e robots=off -U mozilla http://www.example.com  # Download the whole site
fuser -k filename  # kills a process that is locking a file
sed -E 's/(access_log ).+;$/\1 off;/' -i /etc/nginx/nginx.conf
sed -E "s/(\"ConfigKey\":\s*)\".*\"/\1\"new value\"/" -i appsettings.json
find . -path ./.git -prune -o -type f -name *.csproj -exec sed -i 's/netcoreapp2.0/netcoreapp2.1/' '{}' \;
find . -type f -regex '.*\.\(sh\|json\)$' -exec sed -i 's/\x0D$//' '{}' \; -exec echo "Line endings changed to LF in {}" \;
shuf -i 1024-65535 -n 1  # random high port
script_dir="$(readlink -f "$(dirname "$0")")"
ssh-keygen -t ed25519
apt-file search -x /dig$
apt install apache2-utils && htpasswd -nB username

pip install git+https://github.com/ytdl-org/youtube-dl.git
youtube-dl -F URL
youtube-dl -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best' URL
youtube-dl --no-progress -f 137+140/136+140 URL
ffmpeg -i f136.mp4 -i f140.m4a -c copy merged.mp4

# access a resource in remote network from local network
# localhost:3388 <== [ remote-host ==> 192.168.1.2:3389 ]
ssh -NL 3388:192.168.1.2:3389 remote-host

# open a port on remote-host to a resource from local network
# check GatewayPorts in remote-host:/etc/ssh/sshd_config
# [ remote-host:3399 <== remote-host ] ==> 192.168.1.2:3389
ssh -NR 3399:192.168.1.2:3389 remote-host

top: cVeEEttmmbxf

screen: Ctrl+A, d    detach
        Ctrl+A, k    kill
        Ctrl+A, ?    help
        
Alt/Fn+PrtScr + REISUB  # SysRq: unRaw, tErminate, kIll, Sync, Unmount, reBoot. (Space = help)

cli wi-fi:
    # ip link
    # wpa_supplicant -B -i [interface] -c <(wpa_passphrase [SSID] [passphrase])
    # dhclient -v [interface]
    # apt update && apt install network-manager
    # nmtui

See Also: esh, COMMANDLINEFU, bash magic