6 clever command-line tricks for fewer key strokes


Linux commands offer a lot of flexibility. This post details some ways to make them even more convenient to use by making use of some clever tricks.

Using file-name completion

You can avoid typing a full file name by typing the beginning of its name and pressing the tab key. If the string uniquely identifies a file, doing this will complete the filename. Otherwise, you can enter another letter in the name and press tab again. However, you can also get a list of all files that begin with a particular string by typing the string and then hitting the tab key twice. In this example, we do both:

$ ls di<tab><tab>
diff-commands    dig.1            directory
dig.2            dimensions       disk-usage-commands
$ cd dir<tab>
$ pwd
directory
 

Reusing commands and changing them

Reissuing recently used commands is easy in bash. To rerun the previous command, all you have to do it type !! on the command line. You can also reissue a command with changes. If you issued the first command shown below only to find that sshd wasn’t running, you could issue the second command to start it. It simply replaces “status” with “start”.

sudo systemctl status sshd
!!:s/status/start/

Reusing command arguments

You can also reuse just the arguments provided to the previous command without having to retype them by using the string !* as shown in this example:

$ mkdir dir1 dir2 dir3
$ chmod 770 !*
chmod 770 dir1 dir2 dir3

Notice that the command is displayed in full after you type the command using “!*”.

Keep in mind that “all arguments” really does mean “all arguments”. If after typing the commands shown above, you want to list the directories you just created using the !* trick, you’ll run into a small problem.

$ ls -ld !*
ls -ld 770 dir1 dir2 dir3 ls: cannot access '770': No such file or directory <==== oops! drwxrwx---. 2 shs shs 4096 Jun 6 2018 dir1 drwxrwx---. 2 shs shs 4096 Jun 6 2018 dir2 drwxrwx---. 2 shs shs 4096 May 11 09:20 dir3

Looking only at recently entered commands

The history command makes it easy to review previously entered commands, but usually displays 1000 (i.e., all) of the commands that are in your history buffer. If you want to see only recently entered commands, the easiest and fastest way is to supply the number of commands that you want to view as an argument to the history command. This listing shows the most recent five commands entered.

$ history 5
 1162  11/05/21 13:10:54 shopt | wc -l
 1163  11/05/21 13:19:42 sudo systemctl status
 1164  11/05/21 13:20:01 sudo systemctl status sshd
 1165  11/05/21 13:23:37 man history
 1166  11/05/21 13:23:50 history 5

You could also use a command like “history | tail -5”, but the one just shown is easier and doesn’t require sending 1000 lines of output to the tail command.

Searching through history for specific commands and rerunning them

To look back into your recently used commands (most recent first) in order to rerun some particular command, type ^r (hold control key and press “r”). Then type a portion of the command. Keep pressing ^r until you land on the command that you want to reuse and then just press the return key.

(reverse-i-search)`opt': shopt | wc -l
$ shopt | wc -l
53

The output shown tells us that the shopt command has 53 settings.

Making file backups super easy

One very convenient way to back up a file is to use a command like this that adds “.backup” to the backup file:

$ cp myfile{,.backup}

The same technique works for the mv command when you, instead, want to rename the file.

$ mv myfile{,.backup}

If you want to back up a series of files, you can save a little time and trouble by using a script like this one:

#!/bin/bash

for file in $*
do
  cp $file{,.backup}
  ls -l $file.backup
done

The following script will take the list of files you provide as arguments and copy each to its *.backup form. You can change “backup” to “old” or some other word if you like.

$ backup thisfile thatfile otherfile
-rw-r--r--. 1 shs shs 1234 May 11 13:37 thisfile.backup
-rw-r--r--. 1 shs shs 2012 May 11 13:37 thatfile.backup
-rw-r--r--. 1 shs shs  876 May 11 13:37 otherfile.backup

Depending on the names of the files you are backing up, you can also use wild cards. For example:

$ backup project*
-rwxrwxr-x.  1 shs   shs         16800 Jan  5 18:10 project.log
-rwxrwxr-x.  1 shs   shs         16840 Jan  5 18:44 project.plan
-rw-rw-r--.  1 shs   shs           324 Jan  5 17:51 project.staff

Wrap-Up

I’ve loved the Linux command line since I first used it nearly forty years ago. And the best part? There’s always more to learn and more ways to make it even nicer to use!

Join the Network World communities on Facebook and LinkedIn to comment on topics that are top of mind.

Copyright © 2021 IDG Communications, Inc.



Source link