Skip to Content

Useful Command Lines

Ubuntu specfic command-lines can be found here.

Command Lines from commandlinefu.com

Credits: All command lines in this section are from commandlinefu.com.

GeoIP Command Line to print City and Country given IP Address

geoip() { curl -s "http://www.geoiptool.com/?IP=$1" | html2text | egrep --color 'City:|IP Address:|Country:' }

Print info about loaded kernel modules

modinfo $(cut -d' ' -f1 /proc/modules) | sed '/^dep/s/$/\n/; /^file\|^desc\|^dep/!d'

Fetch the NASA Astronomical Pic of the day via command line

apod(){ local x=http://antwrp.gsfc.nasa.gov/apod/;feh $x$(curl -s ${x}astropix.html|grep -Pom1 'image/\d+/.*\.\w+');}

Compress, Encrypt and backup a filesystem to a remote host via ssh (with bandwidth throttling)

nice -n19 dump -0af - /<filesystem> -z9|gpg -e -r <gpg key id>|cstream -v 1 -t 60k|ssh <user@host> "cat > backup.img"

This command will nicely dump a filesystem to STDOUT, compress it, encrypt it with the gpg key of your choice, throttle the the data stream to 60kb/s and finally use ssh to copy the contents to an image on a remote machine.. nice -n19 is recommended as dump is a rather cpu and disk intensive process.

NOTE: I haven't tried this out yet but i like the command. I am not sure how dump works when the filesystem is active. So make sure to run some sort of sanity check on the created backup.

Print out your Public IP

curl --connect-timeout 3 http://www.whatismyip.org/

wget

Command to fetch a page

wget -l 1 -r  --wait 1 --no-host-directories --convert-links --page-requisites --nocache --no-cookies --random-wait  -o wgetlog  " <URL> "

Command to check validity of bookmarks

wget --spider --force-html -i bookmarks.html

lsof

Listing all open files belonging to a directory

Usecase: I wanted to move the .google folder to a different location for space reasons and so had to make sure that none of the files in the directory were being used.

lsof +D .google

find

Command to find files greater than 50M and write the output to tmp file.

sudo find ./ -size +50M -fprintf /tmp/outfile '%-20s %p\n'

Use this version if you do not want find to look at mounted filesystems

sudo find ./ -mount -size +50M -fprintf /tmp/outfile '%-20s %p\n'

nc

Server Fuzzing

cat /dev/random | nc <serverIP>

Bash Shell One Liners

Instantaneous Download Bandwidth calculation

oldbytes=0; while [ 1 ]; do bytes=`ifconfig eth1 | grep "RX bytes" | awk '{print $2}' | awk 'BEGIN {FS=":"} {print $2}'`; bw=`expr $bytes - $oldbytes`; bw=`expr $bw \* 8`; bw=`expr $bw / 1024`; oldbytes=$bytes; sleep 1; echo "$bw Kb/s";  done

gnuplot

Coolest reference for gnuplot can be found here.

Binomial distribution

n=15; p=0.4
set samples n+1;
set xrange [0:n]
i(x)=int(x+.1)
bd(x)=n!/(i(x)!*(n-i(x))!)*p**x*(1-p)**(n-x)
plot bd(x) title 'binomial distribution, p=0.4' with linespoints

Poisson Distribution

mu=5; n=15; p=0.4
set samples n+1;
set xrange [0:n]
i(x)=int(x+.1)
pd(x)=mu**x*exp(-mu)/i(x)!
plot pd(x) title 'Poisson distribution, mu=5' with impulses

Plotting smooth curves with mutiple data columns, with date labels and outputting gif

set out "outfile.gif"
set term gif small size 1024,800
set xdata time
set timefmt "%m/%d/%y"
plot 'test.dat' using 1:6 smooth bezier title "Rise and fall of gas in 2008" w lines

Download test.dat and sample output outfile.gif.

PGP Commands

Generate the keys

gpg --gen-key

Exporting Public Key

gpg --armor --export <Your Email> > mypk

Encrypting files

gpg --out outfile --encrypt filename

Decrypting Files

gpg --output decryptedfile --decrypt file

xrandr - changing display properties

xrandr tutorial for Ubuntu is here.

To display the currently connected displays along with available resolutions

xrandr

Changing the display resolution(to 1440x900) and refresh rate (to 59.9) of the VGA connected display

xrandr --output VGA --mode 1440x900 --rate 59.9

ssh - secure shell

Port forwarding using ssh

ssh -i <identity_file> -f <username@host> -L <local_port_to_forward>:<remote_host>:<remote_port> -N

X11 over SSH

ssh -X -f <username@host>

convert - commandline program to operate on image files

The official documentation is located at the ImageMagick site here

Resizing an image given maximum dimensions and preserving the aspect ratio

convert <original_image_filename> -strip -resize 192x192 <resized_image_filename>

Resizing an image to exact dimensions (with loss of aspect-ratio)

Usecase: Some websites ask you to load your photos with EXACT dimensions (say 192x192) .

convert <original_image_filename> -strip -resize 192x192! <resized_image_filename>

    Notes
  • The change from the previous -resize option is the use of ! mark which tells convert to not care about the aspect-ratio. There are other cool options in the documentation referenced above.
  • The -strip option strips the image of its metadata.