Manuel Amago: CommandLineFoo

Making life taste better…


Page actions: RecentChanges | PrintableView | PageHistory | EditPage



Command Line Foo

A collection of handy tips and tricks for the Unix command line.

Good reference: http://www.commandlinefu.com/

See also:


 
# Avoid tty session timeouts
export TMOUT=0

# SSH escape sequences
# From: https://lonesysadmin.net/2011/11/08/ssh-escape-sequences-aka-kill-dead-ssh-sessions/
~.  - terminate connection (and any multiplexed sessions)
~B  - send a BREAK to the remote system
~C  - open a command line
~R  - Request rekey (SSH protocol 2 only)
~^Z - suspend ssh
~#  - list forwarded connections
~&  - background ssh (when waiting for connections to terminate)
~?  - this message
~~  - send the escape character by typing it twice

# Find out Linux release version
uname -a
lsb_release -a
cat /etc/redhat-release
cat /etc/lsb-release

# Rerun previous command with sudo in front
sudo !!

# Disable <CTRL-d> which is used to logout of a login shell
# (local or remote login session over ssh).
set -o ignoreeof

# Compare directories
diff --suppress-common-lines -y \
  <(cd liquidity-orchestrator-7.1.10.1; find . | sort) \
  <(cd ../liquidity-orchestrator-8.0.0-SNAPSHOT; find . | sort)

# Compare tarballs
diff --suppress-common-lines -y \
  <(tar tzf liquidity-orchestrator-8.0.0.tgz | sed 's/[^/]*\/\(.*\)/\1/' | sort) \
  <(tar tzf liquidity-orchestrator-7.1.11.0.1.tgz | sed 's/[^/]*\/\(.*\)/\1/' | sort)

# Practical networking with netcat
#   http://aplawrence.com/Girish/nc.html
# Check connectivity
nc -vw 1 <ip> <port>
  or
nc -z <ip> <port>
# Bash only
(echo > /dev/tcp/x.x.x.x/443) >/dev/null 2>&1 && echo "It's up" || echo "It's down"
# Listen on a port
nc -l <port>
# Listen on a port - keep connection listening
nc -lk <port>

# Find out which process is using a given port
lsof -i :<port number>

# Find large files
find . -type f -size +50000k -exec ls -lh {} \;

# Disk quotas
quota -s
du -h --apparent-size

# bash lookup maps
declare -A property_names
property_names["liquidity-connector-reuters-mapi"]="reutersmapi"
property_names["liquidity-connector-gain-gtx"]="gaingtx"
property_names["liquidity-connector-fxcmpro"]="currenex"
property_names["liquidity-connector-cme-fix"]="cme"
property_names["mda-fxall-iex"]="fxall"

# Checking for CPU power saving settings\
# (AKA green or tree hugging settings)
watch -n 1 'for f in /proc/acpi/processor/*; do grep active $f/power;done'
watch -n 1 'for f in /proc/acpi/processor/*; do grep active $f/power;done | grep -v C1'
for i in /proc/acpi/processor/*; do echo $i; more $i/* | \
  egrep "active state|power management|throttling control"; done

# Turn off VI auto-indent on pasting
:set paste
(optional: after pasting) :set nopaste

# Run a script on a remote box via SSH (via http://linux.icydog.net/ssh/piping.php)
ssh user@server 'bash -s' < script.sh

# Find out cannonical path for a file
readlink -f <file>

# Handy FIX commands
cat fix.log | sed 's/\x01/|/g'

# DNS lookup commands
# standard lookup cmd
nslookup <host>
# dig is much more flexible. For a super short answer, ideal for scripting:
dig +short +search <host>

# Reverse lookup
dig +short -x <ip_addr>

# Remote Desktop access spanning multiple monitors
mstsc /span

# AWK histograms
# http://braindump.mrzesty.net/index.php?node=63
awk '{data[c]=$1; c++; a+=$1} END {\
  asort(data);\
  print "min=" data[1] ", max=" data[NR] ", median=" a/NR \
        ", 50%=" data[int(NR/2-0.5)] ", 95%=" data[int(NR*0.95-0.5)] \
        ", 99%=" data[int(NR*0.99-0.5)]}'

# Rsync args
rsync -avz $EXCLUDE_DIRECTIVES $REF_LOCATION $REMOTE_URI

# Find which processes are assigned to which CPU
ps -aeww -o pid,psr,args | sort -k 2n

# Find which process threads are assigned to which CPU
ps -aLeww -o pid,lwp,psr,args | sort -k 3n
  or
ps -eLF

# List all groups available on the host
getent group

# CPU performance counters
# See http://blog.monitorscout.com/2014/06/26/accurate-cpu-usage-measurement-on-linux/
cat /proc/stat

# Argument parsing
# From http://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash
while [ "$#" -ge 1 ]; do
    key="$1"

    case $key in
        -e|--extension)
            EXTENSION="$2"
            shift # past argument
            ;;
        -s|--searchpath)
            SEARCHPATH="$2"
            shift # past argument
            ;;
        -l|--lib)
            LIBPATH="$2"
            shift # past argument
            ;;
        --default)
            DEFAULT=YES
            ;;
        *)
            # unknown option
            ;;
    esac
    shift
done


Page actions: RecentChanges | PrintableView | PageHistory | EditPage