home

Oneliners

Sumup Numbers line by line from stdin and print grand total to stdout

alias sumup='tr "\n" + | head -c-1 | bc -li | tail -n1'

Example: If you would like to find all files below 3MB and sumup their file size in bytes use:

find . -type f -size -3M -printf '%s+' | head -c-1 | bc -i | tail -n1

Calculate a perceptual hash of jpg image files

find -type f -name "*.jpg" -exec bash -c \
'printf "%016x {}\n" "$((2#$(jpegtopnm "{}" -nosmooth | \
pnmscalefixed -h 8 -w 8 | ppmtopgm | pgmtopbm | \
pnminvert | pnmtoplainpnm | tr -dc 01)))"' \;

Dependencies: find and netpbm.

This script calculates a 64bit hash of all your jpg files, so you can compare them and find files with similar (or different) content. It's a really rough, quick and dirty approach:


figlet alternative with netpbm

a nice alternative to figlet by using netpbm

date +'%a %d.%m.%Y' | pbmtext | pbmtoascii

resulting in:


              "M""""M         oM   M"""M    oM"Mo  oM      o"""o oM"Mo o"""o oM"Mo
               M   o  o oo   " M      M     M   M " M          M M   M     M M   M
               M"""M   M"      M     M      M   M   M        o"  M   M   o"  M   M
               M       M       M    M       Mo oM   M      o"  o Mo oM o"  o Mo oM
              """"    """     """   "     "  """   """   " """""  """  """""  """


ssh-keygen parallel pattern match fingerprint

this one generate lots of ed25519 ssh keypairs in your ramdisk and tries to find a keypair with a nice publickey sha256 base64 encoded fingerprint(!), matching the given pattern (e.g. your name). such a fingerprint is logged in syslog the host you'll login, and this will be an eyecatcher.

export s=YOURNAME d=/dev/shm;ulimit 64000;yes|xargs -P 0 sh -c "ssh-keygen -t ed25519 -P '' -f \"$d/s.$$.\$\$\"|\
grep -q \"$s\"&&cp \"$d/s.$$.\$\$\" \"$d/f.$$.\$\$.$(date +%s)\";rm \"$d/s.$$.\$\$\" \"$d/s.$$.\$\$.pub\";echo -n ."

it uses ssh-keygen from openssh. it will print a dot for every generated keypair, so you can add pipeviewer "|pv -b >/dev/null" for speed measurement.

just choose a special s= with your search pattern. replace YOURNAME and start the script. warning: this script will run many tasks in parallel and will generate several thousand keys per second on a modern machine.

if a keypair is found, its named "/dev/shm/f.??????.?????". the script won't terminate, just run forever and search further matching keys. all keys are deleted immediatly if they don't match.

you have to check manually ls /dev/shm/f.* if a key is found. if there is a result, choose a passphrase for the key ssh-keygen -p -f /dev/shm/f.????.???? and export your public key with ssh-keygen -y -f /dev/shm/f.?????.???? > /dev/shm/found.pub. copy both files to a safe place.

Hint: Since random input values are used to generate these keypairs, you can't really predict when you'll find a match. For short strings with 3-4 chars, you should get a result quiet fast. roll the dice. and you have to roll more than a double with lots of dices. :) This is just a quick hack.

Offtopic: Other Things that use Ed25519. So here is a tiny version of ssh-keygen: tiny-ssh-keygen-ed25519 from Péter Szabó (pts), based on tweetnacl. (local mirror. untested. do not trust in it. you would also have to modify the sourcecode to get the fingerprint of current generate key at stdout)


funny geigercounter audio ticks free memory

just for fun. hear some tick-sounds, like a geiger counter. they are getting faster if lesser memory is available. use pacat instead of aplay on pulse audio systems.

while true; do echo geigercounter_free_memory | aplay -q -f cd -r 2000; sleep 0.$(($(grep ^MemFree /proc/meminfo | tr -dc 0-9)/1000)); done

Trivia. Day and Year to 16bit hex

hex (0000-ffff) starting in 1970-01-01 overflow after year 2148:

date +%s/3600/24 | bc | xargs printf %04x | head -c 4 | tr -dc "0-9a-f"

hex (0000-ffff) starting 2022-01-01, overflow after year 2200:

date +%s/3600/24-18992 | bc | xargs printf %04x | head -c 4 | tr -dc "0-9a-f"

Different approach

day of the year no sequence, human readable in decimal (YYDDD, YY: 00-99, DDD: day of year 1-366).

hex (0000-ffff), starting 2022-01-01. Overflow after year 2087:

date +%y%j-22000 | bc | xargs printf %04x | head -c 4 | tr -dc "0-9a-f"

the same, but decimal (00000-99366), leading zero, overflow after 2099:

date +%y%j-22000 | bc | xargs printf %05d | head -c 5 | tr -dc "0-9"

intercept stdout/stderr of another process

strace -ff -e trace=write -e write=1,2 -p SOME_PID

A very simple and useful stopwatch

time read -N 1

Start a command on only one CPU core

taskset -c 0 your_command

Limit the cpu usage of a process

sudo cpulimit -p pid -l 50

See udev at work

udevadm monitor

a more compact version of ls -l

ls -hog

Switch last 2 characters on current command line

Just press CTRL+t


Repoint an existing symlink to a new location

ln -nsf  

List files with quotes around each filename

ls -Q

exclude a column with cut

cut -f5 --complement

Display current time in requested time zones

zdump Japan America/New_York

or use environment vars:

TZ="Japan" date

a short counter

yes '' | cat -n

How to run X without any graphics hardware

startx -- `which Xvfb` :1 -screen 0 800x600x24 && DISPLAY=:1 x11vnc

Color Animation in Terminal

yes "$(seq 232 255;seq 254 -1 233)" | while read i; do printf "\x1b[48;5;${i}m\n"; sleep .01; done

Empty a file

:> file

Calendar synchronicity test

cal 09 1752