Duncan Wither

Home Notes Github LinkedIn

Terminal Cheat Sheet

Written: 05-Mar-2022 (Updated: 24-Jun-2024)

Basics

Commands

Folders

Environmental Variables

List of some - of the more useful - environmental variables. Run env to list them all.

Operators

Other Commands

Find

Basic Usage:

find {dir} {comand}

I like to search by regex, for example, searching for pictures:

find . -iregex '.*\.\(png\|jpg\)'

find in current directory (.) with the regex1 for a file with any name length engin in .jpg or .png2.

Moving after finding, apped the -exec option

find /home -type f -name '*.pdf' -exec mv -t /destination {} + 

Find all the filename extensions, courtesy of so:

find . -type f | perl -ne 'print $1 if m/\.([^.\/]+)$/' | sort -u

PDF Stuff

To concatenate a pdfs together try one of [these] and report back!

Split a PDF into the pages you want:

pdftk full-pdf.pdf cat 12-15 output outfile_p12-15.pdf

Youtube-DL

Download MP3s wiht youtube-dl:

youtube-dl -f bestaudio --extract-audio --audio-format mp3 --audio-quality 0 -o output.mp3 $URL_STR

Download Videos with youtube-dl (Resolution <= 720p):

youtube-dl -o "Location/%(title)s.%(ext)s" --restrict-filenames --no-playlist -f "\[height <=? 720\]" "$URL"

Downloading a playlist and converting to mp3 (where $PLAYLIST_STRING is the “string” for the playlist in the URL):

youtube-dl -i $PLAYLIST_STRING -f bestaudio --extract-audio --audio-format mp3 --audio-quality 0 -o "%(title)s.mp3"

FFMPEG

Use ffmpeg to convert .mp3 to .wav:

ffmpeg -i input.mp3 -acodec pcm_s16le -ac 1 -ar 16000 output.wav

Convert images (1.jpg, 2.jpeg and so on) to a video:

ffmpeg -framerate 60 -i %d.jpg -c:v libx264 -r 60 -vf format=yuv420p output.mp4

Use the following to convert images to that naming format (note it might be better to find a method with preceding zeros):

ls -v | cat -n | while read n f; do mv -n "$f" "$n.jpg"; done

Combining that into one script (run in the folder):

mkdir temp
ls -v *.JPG | cat -n | while read n f; do cp -n "$f" "./temp/$n.jpg"; done
$(cd temp/ && ffmpeg -framerate 30 -i %d.jpg -c:v libx264 -r 60 -vf format=yuv420p output30.mp4)
$(cd temp/ && ffmpeg -framerate 60 -i %d.jpg -c:v libx264 -r 60 -vf format=yuv420p output60.mp4)
mv temp/output60.mp4 ./output60.mp4
mv temp/output30.mp4 ./output30.mp4
#rm temp/

Pandoc

Convert .md to a presentation in pdf format:

pandoc -t beamer presentation.md -o output.pdf

This need different headers for each of the slides.

To Convert .md to latex pdf documents:

pandoc MANUAL.txt --pdf-engine=xelatex -o example13.pdf

For more demos use the pandocs demo page.

Server Admin Stuff

Getting Failed login attempts:

grep "Failed password" /var/log/auth.log

Imagemagick

Very useful in general. Code to screenshot part of the screen:

import image_name.png

Code to OCR part of the screen

import png:- | tesseract - - quiet | xclip -sel clip

Convert .heic to .png

heif-convert heic_name.heic png_name.png

Diff images (examples on stack exchange):

compare -compose src tux_orig.png tux_modified.png tux_difference.png

Flameshot

I like flameshot. It’s simple, and effective for screen capture. For your instant screenshot to a folder:

flameshot full -p /path/to/captures

For a screenshot with a handy interface:

flameshot gui

Secure Copy

Use scp to copy files using ssh from/to remote servers. From local to remote:

scp local/file.txt username@123.123.1.23:/path/to/remote/dir/

From remote to local:

scp username@123.123.1.23:/path/to/file.txt  /local/directory

Add -r to do folders.

To transfer between servers (that don’t have linked keys) use the -3 flag. Thank you stack exchange. This copies it all via your machine (so make sure you’ve got the disk space).

scp -3 user@server1:/path/to/file user@server2:/path/to/file

Rsync

Use rsync to send files to remote computer. It’s more advanced than scp for better and worse.

rsync -azP file-or-folder user@site-or-ip:folder

Note: It has the same [SOURCE] [DESTINATION] syntax as scp Note: Those flags are just what I default to. Read the man page for more info.

One line for Loop

Do something for each file in a dir. E.g. count lines of each file:

for f in *; do wc -l $f ;

Tee

tee allows you to pipe to a file, and to stdout

ls -l | tee filename.txt

Password Generation

pwgen -cBsy 20 -N 1

Sed - Stream Editor

Here’s a useful tool for testing sed patterns.

Fuzzy Finder

fzf tutorial and hints.

Finding Extensions

To list the extensions of all the files in a directory use the following (courtesy of stack overflow):

find . -name '*.?*' -type f | rev | cut -d. -f1 | rev  | tr '[:upper:]' '[:lower:]' | sort | uniq --count | sort -rn

Searching the Network

Use nmap as per this answer:

nmap -sn 192.168.1.0/24

PDF tricks

Separate single multi-page pdf into individual pdf files of it’s pages (it will label them out1.pdf, out2.pdf and so on):

pdfseparate multipage.pdf out%d.pdf

Combine pdf files into one (in this case input1.pdf followed by three copies of input2.pdf) (source):

pdftk A=input1.pdf B=input2.pdf cat A B B B output out.pdf

  1. Note: -iregex ignores case, -regex doesn’t.↩︎

  2. Note: escaped chars. needed↩︎