Homepage

Read Fullscreen

2 shell tricks

1) Empty file of any size (useful for testing things, e.g. uploads of various sizes):

Did some googling a came up with using ‘dd’… it’s long, hard to remember, and a bit hard to read. For example, if you want a junk ~50mb file:

$ dd if=/dev/random of=./fifty.junk bs=1024 count=50000

that says ‘in blocks of 1024 bytes (1kb, the Block Size) read 50,000 blocks from /dev/random (the In File) and write them to ./fifty.junk’ (the Out File). It works, but a bit unruly. I just wanted to say ‘make a 50mb file.’ So as I was about to write a script to wrap dd named ‘mkfile’ and discovered this:

$ mkfile 50m fifty.junk

Perfect. Size suffixes make sense (b,k,m,g) and very easy to remember. It isn’t installed by default on my Linux (Ubuntu) but it’s on my Mac, so I’d guess it’s in BSD as well. 

2) Bash Math

bc is a simple calculator (Bash Calculator I assume). You can pipe stuff into it. 

e.g. $ echo ‘(20 * 45) / 7’ | bc

will print 128

you could do it with ruby too (or a myriad of other scripting languages), e.g.

$ ruby -e ‘puts (20 * 45) / 7’

will also print 128.

That’s all too much typing. So I wrapped bc in a script called ‘calc’. Now I can just go

$ calc 20*45/7 

and also have 128 printed. Handy. (If anyone knows of an existing just-as-simple solution like mkfile was, please share).

Here it is, copy it to a file called ‘calc’ (no file extension) somewhere in your path (like /usr/local/bin) and make it executable (chmod +x calc)

#!/bin/sh

join=""

for arg in $@; do join=”$join $arg”;done

echo `echo $join | bc`

Enjoy!