Linux Format

Shortcuts...........................

Nick Peers goes hunting for time-saving tips, tricks and shortcuts that will enable you to use the Terminal in a more efficient manner.

-

Nick Peers figures out how to configure your screen resolution from the terminal and hits the configurat­ion files hard.

Transition­ing from a graphical user interface to the Terminal? Then you’ll want to speed things up. The good news is that the Terminal is packed full of timesaving commands and shortcuts; the trickier part is actually finding them. Never fear, because this issue we’ve dug out a collection of handy command-line tricks that will transform the way you interact with the Terminal going forward.

Repeat previous commands

One of the first time-saving tips you’ll learn is that pressing the up and down arrow keys at the command line cycles you through the most recently used Terminal commands. That’s fine if the command you want was typed a short while ago, but a much quicker way to find what you’re looking for is to press Ctrl+R and then start typing a few letters – you’ll see the most recent match appear in the list. Hit Enter to run it again, or press the right arrow to insert it into the command line, allowing you to modify it first.

If the command isn’t the one you’re looking for, you can keep hitting Ctrl+R to cycle through previous matches until you find the one you’re looking for, or alternativ­ely press Ctrl+C to exit and then type the following: $ history

This will list all the commands stored in the Terminal’s buffer. To repeat one, type the following, replacing ‘1’ with the number next to the command you want to run: $ !1

You can also use !! to simply repeat the previous command. A particular­ly handy use for this is when you’re told the command you tried to run requires root privileges. When this happens, simply type sudo !! and hit Enter.

Faster directory management

In a similar way, !$ allows you to reuse an argument from the previous command in your current one – for example, the following commands create a new directory then change to it: $ mkdir ~/Documents/work $ cd !$

The !$ shortcut is just one way to speed up the way you interact with files or navigate your filesystem, which leads us neatly on to some more tips and tricks. One you may already know is tab completion: as you start typing a command or path, press Tab to attempt to autocomple­te the command or folder. If nothing happens, press Tab twice to bring up a list of potential matches. Type enough letters to make it clear what command or path you’re aiming for, then press Tab and it should pop up.

Do you ever find yourself working within several folders at the same time? Thanks to the pushd command, you can create a folder stack, a list of folders you can quickly navigate to using the cd command.

Let’s start by adding the current directory to the stack: $ pushd

You’ll see the folder is listed next to ~. To add a different directory to the stack and then switch to it, use this syntax: $ pushd /path/to/folder/

You’ll move to this directory, and also see that it’s added to your original folder in the stack. For a better view of the stack’s contents, type the following: $ dirs -v

You’ll see each folder is listed with a number next to it. You can use this number in conjunctio­n with the cd command to quickly jump between these folders (replace ‘0’ with the

number of the folder you wish to access): $ cd ~0

This folder stack is temporary – once you close the Terminal window it’s lost.

Here’s another shortcut: if you’ve created a folder inside which you now need to create a collection of sub-folders, speed things up by using mkdir with the following argument to create the named sub-folders in one go (you can specify as many sub-folders as you like): $ mkdir -p ~/Documents/{work,home,admin,letters} The same argument applies with the rmdir command too, allowing you to quickly delete empty subfolders as well.

One final shortcut: need to quickly ascertain the difference­s between two directorie­s? Make use of the diff command, thus: $ diff /folder1 /folder2 This will do the gruntwork for you, and the output will reveal which files are exclusive to which folder. Some commands – particular­ly those with lengthy arguments – can be a real bind to type if you use them regularly. The alias command enables you to bind a shortcut to the lengthy command and its argument. Ubuntu’s Terminal provides some examples by default, revealed when you type alias and hit Enter. These include different ways of listing a directory using the ls command. To create your own aliases, invoke alias using the following syntax: $ alias shortcut='command'

Replace ‘shortcut’ with the name of your chosen shortcut, noting that it cannot contain any spaces (if you need to, use a different separator, such as a dash or underscore). If you use an existing command – such as ls – then the alias will circumvent it when the command is used without an argument; in other words, it allows you to effectivel­y change the command’s default behaviour.

Once you’ve set up an alias or two, review what they are by typing alias and hitting Enter again. To remove an alias, use the following: $ unalias shortcut

The alias command is very useful, except for one critical shortcomin­g: its effects only last as long as the current Terminal window is open. Close the window and all your aliases are gone. All is not lost, however, as you can permanentl­y embed aliases into each Terminal session by adding them to a hidden file called bash_aliases in your home directory. $ nano ~/.bash_aliases

This creates a new, empty document, so add your alias commands one at a time using the same syntax as before, then save your file. From here either close and reopen the Terminal or use the following command to reload the bashrc file, which includes your Terminal preference­s (and references the bash_aliases file you just created): $ source ~/.bashrc

You’ll find that your aliases should now be preserved across Terminal sessions.

Quickfire tips

To round things off, here are some more handy shortcuts. First, when you launch a graphical app like Firefox or the Nautilus file manager through the Terminal, you’ll see how the Terminal window remains in place, unable to do anything until you close the applicatio­n. To prevent this (or the need to open a second Terminal window), simply append ‘&’ to the end of your command, like so: $ firefox &

Struggling to find the right command to use? The apropos command can steer you in the right direction – just use it in conjunctio­n with some text describing what you’re looking for, such as: $ apropos "download"

Last but not least: you know that the clear command quickly clears the Terminal screen, but what if you have a command lined up and ready to go? Simply press Ctrl+L to clear the screen but leave your command in place.

 ??  ?? The !! command allows you to repeat the last command entered. A great use for this is to insert a missing sudo when a command needs admin privileges.
The !! command allows you to repeat the last command entered. A great use for this is to insert a missing sudo when a command needs admin privileges.
 ??  ?? Nick Peers estimates that by following the tips he’s uncovered in this tutorial, he may actually reduce his typing in the Terminal by up to 50 per cent.
Nick Peers estimates that by following the tips he’s uncovered in this tutorial, he may actually reduce his typing in the Terminal by up to 50 per cent.
 ??  ?? The alias command is a great command for speeding up repetitive commands, but its temporary nature requires a workaround.
The alias command is a great command for speeding up repetitive commands, but its temporary nature requires a workaround.

Newspapers in English

Newspapers from Australia