6 More Terminal Commands You Should Know

Take a stroll through /usr/bin on most Linux or macOS systems and you’ll find hundreds if not thousands of available commands. The amount of options at your disposal seems dizzying. Although some of these commands won’t be the most relevant or useful every day, the majority of them are quite handy and some are just downright clever.

If we count the number of commands available on a fresh installation of Debian 10 (Buster) it is over 2,000 (you can run compgen -c to see the list for yourself). It would take you years to comb through that many options and master them all. Luckily, you don’t have to do that.

In this article (a sequel to 6 Terminal Commands You Should Know) we’ll look at a set of six more interesting and useful commands that will make you a productivity master when you add them to your repertoire.

Let’s start exploring a few!

1. Compare differences in files with cmp

cmp <file1> <file2>

Let’s assume you have two text files. In each file are some simple lines of text like this:

file1.txt           file2.txt
--------           --------
line1              line1
line2              line2
line4              line3

If you didn’t want to open each file up and examine it by looking for lines that weren’t the same, you could use cmp to quickly determine any divergence in the files.

If you pass cmp file1.txt file2.txt you’ll see the following output:

file1.txt file2.txt differ: char 17, line 3

This quickly shows you exactly where the two files diverged by identifying the line and the character number. This utility is super handy for making on-the-fly file comparisons. You could check the basic validity of a copy job by verifying files were the same or hunt down bugs in code by comparing some source files to see where they differ.

2. Find lines common in files with comm

comm <file1> <file2>

On the flip side, instead of finding differing lines like cmp the comm utility scans for similar lines. With this command, you can identify lines that are common to two files.

Let’s use the same two files from the previous example and see how this utility works:

file1.txt           file2.txt
--------           --------
line1              line1
line2              line2
line4              line3

Now if we run comm file1.txt file2.txt we should see the following output:


This output can be a little confusing at first, but all its showing you is three specific columns of results:

  • The first column contains lines present only from the first input file
  • The second column only lines from the second input file
  • The last column lines common to both files

With this utility, we can quickly identify files containing identical lines. This could be useful for finding variables defined in multiple files or hunting for duplicate entries in some text output.

If you need case insensitivity you can also pass the -i flag to comm.

3. Preload trusted SSH keys using ssh-keyscan

ssh-keyscan <hostname> >> ~/.ssh/known_hosts

How many times have you connected to a new machine and received the generic warning:

"The authenticity of host <foo> can't be established.Are you sure you want to continue connecting? (yes/no)?"

If you’re connecting to hosts that you know already to be authentic (from an internal network or a publicly available service like GitHub) then this message isn’t protecting you from much.

Most of the time users simply type “yes” faster than you can say “security vulnerability” anyway. So is there any way to reduce how many times we see this message if we connect to a lot of hosts? Yes!

Using ssh-keyscan you can scan a particular host or a list of hosts for public SSH keys and add them to your known_hosts file. Now when you connect to that host you won’t be prompted because the key will already exist in your known_hosts file. For example, let’s say you wanted to scan GitLab for keys and automatically add them:

ssh-keyscan gitlab.com >> ~/.ssh/known_hosts

Now you should be able to SSH to public GitLab hosts without being prompted to add the keys before proceeding.

4. Record a log of your terminal session with script

Have you ever been working on something complex or frustrating in a terminal session and wished you could automatically save all the output for later? The script command lets you do exactly that.

This simple program was originally designed for students to save logs of all their work in order to show how they arrived at a solution. The usefulness, however, doesn’t stop in academia.

I’ve used this utility to save logs during sensitive operations where I had to be doubly sure I ran a particular command. This has also been implemented as a rudimentary way to log specific user sessions for security reviews later.

5. Generate test data with jot

jot <number>

Need a lot of random or sequential data really fast? Use jot. This nifty and ultra-simple utility lets you generate a huge list of numbers and create some pretty large data output.

With jot you can build sequential sets of numbers, strings, and bytes or just spew random data. The following command will generate a list of sequential numbers from 1–100:

jot 1001

See how easy that was? Now, what about some random numbers?

jot -r 10019

How about a few big long strings?

jot -s abc 1001abc2abc3abc4abc5abc6abc7abc8abc9abc10abc11abc12abc13abc14abc15abc16abc17abc18abc19abc20abc21abc22abc23abc24abc25abc26abc27abc28abc29abc30abc31abc32abc33abc34abc35abc36abc37abc38abc39abc40abc41abc42abc43abc44abc45abc46abc47abc48abc49abc50abc51abc52abc53abc54abc55abc56abc57abc58abc59abc60abc61abc62abc63abc64abc65abc66abc67abc68abc69abc70abc71abc72abc73abc74abc75abc76abc77abc78abc79abc80abc81abc82abc83abc84abc85abc86abc87abc88abc89abc90abc91abc92abc93abc94abc95abc96abc97abc98abc99abc100

This command is fast, simple, and way easier than anything else I’ve used to quickly generate simple test data.

6. Quick reference for networking with iptab


This is one I cannot believe I overlooked. Its brutally simple but incredibly helpful when you’re in a rush trying to make networking changes. Firing off the iptab command with no arguments will display a reference table of subnet masks and other IP address space details:

| addrs   bits   pref   class  mask            |
|     1      0    /32 |
|     2      1    /31 |
|     4      2    /30 |
|     8      3    /29 |
|    16      4    /28 |
|    32      5    /27 |
|    64      6    /26 |
|   128      7    /25 |
|   256      8    /24      1C   |
|   512      9    /23      2C   |
|    1K     10    /22      4C   |
|    2K     11    /21      8C   |
|    4K     12    /20     16C   |
|    8K     13    /19     32C   |
|   16K     14    /18     64C   |
|   32K     15    /17    128C   |
|   64K     16    /16      1B     |
|  128K     17    /15      2B     |
|  256K     18    /14      4B     |
|  512K     19    /13      8B     |
|    1M     20    /12     16B     |
|    2M     21    /11     32B     |
|    4M     22    /10     64B     |
|    8M     23     /9    128B     |
|   16M     24     /8      1A       |
|   32M     25     /7      2A       |
|   64M     26     /6      4A       |
|  128M     27     /5      8A       |
|  256M     28     /4     16A       |
|  512M     29     /3     32A       |
| 1024M     30     /2     64A       |
| 2048M     31     /1    128A       |
| 4096M     32     /0    256A         |

There are no arguments for this command, just a simple table for you to reference and then get back to making those critical network changes.

That’s it for this one — thanks for reading!

By: Tate Galbraith


發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *