Max's notebook

A collection of sorts


Bash Notes

23 Jul 2019

A quick and dirty reference for bash things I find useful.

Loops

one-liner:

for i in $(ls); do cat $i; done

in a shell script:

#!/bin/bash
for i in $(ls)
do
    cat $i
done

Read a csv as input

one-liner:

while IFS="," read col1 col2 col3; do echo $col3 | md5sum; done < my.csv

in a shell script:

#!/bin/bash
## usage: ./script.sh my.csv

CSV_FILE=$1

while IFS=, read col1 col2 col3
do 
    echo $col3 | md5sum
done < $CSV_FILE

kill all the pids

for pid in $(ps aux | grep [m]yprocess | awk '{ print $2} '); do kill -9 $pid; done
# explanation of the bracket syntax: https://askubuntu.com/a/153430
# The square bracket expression is part of ... grep's character class pattern matching.
# omission mine

Compression

ignore dir tree, just compress the files

# one file from the dir, ignore the tree
tar czfv archive.tgz -C /path/to/dir/ file_in_dir_to_archive

# the whole dir, ignore the tree
tar czfv archive.tgz -C /path/to/dir .

extract the files to dir

# tar Xtract Zip File
tar xzfv archive.tgz -C files/should/live/here

unzip to dir

unzip archive.zip -d files/should/live/here

misc

listen on $PORT with netcat

nc -lvnp 9000

serve pwd with python

python -m SimpleHTTPServer 9000

move stuff when you only have a network connection

cat my_bin | base64 | nc -v my.otherhost.com:9000