Skip to content

Instantly share code, notes, and snippets.

@khssnv
Last active April 18, 2023 04:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khssnv/be1a737d70e5864ccb4788ac74337c9b to your computer and use it in GitHub Desktop.
Save khssnv/be1a737d70e5864ccb4788ac74337c9b to your computer and use it in GitHub Desktop.
Usefull shell commands what fits one line

Color golang test output

go test -v ./tests | sed ''/PASS/s//$(printf "\033[32mPASS\033[0m")/'' | sed ''/FAIL/s//$(printf "\033[31mFAIL\033[0m")/''

Backup your user home data excluding some files

cd ~
tar -cvpzf backup.tar.gz --exclude=./backup.tar.gz --exclude=./.local/share/io.parity.ethereum/chains/ --one-file-system .

Get certain package / python module in NixOS environment

Let's get python serial module

nix-shell -p python3Packages.pyserial

Find and replace substring recursively in files with given extention

Replace txt with your extention

find . -name "*.txt" -type f -print0 | xargs -0 -n 50 perl -pi -e 's/Hello/Greetings/g'

Serve files from current directory

For Python 2.x use SimpleHTTPServer instead of http.server

python3 -m http.server 8000

With nix-shell

nix-shell -p python3 --command "python -m http.server"

Or Haskell

nix-shell -p haskellPackages.hserv --command hserv

From willurd/web-servers.md gist.

Periodically reconnect to certain IPFS peer

watch -n 10 ipfs --api=/ip4/127.0.0.1/tcp/5001 swarm connect /dns4/lighthouse.aira.life/tcp/4001/ipfs/QmdfQmbmXt6sqjZyowxPUsmvBsgSGQjm4VXrV7WGy62dv8

Generate ed25519 key

From Risan article.

ssh-keygen -o -a 100 -t ed25519 -f ~/.ssh/id_ed25519 -C "a.khssnv@gmail.com"

Send file content and exit

cat ./slice_160.urs | nc -q 1 192.168.56.4 30002 >/dev/null

Shape networking trafic

Per network interface. Numbers are kb, -d - download, -u - upload.

wondershaper -a eth0 -d 1024 -u 1024

Disable shaping

wondershaper -c -a eth0

Limit files open for a process (including sockets)

prlimit --pid 12345 --nofile=128:128

Grep keeping some surrounding lines around

grep -B 3 -A 2 foo README.txt

or

grep -C 3 foo README.txt

source

Fetch latest commit for given branch in case fetch --all does not help

git fetch origin master:master

Patch or skip patching a file already patched

patch -r - -b -p0 --forward hello.txt hello.txt.patch

where -r - instrusts to not save *.rej file, -b do the same for backup *.orig file and --forward tells it to continue in case patch finds a problem. sources: one, two

Patch an executable built without nix for NixOS

Create appropriate default.nix package and run

nix-build -E '((import <nixpkgs> {}).callPackage (import ./default.nix) { })' --keep-failed --no-out-link

Ignore strings longer than given length with grep

 grep -R foo | grep -a -v -e '[^\ ]\{500,\}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment