Home Classroom Linux Basics Linux Basics Part 5

Linux Basics Part 5

1024

Command-line Tips and Tricks – Bash

In the past two issues we have been looking in detail the structure and organization of the Linux file system hierarchy standard (FHS). In this issue we will get back to the command line discuss a few tips and tricks for you to use, and also to impress your friends.

 

Tip 1: Tab Completion

 

The first tip I am going to give you is the tab completion. If you have been following the articles so far, there is a good chance that you have already found it. When you are about to enter a command in a terminal you can type part of that command and press the “tab” key to use the completion. For example if you want to enter the command “whoami” command, you can type “whoa” and press tab key to complete the rest. The command line will search in your systems and find which commands start from “whoa”, and use the results to complete what you are typing.

 

You might wonder what should be the case where there is more than one command starting with the letters you have typed before pressing type. In this case the command will not be completed. If you press the tab key again (resulting in two consecutive tab key presses), your command line will show you the possible completion options.

 

Eg:

[gaveen@midgard ~]$ wh

whatis    which whiptail  whoami

whereis   while who    whois

[gaveen@ravana ~]$ who

who whoami  whois

[gaveen@ravana ~]$ who
As you can see in the example when you first enter “wh” and press tab key twice, the available completions will be displayed. To narrow down the possibilities then we entered an “o” to make the entered string “who”. The system narrows down the possibilities and then shows us that there are three possibilities starting with “who”. In my case typing an additional “a”, resulting in a “whoa” was enough for the system to narrow down the command possibilities to “whoami”.
Command completion is a useful feature, specially if you do a significant amount of work from the command-line. Is this a feature of Linux or the specific command line we are using?

New Concept: Shell

 

In Unix systems (and in non-Unix systems too), there is a concept called “shell”. A shell is an interface for the user to access the functions of an operating system. A shell can be command-line, a GUI or something different. For example the graphical user interface you see in many modern operating systems can be thought as a graphical shell, while command-line interfaces you find in Unix systems are command shells.

 

If you are running a Linux systems, it is highly possible that the default command shell available in your systems is “Bash”. Bash (Bourne Again Shell) is a modern Unix shell available for many Unix type operating systems including Linux. When you open a terminal program like GNOME Terminal or Konsole, or in the case where you use a text console in your Linux installation you are running a shell (most probably Bash).

 

So a shell is essentially an interface for the user to communicate with the operating system. There are many shell programs our there such as Bourne Shell (sh), C Shell (csh), Korn Shell (ksh), etc. By today the Bash is probably the most popular shell around.

 

The command line tips and tricks described in this article are for Bash. Now that you know tab completion you may want to give it a try before continuing.

 

Tip 2: How to get completion for options and parameters of commands

 

As you see command completion is a pretty nifty feature for bash built in commands. Have you ever wondered if this feature can be extended to further convenience.

 

You have already seen how command line completion helps you with directory/file names and so on. But how about options to commands? You still have to remember those. For example most Linux commands accept word based options. In such a case you are required to type the whole thing.

Eg: $ ls –version

 

This is just one simple example, but in the long run would not it be nice to have your shell support you out when you try to figure out what to input? That is why there is a very handy software package to do just that.

 

To use this you need to install a software package named “bash-completion” under most of the Linux distributions. Installing such software and package management cannot be discussed without more detail. That is why we are going to move that discussion into the next article. More details about the next article can be found towards the end of this article.

 

After you have the bash-completion package installed your shell will help you out when you try to use options for a command. Let us take the example where you want to tell the command “cp” to remove each existing destination file before attempting to open it. The option in question is “–remove-destination”. With the newly enabled completion aid you should be able to just type cp –rem and hit tab to get the option completed for you.

 

New Concept: Options

 

Well, this is not so much a new concept. After all you have been seeing it from article 1. But to make things more clear for you let me explain a bit about what an option is.

 

An option is something we pass to a command, usually in the form of a few characters to change the default behavior of the command. For starters you already know that while the “ls” command will provide a list of things in a directory by default, if you throw in the “-l” option the list would be in the longer more detailed format.

 

Options in Linux commands come in two major types (and an additional flavor). The two types are the short single character options and the longer word-like options. Let us take example for each type. The first type, the short options are usually single charactered but can be chained in use.

 

While “ls -l” will provide a long listing the “ls -a” will provide a listing consisting of everything (including hidden files). So the “ls -la” will provide a long listing of everything in the directory you are working. Similarly “ls -hal” will provide you a long listing of everything with the size of the files in human friendly format.

 

As you can notice the order of the options in such chaining does not matter. This is true as far as the option does not require any parameters. For example the “-t” option for mount command mandates a file system type after the option. So you cannot use any other option before you specify the type. In this case you can type the next option separately from the first option.

Eg: $ mount -t ext4 -v /dev/sda5 /mnt

 

Notice the usage of “-t” and “-v” options here are different from the chaining of options we discussed before.

 

The second major type of options is the longer, word-like options which are preceded by two “-” (hyphen/dash character). We have seen such options too.

Eg: $ ls –version

 

These type of options are longer and usually have more explanatory name. In the above example it is obvious what the options does; prints the version of the “ls” command you are using.

 

Mixing these two types of options are fine.

Ex: $ ls -l –author

 

The above example will display a long list and include a column stating who is the author of each file.

 

There is one last thing to mention before we move the additional option type. When using options of a command, there is no specific or preferred order to follow. They all carry the same weight as long as they do not conflict.

 

Now that you know the main two types of types of options let us see the 3rd type of options; bsd style options. Before I tell you what are they let me tell you a very little note on how the cam into being.

 

In traditional Unix systems the command options were usually the short format options with single character. As the Unix developed into different flavors, so did the command line. When the second-most popular branch of Unix gained popularity (from the University of California, Berkeley) their style of options also came to the public use. These options are typed without any trailing characters such as “-”, and works pretty much the same way the short type options we talked already.

Eg: $ ps aux

 

The above command will show you the list of all the processes running in your systems at the time of the execution of that command. The other type of the options (longer format) were popularized by the GNU project which provides a lot of command line utilities for Linux and other Unix operating systems.

 

The “ps” command is a good place of reference to the different types options as it supports all the 3 major types of options. The classic Unix style options (single letter options which can be grouped and preceded by a single “-”), BSD style options (single letter options which can be grouped and is not preceded by “-”) and finally the newer GNU style longer options (which are preceded by two “-” characters. i.e.: “–”).

 

Tip 3: Clearing a Terminal

If you have been practicing what has been discussed so far in this Linux basics series, you may have wondered if there is a way to clear the contents of a terminal. For example it is always less distracting when you work in a clean terminal rather than in a terminal with output from previously executed commands. If you were keen enough you might have found that the “clear” command clears a terminal. While this is great, it is still 6 key strokes to get there. We have a trick for you to make it quicker. Pressing Ctrl+l keys (i.e. pressing “Ctrl” key and holding it down while pressing “l”) will clear your terminal.

 

Tip 4: Keyboard traversing in a terminal

 

When you work in a terminal you are bound to get the need to move your cursor here ans there. You can always use your arrow keys. But pressing down an arrow key till the cursor reaches the point you need takes time, and after a few times it becomes frustrating. If you have typed a long command you usually do not want to hit backspace all the way back deleting what you have typed. Here are some keyboard shortcuts that should aid you.>

 

Ctrl+a will take you to the beginning of the line.

 

Eg: lets say you have typed something in a terminal, you have not hit the enter key yet because you need to go to the beginning of the line and add/change something. Currently your cursor is at the end of the line. In this case after “t”.

 

$ mount /dev/sdb1 /mnt

 

Pressing Ctrl+a will get the cursor to the beginning of the line. In this case to where the first “n” is. Think of it in this way. “a” is the beginning of the alphabet. So Ctrl+a means go to the beginning.

 

Ctrl+e in the opposite of Ctrl+a, which means Ctrl+e will take the cursor to the (E)nd of the line.

 

Ctrl+RightArrow or Alt+f will move the cursor to the next word, which means in the forward direction. It is more convenient if you want to jump from word to word instead of going through single characters.

 

Ctrl+LetfArrow or Alt+b will move the cursor one word backwords.

 

Next Issue

 

In the last couple of articles we have been going though the use of Linux command line. And in this article we discussed a few useful command line tips. As the articles continue we can find out more tips.

 

However our last few article ventures kept us in the command line. So you might be looking forward to go into the GUI for a change. So far we have discussed some essential things to make you comfortable with moving around and working in the command line, which is in my humble opinion is one of the advantages and true powers of Linux (and other Unix systems have). Since now you should be familiar with the Linux basics we can move on to more day to day topics and possibly more working time in the GUI if you prefer.

 

Linux systems usually inherit a concept called package management and in turn a concept called software packages. These concepts are actually related to system administration. However a user of a desktop Linux systems also need to search, install ans remove software. Because of this reason we will be dedicating the next article to package management. While there is no possible way of explaining every single available variant of package management software for Linux in a single or a few articles, we can try to discuss the basic concepts and usage quite quickly.

 

So get yourself busy on this months article, and try to get a picture of package management by reading the Wikipedia article http://en.wikipedia.org/wiki/Package_management_system

Comments

comments

Gaveen is a Linux System Administrator with several years of industry experience and is a strong FOSS promoter. He is pending graduation from University of Wolverhampton and also holds a certification from Red Hat. He also conducts Linux trainings with Red Hat training partners. Being highly enthusiastic about computing, he supports a keen interest in Ruby programming too. When not distracted by his wide range of interests including reading, music, cricket, etc.,he regularly blogs at Gaveen's Blog

NO COMMENTS

Leave a Reply