38 Comments

5 Unix Commands I Wish I’d Discovered Years Earlier

I’ve been using *nix systems for quite a while. But there are a few commands that I somehow overlooked and I wish I’d discovered years earlier.

1. man ascii

This prints out the ascii tables in octal, hexadeciamal and decimal. I can’t believe I didn’t know about this one until a month ago. I’d always resorted to googling for the tables. This is much more convenient.

ASCII(7)           BSD Miscellaneous Information Manual           ASCII(7)
 
NAME
    ascii -- octal, hexadecimal and decimal ASCII character sets
 
DESCRIPTION
    The octal set:
 
    000 nul  001 soh  002 stx  003 etx  004 eot  005 enq  006 ack  007 bel
    010 bs   011 ht   012 nl   013 vt   014 np   015 cr   016 so   017 si
    020 dle  021 dc1  022 dc2  023 dc3  024 dc4  025 nak  026 syn  027 etb
    030 can  031 em   032 sub  033 esc  034 fs   035 gs   036 rs   037 us

For more information, see the ascii man page.

2. cal

Pulling up a calendar on most systems is almost always a multi-step process by default. Or you can just use the cal command.

> cal
 
    August 2013
Su Mo Tu We Th Fr Sa
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

For more information, see the cal man page.

3. xxd

> xxd somefile.bin
 
0000000: 83ff 0010 8d01 0408 d301 0408 a540 0408  .............@..
0000010: d701 0408 d901 0408 db01 0408 0000 0000  ................
0000020: 0000 0000 0000 0000 0000 0000 1199 0508  ................
0000030: df01 0408 0000 0000 e199 0508 1d9a 0508  ................
0000040: e501 0408 2912 0508 e901 0408 eb01 0408  ....)...........
0000050: ed01 0408 ef01 0408 39e0 0408 55e0 0408  ........9...U...
0000060: 71e0 0408 8de0 0408 a9e0 0408 39f7 0408  q...........9...
0000070: 6df7 0408 a5f7 0408 ddf7 0408 15f8 0408  m...............
0000080: 49f8 0408 81f8 0408 7de5 0408 0b02 0408  I.......}.......
0000090: 4ded 0408 a9ed 0408 1102 0408 c5e0 0408  M...............
00000a0: 1502 0408 1702 0408 1902 0408 1b02 0408  ................
00000b0: e50a 0508 1d0b 0508 590b 0508 2302 0408  ........Y...#...
00000c0: 2502 0408 253d 0508 2941 0508 7106 0508  %...%=..)A..q...
00000d0: 8106 0508 690e 0508 990e 0508 c90e 0508  ....i...........
00000e0: 19e1 0408 3702 0408 3902 0408 3b02 0408  ....7...9...;...

This is another command I can’t believe I didn’t know about until recently. xxd can generate a hex dump of a given file, and also convert an edited hex dump back into its original binary form. It can also output the hex dump as a C array which is also super handy:

> xxd -i data.bin
 
unsigned char data_bin[] = {
  0x6d, 0x61, 0x64, 0x65, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6c, 0x6f, 0x6f,
  0x6b, 0x0a
};
unsigned int data_bin_len = 14;

I’ve also used it to compare binary files by generating a hex dump of two files and then diff’ing them. For more information, see the xxd man page.

4. ssh

ssh was one of the first non-trivial unix utilites that I got familiar with, but it was a while before I realized that it can be used for a lot more than just logging into remote machines.

ssh and its accompanying tools can be used for:

  • Copying files between computers (using scp)
  • X-forwarding – connect to a remote machine and have any gui applications started, displayed as if they were started locally, even if the remote machine doesn’t have an X server.
  • Port forwarding – forward a connection to a local port to a port on a remote machine OR forward connections to a port on a remote machine to a local port
  • SOCKS proxy – allows you forward any connections of an application that supports SOCKS proxies through the remote host. Useful for more secure browsing over public wifi and for getting around overly restrictive firewalls.
  • typing a password on your local machine once, then using a secure identity to login to several remote machines without having to retype your password by using an ssh key agent. This is awesome.

For more information, see the ssh man page.

5. mdfind

This one is specific to mac, as there are other *nix equivalents. It has similar functionality to find but uses the Spotlight index. It allows you to search your entire filesystem in seconds. You can also use it to give you live updates when new files that match your query appear. I use it most often when I’m trying to find the obscure location that an application stores some critical file.

> mdfind -name homebrew
 
/usr/local/Library/Homebrew
/Users/job/Library/Logs/Homebrew

Fore more information, see the mdfind man page