Skip to content

Instantly share code, notes, and snippets.

@mhausenblas
Created July 5, 2018 13:30
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mhausenblas/596df379da3afbb5a807aaf1ce2cdd67 to your computer and use it in GitHub Desktop.
Save mhausenblas/596df379da3afbb5a807aaf1ce2cdd67 to your computer and use it in GitHub Desktop.
Michael's bash snippets

See also:

Basics

File descriptors

The bash has three default file descriptors for each process:

  • 0 ... stdin
  • 1 ... stdout
  • 2 ... stderr

Assuming the terminal is /dev/tty0 then all three point to it on process launch.

command 1> file # redirect stdout
command 2> file # redirect stderr
command &> file  # redirect both stdout and stderr
command >file 2>&1 # same as above, mor idiomatic
command > /dev/null # discard output
command < file # redirect stdin
cmd1 | cmd2 | cmd3 # pipe commands, connect stdout of one with stdin of next
echo ${PIPESTATUS[@]} # show exit codes of each piped command

See also: http://www.catonmat.net/blog/bash-one-liners-explained-part-three/

File info

stat
type
which

Sys info

cat /etc/*-release
cat /proc/version
cat /proc/cpuinfo
cat /proc/meminfo
cat /proc/diskstats
uname -a
ulimit -n
free -t -m
dmidecode -t

Query and change the system clock (NTP)

timedatectl status

Check system services

chkconfig

Date and timestamps

date +"%Y-%m-%d"               # e.g.: 2015-12-31
date +%s                       # Unix epoch
date -u +"%Y-%m-%dT%H:%M:%SZ"  # ISO 8601 timestamp UTC
date +%FT%TZ                   # ISO 8601 timestamp local time

Changes between files

diff -u warden.conf warden.conf.old

List users

cat /etc/passwd | cut -d ":" -f1

Hex editor

xxd -l $LINES

History

Reverse search: Ctrl+R

Cancel search: Ctrl+G

Show 10 entries:

history 10

Repeats the previous command (and replace bits):

!!:s/foo/bar

Memory stats

free -th

Disk usage and stats

Render disk usage (in a human readable form) for given dir:

du -h opt/

Render free disk space (in a human readable form):

df -h

Display continuous device report at 2s intervals:

iostat -d 2

List shared libs/dynamically linked libs

Linux:

ldd

macOS:

otool -L

Display real-time view of a running system for USER

top -U $USER

Processes

ps -e | grep python
ps faux

Record shell interactions

script

List kernel modules under Linux

lsmod

Networking

https://dougvitale.wordpress.com/2011/12/21/deprecated-linux-networking-commands-and-their-replacements/

Print node's hostname

hostname --fqdn

View DNS traffic

sudo tcpdump -i any port 53

List network settings

Modern:

ip a
ip a show dev eth0

Outdated:

ifconfig -a
ifconfig eth0

Open connections to port X

lsof -i TCP:X

List all listening TCP sockets w/o DNS lookup

sudo ss -lptn
lsof -n -i4TCP

Port scan

Linux:

ss -nl

macOS/outdated:

netstat -nl | grep PORT

Show routing tables

Linux:

ip route

macOS/outdated:

netstat -nr

Dump traffic on a network

tcpdump

Traffic control

tc -d -s -p qdisc show

NFS statistics

nfsstat

File and exec manipulation

Substitutions

Lowercases and replaces underscores with dots:

echo 'Com_Acme_Library' | tr '_A-Z' '.a-z'

Replaces foo with quux:

cat 'foo bar baz' | sed -e 's/foo/quux/'

Loops

for i in $(cat input.txt); do echo $i; done;

Run command on every line

cat input.txt | xargs -n1 sha1sum

Directly write into file

cat << 'EOF' > $FILENAME
$LINE1
$LINE2
EOF

Interactively write into FILE

cat > $FILE
# CTRL+D to save

Generate file with zeros

dd if=/dev/zero of=output.dat bs=1024 count=1000 # creates a 1MB file (1000x1kB blocks)

Generate file with random content

strings /dev/urandom | tr -c -d '0-9' | dd of=randnum.dat bs=1 count=1M

Count files & dirs

ls -l opt/ |  wc -l

List files in dir (recursively) that match pattern

find opt/ -name ".txt"

Find stuff and print filename where it is contained

find . -type f -exec grep -H FINDME {} \;

Reference for tmux

Setup and config: https://github.com/mhausenblas/mhausenblas-config/blob/master/home/.tmux.conf

: new -s XXX … create a new session XXX
: kill-session -t XXX … kill session XXX
$ … rename session
L … toggle sessions
s … list sessions
" … split pane horizontally
z … full screen toggle pane
x … kill pane
Spacebar … toggle layout
arrows … switch panes
? … shortcuts
d … detach

Git

Latest commit

git rev-parse HEAD

Create tag/sync

git tag TAG HASH git push origin TAGNAME … sync

Java JAR handling

Get RPM

wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/7u67-b01/jdk-7u67-linux-x64.rpm
curl -L -C - -b "oraclelicense=accept-securebackup-cookie" -O http://download.oracle.com/otn-pub/java/jdk/7u67-b01/jdk-7u67-linux-x64.rpm

1. Create MANIFEST

echo 'Main-Class: Test' > mymanifest

2. Create JAR

jar -cfvm mytest.jar mymanifest Test.class

3. Execute JAR

java -jar mytest.jar

Find packages in jar

grep -ri "org.openanzo.client.jena" *

Recipes

Collection of recipes.

Generate public from private SSH key

ssh-keygen -y -f /path/to/privatekey > /path/to/publickey

Monitor log files

tail -f abc.log
less +F abc.log # hit SHIFT+F for refresh
less +/foo      # highlight term as regexp

Animated GIFs with ImageMagick

To convert a collection of PNGs into an animated GIF with an delay of 0.5s go:

convert -delay 50 *.png output.gif

See also http://www.imagemagick.org/Usage/anim_basics/

Monitoring with watch and tee

Monitor Docker state:

watch -n 3 'date >> dump_ps && docker ps | tee -a dump_ps'

See also http://tinystruggles.com/2015/03/22/bash-monitoring-tips.html

Hide all desktop icons macOS

defaults write com.apple.finder CreateDesktop -bool false && killall Finder

Simulate memory load

yes | tr \\n x | head -c 450m | grep n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment