Skip to content
Andres Olarte edited this page Sep 18, 2024 · 26 revisions

Bash

Logger

Send message to syslog

logger Test Message

Multi line variables (heredoc)

read -r -d '' MY_VAR << EOM
Line 1
Line 2
Line x
EOM

If the delimiter is quotes ("EOM") on the first line. No variable substitution will happen.

Multi line files (heredoc)

cat > init_spinnaker.sh << EOM
Line 1
Line 2      
EOM

Multi line files (heredoc) with sudo

cat << EOM | sudo tee init_spinnaker.sh
Line 1
Line 2      
EOM

Pipes

mkfifo my_pipe
cat < my_pipe > file.out
echo "xxx" > my_pipe

Paste

Multiple lines to a single line

Where -d is the separator:

find * | grep "\.java" | paste -d" "  -s -

Tmux

  • Split Horizontally: Ctrl + b + "
  • Split Vertically: Ctrl + b + %
  • Move to panel: Ctrl + b + arrow
  • Synchronize all panes: Ctrl + b :setw synchronize-panes [on|off]
  • Detach Session: Ctrl + b + d
  • Reattach Session: tmux attach -t 0
  • List Sessions: tmux ls
  • New Window: Ctrl + b + c
  • Next Window: Ctrl + b + n
  • Previous Window: Ctrl + b + p
  • Rename Window: Ctrl + b + ,
  • Cycle through layouts: Ctrl + b + space

Curl

Post JSON (inline)

curl -i -X POST -H 'content-type: application/json' -d '{"VALUE": "key"}' http://localhost:8080/endpoint

Post JSON (file)

curl -i -X POST -H 'content-type: application/json' -T request.json http://localhost:8080/endpoint

Hit a particular IP

curl -O --resolve myapi.com:443:10.10.10.10 -vv https://myapi.com/myfile.tar.gz 

Disk Usage - du

To show all directories size including sub directories, type

du -h

To calculate the current directory size you are in (-s stand for summary)

du -sh

To show all the 1 level sub directories size (which you are not interested at sub sub directories.)

du -sh *

To show the size of specific directory

du -sh /home

To show the size of all sub directories of a specific directory

du -sh /home/*

Find large files

find /wwwroot -type f -size +200000k -exec ls -lh {} \;

Grep

Find non ascii-chars

grep -P -n "[\x80-\xFF]" -R *

In Mac, use ggrep from homebrew

Networking

netcat

Forward privileged port

Linux:

mkfifo fifo
nc -l -p 80 <fifo | nc localhost 4000 >fifo

Mac:

mkfifo fifo
nc -l 80 <fifo | nc localhost 4000 >fifo

netstat

Show open ports netstat -nap | grep LISTEN

DNS

Use a particular name server:

dig @192.168.1.1 www.yahoo.com

nslookup www.yahoo.com - 192.168.1.1

SSH

Tunnel:

ssh host -L 91521:destintaion:1521

TAR

Extract single file

tar -x ./filename/to/extract -f tarfile.tar

xargs

To execute a command with an argument line by line. This will run command with an argument of each line from result of awk.

grep "409" error.log | awk '{print $9}' | xargs -L 1 command

Zsh

Config is in ~/.zshrc

To show path in prompt:

export PS1="[%~]$ "

To update terminal title in macOS:

source /etc/zshrc_Apple_Terminal
Clone this wiki locally