Karambir Singh Nain

Simple Linux Tips and Tricks

Published
Updated

Using Ubuntu(Pop!_OS) and Arch for a decade and I still forget how to do simple tasks. I am documenting a few here and will keep updating this for future reference.

Quick Upgrade Ubuntu or Arch

Add one of the following to ~/.bashrc:

bash alias _ug="yay -Syu --devel --needed && flatpak update --noninteractive" alias _ug="sudo apt update && sudo apt upgrade -y && flatpak update --noninteractive"

Arch based system is using yay, pacman wrapper and aur helper.

Download Videos

Use yt-dlp to download videos for offline viewing:

bash yt-dlp -f "bestvideo[height<=2160][vcodec!^=av01]+bestaudio/best[height<=2160][vcodec!^=av01]" --all-subs --convert-subs srt --embed-subs --external-downloader aria2c

Check drive temperatures

Use HddTemp package

sudo hddtemp /dev/sda /dev/sdb /dev/sdc /dev/sdd /dev/sde /dev/sdf /dev/sdg /dev/sdh

tar archives

Tar has the following options:

  • -c is crate archive mode, others are options in that mode
  • -x is extract archive mode
  • -v verbose
  • -z create gzip
  • -f tar filename

Create tar -cvzf test.tar.gz test-dir/

Extract tar -xvzf test.tar.gz /op/test

Split and combine large files

Split command can be used to split large files into many smaller ones:

e.g. split -b 50M thunderbird-profile.tar.gz "profile.tar.gz.part."

Similarly to combine these files again, use cat:

e.g. cat profile.tar.gz.part.* > thunderbird.profile.tar.gz

Combine multiple pdf files

The command pdfunite does this job well. It is part of popplet-utils package

bash sudo apt install poppler-utils pdfunite file1.pdf file2.pdf file3.pdf output.pdf

Combine multiple images into PDF

The cli convert, which is part of imagemagick suit, can be used to convert images to pdf

bash sudo apt install imagemagick convert image1.jpg image2.jpg image3.jpg output.pdf

It can convert between image formats as well as resize an image, blur, crop, despeckle, dither, draw on, flip, join, re-sample, and much more. See ref

Remove password from protected pdf file

  1. Install pdftk
  2. sudo apt install pdftk
  3. pdftk <path-to-encrypted.pdf> input_pw <password> output <new-file.pdf>

Newer protection mechanisms are not supported by pdftk. Use qpdf for those:

bash qpdf <path-to-encrypted.pdf> --password=<password> --decrypt <new-file.pdf>

Remove password for all protected pdf files in DIR

(Optional) First rename all files with some suffix

bash rename .pdf _protected.pdf *

Decrypt all files ending with _protected.pdf and output files with suffix

bash for filename in *_protected.pdf; do IFS='_' read -ra file_first file_ext <<< "$filename"; pdftk $filename input_pw <password> output "${file_first}.pdf"; done

Socks proxy

ssh -D 8123 -f -C -q -N <user@example.com>

Monitor ongoing tcp traffic

sudo tcpdump -i eth0

List open TCP ports

sudo ss -plnt

Disable Gnome tracker daemon

It hogs cores when files are changing fast.

https://gist.github.com/vancluever/d34b41eb77e6d077887c

Flash windows ISO to usb

Installation

```bash sudo add-apt-repository ppa:tomtomtom/woeusb sudo apt update sudo apt install woeusb wimtools

Then flash ISO

sudo woeusb --device windows.iso /dev/sda ```

Check Postgres table size

SELECT table_name, pg_relation_size(quote_ident(table_name)), pg_size_pretty(pg_relation_size(quote_ident(table_name))) FROM information_schema.tables WHERE table_schema = 'pub lic' ORDER BY 2 DESC;

Backup and restore containerized Postgres db

bash docker exec -t "container_id" pg_dump --no-owner --quote-all-identifiers -U "pg_user" "pg_db" > dump_`date +%d-%m-%Y"_"%H_%M_%S`.sql

Either restore using psql execute or give postgres docker container sql file in initrd

```bash cat dump_01-01-2020_20_00_00.sql | docker exec -i "container_id" psql -U "pg_user" "pg_db"

```

Linux Swiss Army Knife

Cyberchef is the best software tool to do daily task that a programmer or sysadmin might need to do. It gives a lot of recipes like json minify, do data compression/decompression, base54-encode-decode, convert to and from hex, calculate hashes etc.

Bookmark/pin its page: https://gchq.github.io/CyberChef/

Local TOTP Generation

If you want to generate totp from a secret locally, use oath-toolkit package. To generate totp:

bash oathtool --base32 --totp "SECRET" -d 6

More

See my dotfiles repo on GitHub to see helpful bash aliases and utilities I use daily.