Americas

  • United States
sandra_henrystocker
Unix Dweeb

6 Linux command-line tricks for fewer keystrokes

How-To
May 12, 20214 mins
Linux

Linux has many tricks that can reduce the number of keystrokes it takes to type in commands. Here are six of them.

man looking up linux code command for user assessment by electravk getty images
Credit: electravk / Getty Images

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
diff-commands    dig.1            directory
dig.2            dimensions       disk-usage-commands
$ cd dir
$ 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   
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!

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.

More from this author