May 27, 2008

Get Used to the Command Line in Linux, Part 1

Part 1: The Basics

A shell is a command interpreter which allows the user to interact with the computer. Basically, it is a program which takes the commands you type, interprets them and performs the tasks you asked it to do, then it sends the results to the standard output, which usually is the screen. The most widely used shell on Linux systems is Bash, and it is also the default on most of the distributions out there, like Ubuntu or Fedora. Bash stands for Bourne-Again Shell, and is a modern implementation of the older Bourne Shell (sh), developed by the GNU Project.

Linux features several GUI (Graphical User Interface) applications which allow you to use a shell, like the GNOME Terminal or Konsole in KDE. Many more are available, and a review is available here. The next screenshot shows a typical instance of Konsole, using a transparent background and showing the output of the ls command:

Typical instance of Konsole (click to enlarge)

The ls command is used to list the files and directories in the current working directory. Here's the Gnome Terminal:

GNOME Terminal, using desktop transparency

Commands in Linux are of the form:

command option(s) filename(s)

For example, ls in the above example lists the files and directories in the current directory, but ls -a will also list the hidden files (the ones preceded by a dot).

To see which is the directory you're currently working in, use the pwd command:

$ pwd

pwd stands for 'print working directory'. In order to navigate through the file structure, you would use the cd (change directory) command, like this:

cd /home/christmas

which is equivalent to:

cd ..

'.' and '..' are two special types of files in Linux, the first one pointing to the current directory, while the second one points to one directory up in the file structure hierarchy. So cd .. will change the working directory from /home/christmas/docs to /home/christmas/, while the single dot can be used, for example, to copy files faster. To copy the /etc/motd file in the current directory, you would issue the command:

cp /etc/motd .

Of course, instead of '.' you could have used the full path, like this:

cp /etc/motd /home/christmas/docs

But the first one is obviously faster to type. The following is a list of basic commands, together with a short description. Try them and play with them, since they will prove themselves handy when you'll work on command line:

ls list directory contents
pwd print name of current/working directory
date print or set the system date and time
mkdir make directories
rm remove files or directories
mv move (rename) files
who show who is logged on
whoami print effective userid
cat concatenate files and print on the standard output
less file viewer

The man command offers detailed help about a specific tool or application on Linux. Usually, any application designed for Linux should have a manual accessible through the man command. For example, try:

man bash

To navigate throughout the manual use the J and K keys, or CTRL+N for next line and CTRL+P for previous line. Use Q to quit and return to the shell prompt. man is one of the most used commands in Linux when trying to get help about a specific command, and you should get used to read the manual page for a certain application before going elsewhere for help. Usually, all the GNU utilities include everything you would want to know in the manual page.

When using commands, try to use the TAB key in order to have the shell complete the commands instead of typing their full name.

Updated: Jun 20, 2008 (Created: May 27, 2008)

1 comment:

Heso said...

I think you should mention that by "faster" you meant "faster for the user to write". It doesn't mean the files will be copied faster. :)