Search This Blog

Monday 18 July 2011

Five tips for a smoother Linux command-line experience

Five tips for a smoother Linux command-line experience

Takeaway: Many Linux users find the command line intimidating and avoid it like the plague. But these tricks and shortcuts can make the command line more manageable — and downright useful.

When I talk to new (and some even not-so-new) Linux users about the command line, I sometimes get a fairly high cringe factor. This always surprises me, seeing as how the command line is not that difficult. Nevertheless, there it is: Many users simply do not want to use the command line. I get that. But the truth is, at some point, the command line might well be a necessity. And when it is, it’s good to be comfortable using it.

Here are five tips to help make the Linux command-line experience much smoother. I’m not digging deeply into any specific command. These are just a few pointers that will help users understand what they are doing and how to make it easier.

1) Aliases
Certain commands can be ridiculously cumbersome to type. They go on and on and on with options, switches, addresses, and more. Not only are those command painful to type (over and over again), but they’re also a challenge to remember. Fortunately, the Linux command line has a built-in ability to create aliases for those commands. Aliases are lines that are entered into the ~/.bashrc file under the “#some more aliases” section. The format of the alias looks like:
alias ALIAS=COMMAND
where ALIAS is the nickname of the alias and COMMAND is the full command of the alias. There may be instances where the full command must be placed in single quotes (if there are spaces in the command). The best way to test an alias is to do the following:

  1. Open a terminal window.
  2. Create the alias by editing the ~/.bashrc file.
  3. Save the ~/.bashrc file.
  4. Open another terminal.
  5. Test the alias. If the alias works, you’re done. If not, go back and re-edit the file.

2) Terminal history
Open up a terminal window and hit the Up arrow on your keyboard. You should start seeing a list of previously issued commands. This is your bash history. The history keeps a list of the commands you have run and allows you to rerun them simply by locating them in the history (until the command you want shows up at the prompt) and hitting the Enter key. By default, at least in the Ubuntu distribution, the history will contain the latest 1,000 entries. You can change that in the ~/.bashrc file. Look for the line HISTSIZE=1000 and change the number to reflect whatever you require.

3) Tab completion
This is a great tip that always helps. If you are unsure of the exact name of a command, but you know the first couple of letters, type them and hit the Tab key. Bash will then attempt to complete the command for you. If there are multiple possible entries, it will ask whether you’d like to see all of them. This is a great way to locate commands when you can’t remember the name or you simply don’t want to type the entire command. For example, say you know there is a command-line way to start the GNOME Control Center, but you can’t remember the command. You’re sure it starts with gnome, so you type gnome and hit the Tab key. Bash will display a number of possible options, including gnome-control-center. There you go.

4) Running multiple commands at once
Running multiple commands is helpful when compiling applications. Instead of running ./configure, wait for that to complete, then make, wait for that to complete, and then make install, you could instead combine them together like so:
sudo ./compile && make && make install
Notice the use of sudo. It’s needed because the make install command most always requires administrative rights, since this command is copying the executables into the correct directory (usually /usr/bin, /usr/sbin, or /usr/local/bin). Of course, if your compile or make requires any options, you can add them at the same time.

5) Running a second command with prior command arguments
This trick is great. Say you search for the directory ~/firewalls, only to find out that the directory does not exist. Instead of having to issue the command mkdir ~/firewalls, you can use a special trick to auto-magically create that directory. The trick in this case, would be
mkdir !*
The !* characters tell bash to run the new command using the previous command arguments. Here, the arguments are ~/firewalls. The mkdir command is run using the arguments from the previous command, so the full command is then mkdir ~/firewalls. Although this is a simplistic example, it illustrates how this tip can be useful.
Related Posts Plugin for WordPress, Blogger...