let's search

Google
 

Sunday, June 1, 2008

Linux: Introduction to the command line

People can interact with computers running Linux in two ways -- using the graphical user interface (GUI) or the command line interface (CLI). By learning a few fundamentals, you can use the command line to accomplish miracles.

Often, a graphical interface is the best, if not the only, way to go: the most common example is advanced photo editing. A mouse-only approach, however, can be extremely limiting. In a graphical interface you only find the buttons that somebody else considered necessary.

Above all, you must be there to push them, so it may be impossible to automate anything but the simplest button sequences. If doing something requires just three mouse clicks, doing it 100 times may require 300 mouse clicks. Doing the same thing at the CLI, however, would be just require an action loop. This command, for example, is all you need to find all the JPG images in the current folder and place a thumbnail of each of them in /tmp/thumbnails/:

for picture in `find . -name "*jpg"`; do convert -sample 80x40 $picture /tmp/thumbnails/$picture ; done

ou can get to the CLI in two ways. The first is the standard Linux console, which you'll see if your system isn't running X or if you switch to one of the virtual consoles while X is running. Everything you enter at the command line is interpreted by the shell, which is a program that interprets and executes commands run at the command line or read from a script. The default shell on most Linux distributions is the Bourne again shell, or bash.

Data and variables made only of text are also called strings.

Every time you type a command, the shell does one of two things. If the first argument -- that is, the first sequence of non-whitespace characters -- is the name of a program, then the shell launches that program, passing as arguments to it everything you wrote after that string.

If what you typed isn't a program name, the shell interprets it as a command. The shell also has a simple programming language built in, with the possibility to read or create files and many reserved words associated to the most common functions and operations. For example, cd means "change directory," pwd means "print the name of the current working directory," and history lists the most recent commands you typed.

Shells are extremely powerful also thanks to another feature: any sequence of commands can be saved to a plain text file and executed again, any time you wish, without retyping everything. All you have to do is to write as the first line in that file the string:

#! /bin/bash

This tells the system that the rest of the file should be directly interpreted by bash. You must also make the file executable with the chmod command:

  chmod 755 my_shell_script_file

This form of programming, called scripting, may be all you ever need to fully customize your Linux computer.

Many Linux commands and programs have documentation available right at the command line. When you need to know how some program works or what are its options, type man or info followed by the program name, and most of times you'll have the answer

The best documentation freely available online to help you become a shell guru is probably the Advanced Bash Scripting Guide. As far as quick and practical tips go, instead, there are plenty of them in the "CLI Magic" series on Linux.com. Some are related to system administration, from knowing what is happening in your computer to automating file searches and other operations or discovering malware. Desktop-wise, is is possible to access a Bluetooth phone or manage your contacts.

No comments: