Post

I have a list of command line tricks that have proven to be useful, fast, or just curious. Here they are:

* Compare a remote file with a local file:

ssh user@host cat /path/to/remotefile | diff /path/to/localfile -

* Serve current directory tree at http://HOSTNAME:8000/

python -m SimpleHTTPServer
* Compile with colorized output, multicore CPU support, and low CPU priority:
nice -n 19 colormake -j 3

* Colorized diff:

colordiff

* Guess your current public IP:

curl -s http://checkip.dyndns.org/ | grep -o \" [[:digit:].]\+\" "

* Copy *.h, *.c and *.cpp to other host for debugging:

rsync -a --cvs-exclude --progress --exclude='.*/' --include='*/' --include='*.c'
      --include='*.h' --include='*.cpp' --exclude='*' SOURCE DEST

* Make a backup of a directory

tar -cjvvf backup$(date +%Y%m%d_%H%M%S).tar.bz2

* Generate a list of random IPs:

nmap -n -iR 0 -sL | cut -d" " -f 2

* Benchmark an HTTP server:

ab -n 9000 -c 900 localhost:8080/index.php

* Display ncurses based network monitor:

nload -u m eth0
ethstatus -i eth0
ifstat -nt

* The same but by process:

nethogs -p eth0
ss -p
lsof -P -i -n

* SSH connection through host in the middle:

ssh -t reachable_host ssh unreachable_host

* Create a persistent connection to a machine

ssh -MNf user@host

* Save a file as root when VIM was launched as another user:

:w !sudo tee %

* A better traceroute:

mtr google.com

* Copy SSH key to remote host, in order to enable login without password:

ssh-copy-id user@host

* Create a partition which resides in memory, so it has ultra-fast read/write:

mount -t tmpfs tmpfs /mnt -o size=1024m

* Like top, but for files:

watch -d -n 2 ‘df; ls -FlAt;’

* Make a backup of a file:

cp /long/path/to/filename{,.bak}

* Analyze traffic on a remote machine with Wireshark

ssh root@server.com ‘tshark -f “port !22″ -w -‘ | wireshark -k -i –

* Pipe stdout, stderr… etc. to separate commands:

some_command > >(/bin/cmd_for_stdout) 2> >(/bin/cmd_for_stderr)

* Execute a command with a timeout:

timeout 10 sleep 11

* Colored svn diff:

svn diff | vim -R –

* Hierarchic list of processes:

ps -e -o pid,args –forest

* Close shell keeping subprocesses running

disown -a && exit

* Open a file in a new, full screen, Emacs window:

function emw() { emacsclient -c -a ” -e “(progn (toggle-fullscreen) (find-file \”$*\”))”; }

* Display a one line description of one Wikipedia article:

function wikipedia() { dig +short txt ${1}.wp.dg.cx; }

* Search http://www.commandlinefu.com for the tricks that contain a command:

function cmdfu(){ curl “http://www.commandlinefu.com/commands/matching/$@/$(echo -n $@ | openssl base64)/plaintext”; }

* Backup Del.icio.us bookmarks:

wget –user=username –password=password https://api.del.icio.us/v1/posts/all -O bookmarks.xml

* Quick find files in a directory tree:

function ff() { find . -type f -iname ‘*’$1’*’ $2 $3 $4 $5 $6 $7 $8 $9 ; }

* Update the twitter status:

function twit() { curl -u myusername -d status=”$*” http://twitter.com/statuses/update.xml ; }

* Funtion that moves files to a recicle-bin folder instead of deleting them:

function trash()
{
if [ -z “$*” ] ; then
echo “Usage: trash filename”
else
DATE=$( date +%F )
[ -d “${HOME}/.Trash/${DATE}” ] || mkdir -p ${HOME}/.Trash/${DATE} for FILE in $@ ; do
mv “${FILE}” “${HOME}/.Trash/${DATE}”
echo “${FILE} trashed!”
done
fi
}

* Function that extracts the contents of a compressed archive, independently of it’s format:

function extract()
{
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo “‘$1’ cannot be extracted via >extract<” ;; esac
else
echo “‘$1’ is not a valid file”
fi
}

* The Matrix:
tr -c ” [:digit:]” ” ” < /dev/urandom | dd cbs=$COLUMNS conv=unblock | GREP_COLOR=”1;32″ grep –color “[^ ]”

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s