Command Line Foo
A collection of handy tips and tricks for the Unix command line.
Good reference: http://www.commandlinefu.com/
See also:
- http://cfenollosa.com/misc/tricks.txt
- https://developer.atlassian.com/blog/2015/02/ten-tips-for-wonderful-bash-productivity/
- Gentoo's Bash scripting guide
# A useful prompt PS1='\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]\nλ ' # 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 # 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-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 process's threads ps -LFp <PID> # List all process's thread's affinity settings taskset -acp <PID> # List LWP affinity settings taskset -cp <LWP_ID> # Get JVM thread dump jstack <PID> # List JVM thread LWP_IDs (converting jstack's hex output to decimal along the way...) jstack <PID> | grep nid= | sed 's/\("[^"]*"\).*nid=\([^ ]*\) .*/printf "%d %s" \2 \1/e' # Check core isolation settings on host cat /proc/cmdline # 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 # Handling both stdin and arguments from functions # From https://unix.stackexchange.com/questions/301426/bash-function-that-accepts-input-from-parameter-or-pipe my_function() { if (( ${#} == 0 )) ; then while read -r __my_function ; do my_function "${__my_function}" done else target_utility "${@}" fi } # or b64decode() { if [ "$#" -ne 0 ] then printf '%s\n' "$1" else cat fi | base64 --decode echo } # or b64decode() { ( if [ "$#" -ne 0 ] then exec <<< "$1" fi base64 --decode echo ) } # 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