How to quickly make minor changes to complex Linux commands


When working in the Linux terminal window, you have a lot of options for moving on the Linux command line; backing up over a command you’ve just typed is only one of them.

Using the Backspace key

We likely all use the backspace key fairly often to fix typos. It can also make running a series of related commands easier. For example, you can type a command, press the up arrow key to redisplay it and then use the backspace key to back over and replace some of the characters to run a similar command. In the examples below, a single character is backed over and replaced.

$ cat mytext | head -1
On the way down the road, I saw one of my friends standing at the corner.
$ cat mytext | head -2
On the way down the road, I saw one of my friends standing at the corner.
She was wearing a hat that was just like one I own. I waved frantically,
$ cat mytext | head -5
On the way down the road, I saw one of my friends standing at the corner.
She was wearing a hat that was just like one I own. I waved frantically,
but she didn't notice. She was staring at a clown on the opposite corner.
When I waved at him, he waved back quite enthusiastically. Only then did
she notice me.

If the command you want to run is complex enough and only the last portion needs to be replaced, this can save you a lot of typing.

To replace text anywhere in the line, you could type a replacement string like ^06^07^ (change 06 to 07) to replace a string with another.

$ cal 06 2023
      June 2023
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
$ ^06^07^
cal 07 2023
      July 2023
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

Using the arrow keys

To move back and forth on the line without changing any of the text in a command, use the left and right arrow keys. You can always stop to add characters or backspace over some of them. The ^ in the example below shows the position of the cursor after which pressing the backspace key followed by a “1” changes “20” to “10”.

$ tail -20 mytext
         ^
$ tail -10 mytext

Moving to the beginning or the end of a line

Once you type a line of text, you can move to the first character without overwriting any of the characters. One way (and the one I prefer) is to use the Home key. The other way is to type ^a (Ctrl + a). These options allow you to insert text at the beginning of the line if you need to do so.

Again, the ^ shows the position of the cursor in the example below.

$ echo Make today a better day than yesterday!
                                              ^
$ echo Make today a better day than yesterday!
  ^

To move back to the end of the line, you can press the End key or type ^e (Ctrl + e).

Removing text to the left and right of the cursor

Linux also provides easy ways to remove text to the left or right of the cursor.

To remove the text on the left of your cursor, press ^u (Ctrl + u). You can then enter the replacement text. In the example below, the position on the ^ is where the cursor would be before and after pressing ^u.

$ echo Make today a bright shiny day
                 ^
$  a bright shiny day
  ^

To remove text on the right of the cursor, press ^k (Ctrl + k). Here’s the text from above before and after ^k was entered:

$ echo Make today a bright shiny day
                 ^
$ echo Make today
                 ^

Wrap-Up

There are numerous ways to move around on the Linux command line and modify commands in order to fix typos or change commands in some way. Some of these little tricks make it both fast easy to reuse complex commands by making only minor changes.

Copyright © 2023 IDG Communications, Inc.



Source link