The command line is the most direct way to send commands to your machine. If you use the GNU/Linux command line, you will soon find that it is much more powerful and capable than other command prompts you may have previously used. The reason for this is that you have direct access, not only to all X applications, but also to thousands of utilities in console mode (as opposed to graphical mode) which do not have graphical equivalents, or the many options and possible combinations of which would be hard to access in the form of buttons or menus.
But, admittedly, it requires a little help to get started. The first thing to do is to launch a terminal emulator. While accessing either your GNOME or KDE application menu, you will find these in figure 1-3).
. Then, choose the one you want, for example Terminal or xterm. You also have an icon which clearly identifies it on the panel (When you launch a this terminal emulator, you actually use a shell. This is the name of the program with which you interact. You find yourself in front of the prompt:
[queen@localhost] ~ $ |
# Enter the root password; it will not appear on the screen [queen@localhost] ~ $ su Password: # exit will make you come back to your normal user account [root@localhost] queen # exit [queen@localhost] ~ $ |
When you launch a shell for the first time, you normally find yourself in your home directory. To display the directory you are currently in, type pwd (which stands for Print Working Directory):
$ pwd /home/queen |
Now, we will look at a few basic commands, and you will soon find that you cannot live without them!
The cd command is just like DOS', with a few extras. It does just what its acronym states, changes the working directory. You can use . and .., which stand respectively for the current and parent directories. Typing cd alone will take you back to your home directory. Typing cd - will take you back to the last directory you visited. And lastly, you can specify the peter's home directory by typing cd ~peter (~ alone means your own home directory). Note that as a normal user, you usually cannot go into another user's home directory (unless she explicitly authorized it or if this is the default configuration on the system), except if you are root, so let's be root and practice:
$ pwd /root $ cd /usr/share/doc/HOWTO $ pwd /usr/share/doc/HOWTO $ cd ../FAQ-Linux $ pwd /usr/share/doc/FAQ-Linux $ cd ../../../lib $ pwd /usr/lib $ cd ~peter $ pwd /home/peter $ cd $ pwd /root |
Now, go back to being a normal user again.
All processes have their environment variables and the shell allows you to view them directly with the echo command. Some interesting variables are:
HOME: this environment variable contains a string which represents your home directory.
PATH: this variable holds the list of all directories in which the shell should look for executables when you type a command. Note that unlike DOS, by default, a shell will not look for commands in the current directory!
USERNAME: this variable contains your login name.
UID: this one holds your user ID.
PS1: this variable contains your prompt's definition. It is often a combination of special sequences. You may read the bash(1) manual page for more information.
To have the shell print a variable's value, you must put a $ in front of its name. Here, the echo command will help you:
$ echo Hello Hello $ echo $HOME /home/queen $ echo $USERNAME queen $ echo Hello $USERNAME Hello queen $ cd /usr $ pwd /usr $ cd $HOME $ pwd /home/queen |
As you can see, the shell substitutes the variable's value before it executes the command. Otherwise, our cd $HOME would not have worked here. In fact, the shell first replaced $HOME by its value, /home/queen. Therefore, the line became cd /home/queen, which is what we wanted. It is the same thing for echo $USERNAME and so on.
Nothing much to say, this command does just that: print the contents of one or more files to the standard output, normally the screen:
$ cat /etc/fstab /dev/hda5 / ext2 defaults 1 1 /dev/hda6 /home ext2 defaults 1 2 /dev/hda7 swap swap defaults 0 0 /dev/hda8 /usr ext2 defaults 1 2 /dev/fd0 /mnt/floppy auto sync,user,noauto,nosuid,nodev 0 0 none /proc proc defaults 0 0 none /dev/pts devpts mode=0620 0 0 /dev/cdrom /mnt/cdrom auto user,noauto,nosuid,exec,nodev,ro 0 0 $ cd /etc $ cat modules.conf shells alias parport_lowlevel parport_pc pre-install plip modprobe parport_pc ; echo 7 > /proc/parport/0/irq #pre-install pcmcia_core /etc/rc.d/init.d/pcmcia start #alias char-major-14 sound alias sound esssolo1 keep /bin/zsh /bin/bash /bin/sh /bin/tcsh /bin/csh /bin/ash /bin/bsh /usr/bin/zsh |
Its name is a play on words related to the first pager ever used under UNIX, which was called more. A pager is a program which allows a user to view long files page by page (more accurately, screen by screen). We speak here about less rather than more because its use is much more intuitive. You should use less to view large files, which do not fit on a screen. For example:
less /etc/termcap |
To browse through this file, use the up and down arrow keys. Use q to quit. However, less can do far more than just that. In fact, just type h for help, and take a look. But anyway, the goal of this section is just to enable you to read long files, so we already reached our goal :-)
The ls (LiSt) command is equivalent to DOS' dir command, but it can do much much more. In fact, this is largely due to the fact that files can do more too. The command syntax for ls is:
ls [options] [file|directory] [file|directory...] |
If no file or directory is specified on the command line, ls will list files in the current directory. Its options are very numerous and we will only explain a few of them:
-a: lists all files, including hidden files (remember that in UNIX, hidden files are those whose names begin with a .); the -A option lists "almost" all files, which means every file the -a option would print except "." and "..";
-R: lists recursively, i.e. all files and subdirectories of directories mentioned on the command line;
-s: prints the file size in kilobytes next to each file;
-l: prints additional information about the files;
-i: prints the inode number (the file's unique number on a file system, see chapter The Linux Filesystem) next to each file;
-d: treats directories on the command line as if they were normal files, instead of listing their contents.
Some examples:
ls -R: lists the contents of the current directory recursively;
ls -is images/ ..: lists files in the images/ directory and in the parent directory of the current one. Then, it prints, for each file, the inode number and its size in kilobytes;
ls -al images/*.png: lists all files (including any hidden files) in the images/ directory whose names end with .png. Note that this also includes the file .png, if one exists.
Many keystrokes are available and their main advantage is that they save you a lot of typing time. This section assumes you are using the default shell provided with Mandrake Linux, bash, but these keystrokes should work with other shells too.
First: the arrow keys. bash maintains a history of previous commands which you can view with the up and down arrow keys. You can scroll up to a maximum number of lines defined in the HISTSIZE environment variable. Moreover, the history is persistent from one session to another, so you will not lose the commands you typed in previous session.
The left and right arrow keys move the cursor left and right on the current line, so you can edit your commands this way. But there is more to editing: Ctrl+A and Ctrl+E, for example, will bring you respectively to the beginning and the end of the current line. The Backspace and Del keys work as expected. An equivalent to Backspace is Ctrl+H and an equivalent to Del is Ctrl+D. Ctrl+K will delete all the line from the position of the cursor to the end of line, and Ctrl+W will delete the word before the cursor.
Typing Ctrl+D on a blank line will make you close the current session, which is much shorter than having to type exit. Ctrl+C will interrupt the currently running command, except if you were in the process of editing your command line, in which case it will cancel the editing and get you back to the prompt. Ctrl+L clears the screen.
Finally, there is the case of Ctrl+S and Ctrl+Q: these keystrokes respectively suspend and restore the flow of characters on a terminal. They are very seldom used, but it may happen that you type Ctrl+S by mistake (after all, S and D are very close to each other on a keyboard...). So, if you strike keys but do not see any character appearing on the Terminal, try Ctrl+Q first and beware: all characters you typed between the unwanted Ctrl+S and Ctrl+Q will be printed to the screen all at once.