Bash Snippets
General
Find difference in output of two shell commands:
bashdiff <(ls) <(ll) # or to see all the difference similar to (n)vim -d (diff) nvim <(pacman -Qi nvim) <(pacman -Si nvim)
Upload files and share for free (limit 10GB):
bashcurl --upload-file ./hello.txt https://transfer.sh/hello.txt
Translate or delete characters:
bashecho $PATH # /home/prabhu/.local/bin:/usr/local/sbin echo $PATH | tr ':' '\n' # /home/prabhu/.local/bin # /usr/local/sbin
Columnate lists:
bashmount | column -t
Images
Compress image:
bashmagick -format jpg -quality 50 /path/to/image.svg /path/to/image.jpg # or mogrify -compress JPEG -quality 50 /path/to/image.jpg # or convert -strip -interlace Plane -gaussian-blur 0.05 -quality 85% /path/to/source/image.jpg /path/to/result/image.jpg # or jpegoptim --size=512k /path/to/image.jpg
Compare images: using Image magick compare
bashmagick compare image1.jpg image2.jpg diff.png
7z
7z with ultra compression:
bash7z a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on archive.7z dir1
PDF
PDF to image:
bash# pdftoppm <image_format> <input_pdf> <image_output> pdftoppm -png resume.pdf resume.png # by default it will output 100 DPI, which can be increased pdftoppm -png -rx 300 -ry 300 resume.pdf resume.png
ffmpeg
Simple conversion of videos:
bashffmpeg -i input.mp4 output.mp4
mpv
Use mpv to take snapshots from webcam:
mpv av://v4l2:/dev/video0 --profile=low-latency --untimed
To use MJPEG as the pixelformat instead of the default, which in most cases is YUYV, you can run the following instead:
mpv --demuxer-lavf-format=video4linux2 --demuxer-lavf-o-set=input_format=mjpeg av://v4l2:/dev/video0
In some cases this can lead to drastic improvements in quality and performance (5 FPS -> 30 FPS for example), see the mpv documentation
Python
pip
pip batch update:
bashpip install -U `pip list --outdated | tail -n +3 | awk '{print $1}'`
Manjaro
Pacman
List all installed packages sorted by size:
bashpacman -Qi | awk '/^Name/{name=$3} /^Installed Size/{print $4$5, name}' | sort -h
Clear pacman cache
bash# get total cached packages sudo ls /var/cache/pacman/pkg/ | wc -l # get total cache size du -sh /var/cache/pacman/pkg/ # clean all packages, expect the 3 most recent versions sudo paccache -r # clean all packages, expect the n most recent versions sudo paccache -rk n # remove all uninstalled packages sudo paccache -ruk0 # OR sudo pacman -Sc # remove all installed and uninstalled package cache sudo pacman -Scc
Alternative create a hook to auto clean cache
bashsudo vi /etc/pacman.d/hooks/clean_package_cache.hook
text[Trigger] Operation = Upgrade Operation = Install Operation = Remove Type = Package Target = * [Action] Description = Cleaning pacman cache... When = PostTransaction Exec = /usr/bin/paccache -r
Starship Prompt
Starship is a minimal, blazing-fast, and infinitely customizable cross-shell prompt for any shell
Installation Linux
Install/Update the latest version:
bashcurl -sS https://starship.rs/install.sh | sh
Add the following to the end of
~/.bashrc
or~/.zshrc
:basheval "$(starship init bash)" # -- or -- eval "$(starship init zsh)"
OpenSSH
Known Hosts
- Remove Entry from the Known-Hosts File:
ssh-keygen -R hostname
Using the SSH Config File
If you are regularly connecting to multiple remote systems over SSH, you can configure your remote servers with the .ssh/config
file
Example:
Host dev
HostName dev.your-domain
User xcad
Port 7654
IdentityFile ~/.ssh/targaryen.key
Host *
User root
Compression yes
Connect to a host (like dev
, eg.) with ssh dev
OpenSSL
- Generate a DKIM private and public keypair:
openssl genrsa -out dkim_private.pem 2048
openssl rsa -in dkim_private.pem -pubout -outform der 2>/dev/null | openssl base64 -A
KDE
Baloo File Indexer
Disable it
bashbalooctl suspend balooctl disable