Table of Contents generated with DocToc

System administration

Find PID using a given port number

lsof -i :6234

Find disk usage per directory

find . -maxdepth 1 -type d -exec du -sh {} \;

or

find . -maxdepth 1 -type d | xargs du -sh

Delete all files more than x days old

find /path/to/directory/ -mindepth 1 -mtime +5 -delete

Delete everything except files with given extension

$ find . ! -name "*.jar" | xargs rm -rf

Measure response time with curl

$ curl -o /dev/null -s -w %{time_total} http://www.example.com/

Utilities

SSL Keys

Convert .p12 to .key

openssl pkcs12 -in private_key.p12 -nodes -out private.key -nocerts

Convert .crt to .pem

openssl x509 -in STAR_quizalize_com.crt -out STAR_quizalize_com.pem -outform PEM

SSH tunnel - forward traffic from a specific localhost port to a specific remote host and port

ssh -L <localhost_port>:<remote_host>:<remote_port> <username>@<tunnel_server>

SSH tunnel - forward traffic from a specific localhost port to remote host - can also be used to setup SOCS proxy

ssh -D <localhost_port> -C -q -N <username>@<tunnel_server>

Download file using curl

$ curl -O <url>

Upload file using curl

$ curl --verbose -F 'file=@"/tmp/myFile.txt"' http://localhost:3000/api/myFileEndpoint

Rename all files in a directory with sequential numbers

a=1
for i in *.jpg; do
  new=$(printf "erg-%02d.jpg" "$a") #04 pad to length of 4
  mv -i -- "$i" "$new"
  let a=a+1
done

Simple load testing shell script

Measures time in seconds

#!/bin/bash

echo "fastlyTime,awsGatewayTime"

while true
do
  fastlyTime=`curl -o /dev/null -s -w %{time_total} -H 'X-Api-Key: bla' https://round-trippa.ft.com/callback`
  sleep 1
  awsGatewayTime=`curl -o /dev/null -s -w %{time_total} -H 'X-Api-Key: bla' https://round-trippa-gw-eu-west-1-prod.memb.ft.com/callback`
  sleep 1
  echo "${fastlyTime},${awsGatewayTime}" >> responsetimes.log
done

Sort file in a directory by name and print size and file name

ls -ltr | sort -k 9 | awk '{print $5":"$9}'

CentOS

Manage network interfaces

$ nmtui

Ubuntu

mkdir $HOME/Shared
/usr/bin/vmhgfs-fuse -o auto_unmount .host:/ $HOME/Shared

Switch between apt-get installed java versions

sudo update-alternatives --config java

AWS CLI

S3 copy/download

aws s3 cp s3://<bucket-name>/<path-to-file> /tmp/. --region=eu-central-1

S3 upload object

aws s3 cp /Users/anuragkapur/Desktop/hello-world.txt s3://<bucket-name>/ --profile <cli-credentials-profile-name>

EB get saved config

eb config get NAME

Cloud Watch - Describe Subscription Filters

aws logs describe-subscription-filters --log-group-name myLogGroup --region ap-southeast-1

Cloud Watch - Update Subscription Filter

aws logs put-subscription-filter --log-group-name myLogGroup --filter-name myFilterName --filter-pattern "{($.userId = *) || ($.data[0].userId = * )}" --destination-arn destinationArn --region eu-central-1