Commandline-fu

Derping around a GUI is all fine and good for many things but sometimes you really just need to get things done and your GUI just won’t cut it. Having a powerful command line interface and being knowing how to use it are critical skills. I’ve recently found myself having to use the command line a bit more than usual, so below are some of the learned or re-learned tidbits of knowledge that might help others.

Note: I’m a Windows developer, but the commands below all use bash in Cygwin.

Q: How can I generate a file of an arbitrary size with random data?

A: Use dd in conjunction with /dev/urandom

# 2 Megs
dd if=/dev/urandom of=a.log bs=1M count=2

# 2 Gigs (1 Meg * 2 * 1024)
dd if=/dev/urandom of=a.log bs=1M count=2K 
Note that 1kB = 1000 and 1K = 1024, 1MB = 1000 * 1000 while 1M =1024 * 1024, and so on.
Credit for this protip goes to the Linux Commando blog: http://linuxcommando.blogspot.com/2008/06/create-file-of-given-size-with-random.html

 

Q: How can I sort a file, remove duplicate elements, and do it all in-place?

A: sort temp.txt -o temp.txt

Q: How can I pipe the output from foo.exe to bar.exe if bar.exe does not accept input from stdin?

A: The obvious answer is to redirect stdout to a file then use the file as an input parameter.
(Note: md5sum *does* accept stdin input. Ignore that for the sake of the example)

echo -n hello > temp.dat
md5sum temp.dat
rm temp.dat

The command below can skip the intermediate step.

md5sum <(echo -n hello)
I wasn’t familiar with this syntax till recently. I discovered it in this bash redirections cheat sheet: http://www.catonmat.net/download/bash-redirections-cheat-sheet.pdf

Leave a Reply

Your email address will not be published. Required fields are marked *