Comfortable command line

10–

Introduction

Most modern people working with laptop or desktop computers, be they using Apple Macs, Windows PCs or Linux PCs, prefer graphical desktops and graphical applications. I am familiar with them too: between 1994 and 2019 I used Windows, in addition to the MSDOS and Windows command line.

In 2019, I completely switched to Linux. I am quite skilled with mouse and touchpad, I have used graphical menus, browsers like Firefox and Chrome, Libreoffice for spread­sheets and documents (although I now prefer HTML text files), media player vlc, and File Managers such as nemo, pcmanfm, pcmanfm-qt, and thunar. And many other graphical programs.

However, I also still use the Unix command line, with which I am familiar already since 1985, when I got a new job with Nixdorf Computers. So yes, even in 2024 I still intensively use what some people think is a 1990 DOS Box, though in Windows it had already become a 32-bit command line. Now in Linux it is actually a terminal emulation which runs a shell, in my case bash.

Why is it hard?

What makes a command line interface hard to use? Three points:

  1. You must know command names by heart, and type them.
  2. You must know file names, and type them.
  3. You must know where in the directory tree a file resides, and navigate to that place using the cd command.

How make it easy? (1)

The second point is less hard than it seems. You don’t really need to know file names, because you can see then. The command ls (‘list sorted’) is especially made for that. It’s often used in the form ls -l, where -l asks for the long format, which includes permissions, owner, group, size, date and time.

Because ls -l is painful to type all the time, I have a shortcut alias defined for it: ll. This alias is often defined out of the box, although I sometimes encounter systems where it isn’t available. Then I wonder: why make life hard if it can be easy?

The second reason why item 2 above isn’t really much of a problem: wildcards. You have * for zero or more characters, ? for just one character, and [] for listing a number of optional characters, or one or more ranges, for example [a-zA-Z].

For those who want to know the details: note that shell file name wildcards are not exactly the same as regular expressions. But they’re similar. Perhaps confusingly similar.

How make it easy? (2)

Two factors eliminate much of the difficulty of points one and three above. As a bonus, they also greatly reduce the need to rely on the previously described ways to overcome difficulty two. These factors are:

Examples

Mounting Veracrypt containers

Sensitive data on my computer is in encrypted Veracrypt containers. To access the data, you must first mount the volumes providing passwords. Because I always fail to remember how to do that, I made a little shell script that does the work. But I also can’t remember the name of the shell script. Or I am just too lazy to learn it.

No problem with bash’s tcsh-style command line history. You just enter a few letters of the command – typing one or two is usually enough – and Arrow Up will reveal the full command, ready to execute it by pressing Enter. If there are several recent commands starting with the letters typed, you may have to press Arrow Up (↑) more than once, and perhaps Arrow Down for correction (↓) if you went too far.

rudhar@AcerOne:~$ vc↑
rudhar@AcerOne:~$ vc-mymount↵
Please enter password for Veracrypt containers

Mounting a volume is a privileged operation on most Unix and Linux systems, so after entering the password or pass phrase, the sudo password (which is that of the current user, with administrator privileges) must be entered. This sudo command is inside the script vc-mymount.

A note for the curious and somewhat skilled.

Backing up to external medium

I periodically back up important data to one of several external disk drives or SSDs. I insert the USB plug and wait until the device mounts automatically. That takes long, by the way. I just measured it: 12 seconds. Why so long? Anyway, it mounts.

I changed the labels of all the USB disks I own to ‘USB’. As a result, they all have the same mount in the Linux directory tree. But I don’t have to remember what that mount point is, and I don’t have to type it in a cd command.

Instead, I just do ncd USB, or even simpler, just nc followed by one or several times arrow up (↑) is usually also enough. That’s because this USB mounting directory is one of not even that many locations that I often ncd. The command line history mechanism shows recent commands first. So this is the sequence of commands and what is displayed:

rudhar@AcerOne:~$ nc↑↑↑
rudhar@AcerOne:~$ ncd USB
-> /media/rudhar/USB
rudhar@AcerOne:~$ ls↑
rudhar@AcerOne:/media/rudhar/USB$ ls -lt www-zip/rudha*zip
-rw-r--r-- 1 rudhar rudhar 201450287 Dec 14 16:34 www-zip/rudha1.com.zip
-rw-r--r-- 1 rudhar rudhar    133503 Dec 14 16:34 www-zip/rudhar.com.install.zip
-rw-r--r-- 1 rudhar rudhar 201450287 Dec 14 16:34 www-zip/rudhar.com.zip
-rw-r--r-- 1 rudhar rudhar    133503 Dec 14 16:32 www-zip/rudha1.com.install.zip
-rw-r--r-- 1 rudhar rudhar    133502 Dec 10 15:15 www-zip/rudha4.com.install.zip
-rw-r--r-- 1 rudhar rudhar 201447306 Dec 10 15:15 www-zip/rudha4.com.zip
-rw-r--r-- 1 rudhar rudhar    133502 Dec 10 15:14 www-zip/rudha5.com.install.zip
-rw-r--r-- 1 rudhar rudhar 201447306 Dec 10 15:14 www-zip/rudha5.com.zip
-rw-r--r-- 1 rudhar rudhar 201236939 Dec  3 12:48 www-zip/rudha2.com.zip
-rw-r--r-- 1 rudhar rudhar    133050 Dec  3 12:46 www-zip/rudha2.com.install.zip
-rw-r--r-- 1 rudhar rudhar    133050 Dec  3 12:45 www-zip/rudha3.com.install.zip
-rw-r--r-- 1 rudhar rudhar 201236939 Dec  3 12:45 www-zip/rudha3.com.zip
rudhar@AcerOne:/media/rudhar/USB$

So after arriving at the correct directory with minimal effort and memory work, I can quickly recall a rather long and complicated ls command, because just to look around what’s there, elsewhere I usually use the shortcut alias ll, which is recorded in the command history separately from any ls commands I have recently used.

From this list of backup zips of the local copy of my website (in reality there are more), I can see which ones are the oldest, so they are eligible to overwrite with a more recent backup, using a shell script that is off-topic here. I described this backup mechanism already in 2012, then still with the Windows command line. I abandoned Windows for Linux in 2019. I can’t remember if I already used wcd alias ncd back then. A Windows version of it does exist.

Now comes the magic: I can recall this command, move the cursor back to a certain position in the command, type a different letter, here v instead of w, and then recall a command which has the string part to the left as its first part, but something else to the right.

rudhar@AcerOne:/media/rudhar/USB$
rudhar@AcerOne:/media/rudhar/USB$↑
rudhar@AcerOne:/media/rudhar/USB$ ls -lt www-zip/rudha*zip▮
←←←←←←←←←←←←←←←←←v
rudhar@AcerOne:/media/rudhar/USB$ ls -lt vwww-zip/rudha*zip
rudhar@AcerOne:/media/rudhar/USB$ ls -lt vwww-zip/rudha*zip↑
rudhar@AcerOne:/media/rudhar/USB$ ls -lt v*/R*il.hc

(Instead of 17 times arrow left, I could also have type Home, followed by 7 times arrow right.) The result of that complicated and hard to remember ls command is a list of backups of my Veracrypt containers. Then too, I can look for the oldest one (always below, due to the -t option for ls), cd to the subdirectory in question, and – again using the command history mechanism – update the old backup using a rather complicated rsync command.

Legacy mail program

For e-mail I still use the legacy program Eudora, which is only available for Windows, so I use wine to run it under Linux.

Eudora stores e-mail attachments in a directory called attach. To find out when that is, perhaps to pick up an attachment to use or store it elsewhere, I can do: ncd attach . There are several in my system, so ncd marks them with letter a, b, c, d, etc. By pressing one those letters on the keyboard (no Enter is required), you select that result, and the Change Directory becomes effective immediately.

If that was a mistake, ncd - should take you back.

Wildcards in Unix proper operate only between / separator: they expand to a single file name or directory name. Wildcards for wcd (or ncd, as I call it) can extend over directories, which can be quite useful. Consider this command for example:
ncd -i /i*/atta*
ncd -i /m*/atta*
Both go to /home/rudhar/m/internet/eudora/attach, so the * in i* also matches /eudora, and the * in m* matches /internet/eudora.

I used the option i to ncd to make the search case-insensitive. The default is case-sensitive, so uppercase and lowercase are considered different, and won’t be found unless entered exactly as the are. I expected this option to be necessary in this example, but now see that it isn’t. But using it anyway does no harm.


Notes and details

wcd

About Erwin Waterlander, see here. About wcd, see here and here.

bash

The shell bash probably has a command history mechanism of its own. I can’t remember how it works, but I must have tested it at one point, and didn’t like it.

Currently I use Alpine Linux on my website server, but that used to be FreeBSD. There the default shell was tcsh, and tcsh had a superior command line history mechanism. I can’t remember how I found out, but bash can be configured to behave exactly (or perhaps only nearly) the same way, by putting this:

# arrow up
"\e[A":history-search-backward
# arrow down
"\e[B":history-search-forward

in /etc/inputrc, which actually is a config file for readline, not for bash. But bash intimately cooperates with readline.

which and backticks

Only for the curious and somewhat skilled: to see where the shell script is:
which vc-mymount
To see the shell script’s content and maybe even edit it (but why?)
nano `which vc-mymount`
nano is my preferred text editor – but to each their own. The which command between the backticks ` ` finds out in which of the directories mentioned in the PATH environment variable, the command vc-mymount resides. Using that as a command line argument for nano opens that file in the text editor.

File Manager too!

What I more or less expected, is true: File Manager thunar also has a search function for directories. That invalidates my whole article, the command line is NOT easier and more comfortable than the more modern graphical approach. I should have investigated earlier, and cancelled the plan to write this useless article altogether, right?

Well, no, not really. As a test, I pressed the search symbol (upper right) in thunar, entered USB as in my backup example, and got a mile-long list of irrelevant hits. Reasons:

Remember recent file positions

A point that is in fact off-topic here, because this article is not about text editors, and the keyboard shortcuts of editor nano are rather unusual and require a steep learning curve, but I mention it anyway, because it adds greatly to the overall comfort I experience in my seemingly primitive but in fact in my opinion quite advanced way of working:

In nano’s configuration file ~/.config/nano/nanorc, I have added the option
set positionlog. This config option has the same effect as the command line option -P or --positionlog, as described in the manual page (try man nano):

For the 200 most recent files, log the last position of the cursor, and place it at that position again upon reopening such a file.

Two hundred different text files, that’s more than I normally open in say a week or a month. So in practice that means that in ANY file I open, the cursor is already in the right place, viz. where I left off the previous time. Very handy.

Copy and paste

More on-topic: in most or all Linux terminal emulators, and so seemingly in all shell programs, you can do copy&paste in Windows style: select a bit of text with the mouse, and copy it to the clipboard using shift-ctrl-c.

Yes, shift-ctrl-c, not just ctrl-c, because ctrl-c traditionally has another meaning in Unix: abort the currently running foreground program. Likewise, paste is shift-ctrl-v, not ctrl-v. The older Common User Access (CUA by IBM) shortcuts also still work: ctrl-insert is copy, shift-insert is paste.