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
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)