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:

See also:

# Avoid tty session timeouts
export TMOUT=0

# SSH escape sequences
# From:
~.  - 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

# Retrieve arguments from previous command
!^      first argument
!$      last argument
!*      all arguments
!:2     second argument

!:2-3   second to third arguments
!:2-$   second to last arguments
!:2*    second to last arguments
!:2-    second to next to last arguments

!:0     the command
!!      repeat the previous line

!n      repeat command n from the history
!-n     repeat command from -n entries in the history

# 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-; 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- | sed 's/[^/]*\/\(.*\)/\1/' | sort)

# Practical networking with netcat
# Check connectivity
nc -vw 1 <ip> <port>
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

# 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
ssh user@server 'bash -s' <

# 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
awk '{data[c]=$1; c++; a+=$1} END {\
  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

# 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
ps -eLF

# List all groups available on the host
getent group

# CPU performance counters
# See
cat /proc/stat

# Argument parsing
# From
while [ "$#" -ge 1 ]; do

    case $key in
            shift # past argument
            shift # past argument
            shift # past argument
            # unknown option

Page actions: RecentChanges | PrintableView | PageHistory | EditPage