4 unix goodies I cannot live without

In the past few years I managed to discover some nice unix / unix-inspired utilities that are, simply, life-savers.

Not sure if you already know all the stuff here, but I hope you’ll be able to find this brief list useful!

watch

Ever needed to run a command multiple times at a certain interval? Enter watch.

(funny enough, my friend Cirpo yesterday told me “Why do everytime I have to look for a unix command I end up on a bad-looking webpage?”)

Anyhow, at its core, watch -n X cmd simply re-executes cmd every X seconds:

1
2
3
4
5
6
~  ᐅ watch -n 1 "mysql -h 127.0.0.1 -u root -proot -e 'show processlist;'"

Every 1.0s: mysql -h 127.0.0.1 -u root -proot -e 'show processlist;'                                                                                                                 Wed Jul 15 18:30:19 2015

Id      User    Host    db      Command Time    State   Info
10      root    172.17.42.1:56805       NULL    Query   0       init    show processlist

Useful when you’re waiting for something to happen!

htop

If you are familiar, and possibly frustrated with, top you will definitely love htop.

In general, just imagine top with a better user-interface and an intuitive way to sort, filter and so on.

$?

Oh Jesus, help with exit codes here!

Ever ran a program to then wondered what its exit code was? You can simply do it with a $? which will return the exit code of the last command you executed:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/tmp/test  ᐅ ls -la
total 44
drwxrwxr-x  2 odino odino  4096 Jul 15 18:40 .
drwxrwxrwt 24 root  root  40960 Jul 15 18:40 ..
/tmp/test  ᐅ echo $?
0
/tmp/test  ᐅ ls -la non-existing
ls: cannot access non-existing: No such file or directory
/tmp/test  ᐅ echo $?
2
/tmp/test  ᐅ  i-dont-exist
zsh: command not found: i-dont-exist
/tmp/test  ᐅ echo $?
127
/tmp/test  ᐅ

screen

You SSH into a server, run a command, the internet goes off…profanities all around you!

Had you used screen, this wouldn’t have happened!

With screen, you will:

Boom baby!

Bonus: mysql’s \G

A hidden gem I really like to use is the \G option when doing SELECTs, which will format returned records in a very particular way (vertically-aligned, though it’s only doable for queries that return very few results):

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
32
33
34
35
36
37
38
mysql> SELECT * FROM TABLES LIMIT 1;
+---------------+--------------------+----------------+-------------+--------+---------+------------+------------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------------+
| TABLE_CATALOG | TABLE_SCHEMA       | TABLE_NAME     | TABLE_TYPE  | ENGINE | VERSION | ROW_FORMAT | TABLE_ROWS | AVG_ROW_LENGTH | DATA_LENGTH | MAX_DATA_LENGTH | INDEX_LENGTH | DATA_FREE | AUTO_INCREMENT | CREATE_TIME         | UPDATE_TIME | CHECK_TIME | TABLE_COLLATION | CHECKSUM | CREATE_OPTIONS | TABLE_COMMENT |
+---------------+--------------------+----------------+-------------+--------+---------+------------+------------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------------+
| def           | information_schema | CHARACTER_SETS | SYSTEM VIEW | MEMORY |      10 | Fixed      |       NULL |            384 |           0 |        16434816 |            0 |         0 |           NULL | 2015-07-15 14:56:18 | NULL        | NULL       | utf8_general_ci |     NULL | max_rows=43690 |               |
+---------------+--------------------+----------------+-------------+--------+---------+------------+------------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------------+
1 row in set (0.03 sec)

mysql>
mysql>
mysql>
mysql>
mysql>
mysql>
mysql> SELECT * FROM TABLES LIMIT 1\G
*************************** 1. row ***************************
  TABLE_CATALOG: def
   TABLE_SCHEMA: information_schema
     TABLE_NAME: CHARACTER_SETS
     TABLE_TYPE: SYSTEM VIEW
         ENGINE: MEMORY
        VERSION: 10
     ROW_FORMAT: Fixed
     TABLE_ROWS: NULL
 AVG_ROW_LENGTH: 384
    DATA_LENGTH: 0
MAX_DATA_LENGTH: 16434816
   INDEX_LENGTH: 0
      DATA_FREE: 0
 AUTO_INCREMENT: NULL
    CREATE_TIME: 2015-07-15 14:56:21
    UPDATE_TIME: NULL
     CHECK_TIME: NULL
TABLE_COLLATION: utf8_general_ci
       CHECKSUM: NULL
 CREATE_OPTIONS: max_rows=43690
  TABLE_COMMENT:
1 row in set (0.04 sec)

Neat, ah?

comments powered by Disqus