================================ THE ESSENTIAL LINUX COMMAND-LINE ================================ An introduction to the Linux console, oriented towards the novice-user with no prior experience using a command-line environment. Deepak N. 09 March 2003. http://puggy.symonds.net/~deep/ n.deepak@gmail.com This document is in ASCII text, in order to make it readily accessible from any text editor. It is intended to be read after viewing the presentation introducing you to the Linux operating system. A very basic (essential) coverage of the vi and emacs text editors can be found in the file 4viemacs.txt. This document is distributed under the GNU Free Documentation Licence. -------------------- FILES IN THIS PRIMER ==================== 01. bash! --------- Introduction to the Linux command-line 02. Lonely Shell -- Commandeering your Linux system 03. Yes, Master --- (Helpful) advanced topics 04. C-x C-c :q! --- Appendix: Essential vi and emacs -------------------------------------------- BASH! INTRODUCTION TO THE LINUX COMMAND-LINE ============================================ Many people are scared of commands. They think it is a geekish way of accomplishing things, if not archaic. Let me clear this misconception first: even if you use a graphical interface, it ultimately boils down to a command. When you click on a directory's icon in a file-browser, it is a command for the application to list out the directory's contents (as icons, perhaps). The menus offer a hassle-free way of interacting with a system, only because you do not need to memorise the commands. You just click on a menu-item, and the system considers it as a command: so your work is done. Ah, if things were always so easy. If you have ever experienced a situation in which Windows will refuse to boot and you are stuck with DOS, you will understand what I am trying to say. Even though GUIs exist, it does not mean that you can forego learning commands. Many a time, commands accomplish a task much faster. They are also less resource-hungry compared to their graphical equivalents. They tend to have fewer bugs, and are sometimes inevitable. So it is much better to know a few commands, isn't it? I give below some important commands and are worth learning. I don't make any attempt to make any command comprehensive: that is left to their manuals. Neither do I list each and every command; only the most important and useful are covered. The point kept in mind while considering every command is this: does it help for an average user to know this command? Will it make his experience in using Linux any better? Let's begin our sojourn, then. In this file, I introduce you to the Linux command-line, and also talk about several miscellaneous points you need to know before you can start trying out commands. -------- CONTENTS ======== o The shell o Some things Windows users need to know + Mounting / unmounting - Making a DOS/Windows partition visible in Linux + Front-slash or back-slash? + Executing a file in the current directory + User and super-user o Important directories in Linux o Miscellaneous notes + * and ? -- Two important wildcards + Short-cuts to directories + Three useful tips o Caveats o Elementary troubleshooting --------- THE SHELL ========= Commands are entered at a 'prompt'. A typical Linux prompt looks this way: [foo@host foo]$ _ The x@y signifies that this console is being used by user x, and the host-name is y. The second word is the current working directory, in this case /home/foo, which is the default 'home' directory for a user foo in Linux. Hereafter I will simply use the '$ _' combination to signify a prompt. The cursor blinks and waits for you to enter a command. The prompt is issued by a special program called the 'shell'. The shell is a kind of filter. It reads your commands, modifies them if necessary, and then calls the respective program. Some commands are executed by the shell itself. There are many shells for the UNIX environment, and bash is the standard shell for Linux. At this stage you need not know anything more, so let us move on. *********************************************************************** -------------------------------------- SOME THINGS WINDOWS USERS NEED TO KNOW ====================================== ------------------- MOUNTING/UNMOUNTING ------------------- This is a frequently asked question. What does 'mount' mean anyway? In DOS, a floppy-drive is A:, a CD-ROM drive is E: or F: or some other letter, and so on. UNIX does not assign any letter to a peripheral, and instead gives them a place in its root (/) filesystem. Let me elaborate. Suppose you want to use a floppy. You insert it into the drive, and then 'mount' the floppy onto a directory by entering this command at the prompt: mount /dev/fd0 /mnt/floppy You then go to /mnt/floppy directory, and if you give the ls command, you will see the floppy's contents. (In most Linux distributions, the /dev/fd0 parameter can be omitted. mount will look for it by default.) For all practical purposes, /mnt/floppy denotes your floppy: you can move, copy, and even delete files from /mnt/floppy: the changes will be reflected in the actual floppy disk. After finishing your work, you say: umount /mnt/floppy Now you have 'detached' the floppy from the system. Give an ls to verify. The same idea applies to other devices and filesystems, such as your CD-ROM drive, Windows FAT32 filesystem, and so on. You might ask, why such a round-about way? It is such an irksome method! The technical reason is security. mount command is the only way you can gain access to the contents of a peripheral or another filesystem, so if that is disabled to a user, we have secured our system to some extent, right? (I told you Linux can run a server. Servers definitely cannot run an insecure operating system such as Windows 95 or MacOS. DOS doesn't even come into picture.) MAKING A DOS/WINDOWS PARTITION VISIBLE IN LINUX ----------------------------------------------- As a by-the-way tip, I am also including the commands to mount a DOS / Windows drive in Linux. _Usually_ your C drive will be /dev/hda1, D drive will be /dev/hda5, E will /dev/hda6 and so on. If you ask what happened to /dev/hda2 to /dev/hda4, well, they are reserved for primary partitions. Never mind. Here is how to make your Windows partitions visible: $ su Password: (Enter root's password. It is not displayed on-screen.) # mkdir /mnt/winc /mnt/wind # These are the 'mount-points' # mount /dev/hda1 /mnt/winc # Now your C-drive is /mnt/winc # mount /dev/hda5 /mnt/wind # Your D-drive is /mnt/wind and so on. # exit $ _ Note, however, that you have to remount the partitions every time you reboot your system. If you want them to be automatically mounted every time, you have to make entries in the file /etc/fstab. Here are sample entries for C, D and E drives. C drive: /dev/hda1 /mnt/winc vfat defaults 0 0 D drive: /dev/hda5 /mnt/wind vfat defaults 0 0 E drive: /dev/hda6 /mnt/wine vfat defaults 0 0 and so on. Also, if the hard-drive is a primary slave, replace hda with hdb. If it is secondary master, you have to say hdc, and finally, if it is a secondary slave, it is named hdd. As a note of warning, /etc/fstab is a very important file. Don't play with it. Be careful. -------------------------- FRONT-SLASH OR BACK-SLASH? -------------------------- Before you actually try out the commands, a line of warning: in UNIX, directories are separated by the front-slash (/), not the backslash (\) as in DOS and Windows. So you say: cd /home/tomh and NOT cd \home\tomh The backslash has a special use in UNIX/Linux, namely to 'escape' the special meaning of the character which follows it. Don't worry, just remember to use the front-slash, and not the back-slash. ----------------------------------------- EXECUTING A FILE IN THE CURRENT DIRECTORY ----------------------------------------- By default, most Linux versions do not search the current directory when you type the name of an executable. For example, let's say you are in DOS, and you have a file in C:\MYDIR, with the name GAME.EXE. Then you can simply say: C:\MYDIR>GAME The binary will be executed. Now let's suppose you are in Linux, and in /home/tomh/mydir directory, with an executable game (executables need not have any extensions in UNIX). Try saying game: $ game bash: game: Command not found $ _ The trick is, you have to specifically execute from the current directory: $ ./game Don't be unnerved. '.' always signifies current directory, and '..' always parent directory, both in Linux and DOS. This roundabout method is implemented in order to prevent accidental execution of probably harmful binaries in the current directory. ------------------- USER AND SUPER-USER ------------------- In network operating systems like Linux and Windows NT, a few users have special powers. They are called Administrators in NT and 'root' in UNIX/Linux. They can execute commands which ordinary users cannot, and hence they are called super-users. In Linux, the default login-name for the super-user is 'root'. If you log in with the id root (and with root's password), then you have logged in as a super-user. *********************************************************************** ------------------------------ IMPORTANT DIRECTORIES IN LINUX ============================== Before you start commanding, you should know what directories exist in Linux, and why they exist. I obviously cannot explain each and every directory, so I give here a short list of the most important directories and what they house. / The 'root' directory; reference point for all directories. /bin Binaries which are absolutely essential to run Linux. /boot All the files required for booting Linux on a system. /dev All the devices have their corresponding files here. /etc All the configuration files for the various software are stored here. Don't play with this directory. /home All users will have their 'My Documents' under this directory. If your id is tomh, your 'My Documents' (called home-directory) is /home/tomh. /lib The libraries required by system-applications. (Just like DLLs in Windows.) /lost+found When a disk-check finds files which are damaged or which are not linked to any directory, they are recovered to this directory. Such damages are almost always due to incorrect shutdown. /misc Miscellaneous files! /mnt The directory where peripherals and other file-systems are mounted. /opt The directory where optional software are installed. /proc proc houses a pseudo-filesystem. Its contents really do not exist anywhere on the disk, and are made available only when you cd to this directory and look at some file. Don't worry about it, anyway. /root The home-directory for the super-user: root. /sbin The system-administration binaries exist here. /tmp The directory where temporary files are created and stored. /usr Everything related to users! /usr/bin /bin houses critical binaries, whereas /usr/bin stores other binaries: not so critical but required nevertheless. /usr/include The header-files required by programs for compilation. /usr/lib The libraries required by user-applications. /usr/local Files peculiar to this particular machine. /usr/sbin User-administration binaries. /usr/share Information that can be shared by most users. /usr/src The source-code for the Linux kernel. /usr/X11R6 Files needed by the X Window system. /var Files whose contents vary frequently are in this directory. /var/log The log-files of the system. /var/spool Directories for mail, news, printing and other queued work. *********************************************************************** ------------------------------------------------------------------- SOME MISCELLANEOUS NOTES BEFORE YOU CAN START CHECKING OUT COMMANDS =================================================================== --------------------------------- * AND ?: TWO IMPORTANT WILD-CARDS --------------------------------- When using the command-line, you will be making use of the '*' and the '?' characters quite often. If you are new to these symbols, here are their meanings. If you have already used them, you can skip this. * means 'match any number of characters'. For example, chap* matches: chap01, chapa, chap_end, and also chap. Similarly, *er matches wonder, maker, goner, der, and even er. If you just give * (nothing else), it matches every file. ? means 'match one character'. For example, chap? matches: chapa and chap1, but _not_ chap01 and chapab. ------------------------- SHORT-CUTS TO DIRECTORIES ------------------------- Whenever you have to give a directory name, you may find these shortcuts useful: . indicates current directory. .. indicates parent directory. ~ indicates home directory. ----------------- THREE USEFUL TIPS ----------------- I. If the output of a command scrolls too fast and you missed something, press SHIFT+PAGEUP key to scroll back up. II. You need not type the full name of a file or a command: the TAB key has an autocomplete feature in bash. For example, let's say you have a file one_awefully_long_name_file in your home directory, and want to look at its contents. You can say: $ cat one[TAB] Bingo, the file name will be completed, provided no other file has its name starting from 'one'. This tip can be applied any time you have to enter a file-name or path-name at the command-prompt. Similarly, you can also autocomplete commands also: $ Xc[TAB] will automatically insert: $ Xconfigurator As if that were not enough, pressing TAB twice will list all the files/commands starting with that pattern. Try these: $ cat a[TAB][TAB] $ X[TAB][TAB] III. You can work in multiple terminals, running KDE in one and pure console on another. Press CTRL+ALT+F2. See the login prompt? Usually there will be six such virtual terminals. You can be using all of them. Press CTRL+ALT+F-keys to switch. CTRL+ALT+F7 returns you to X if you were originally using X Window like KDE/GNOME. *********************************************************************** ------- CAVEATS ======= o The Linux rm command (to remove a file) permanently deletes a file. There is no recycle bin. o The Linux commands to create, copy, move and rename files overwrite existing files. They don't ask you for confirmation. o Most Linux commands do not needlessly clutter the screen. For example, if you compare two files and there are no differences found, you simply get back the prompt. Unlike DOS, you don't see 'No differences found'. o Since a super-user has enormous powers, never work under Linux as a super-user. Always create an ordinary account for yourself (the command is: useradd id_name), set its password (command is: passwd id_name), and work under this account. Reserve super-user log-in only for administrative tasks. o Just like Windows, you should not shut down Linux by simply pressing the POWER button on your PC cabinet. It will seriously harm the file-system. Always use the halt or shutdown command. *********************************************************************** -------------------------- ELEMENTARY TROUBLESHOOTING ========================== If a command causes an error message like the one below: bash: : Command not found then try these steps: 1. Did you type it all right? 2. Check whether the executable for the command has been installed with the 'whereis' command. As an example, let us apply whereis for useradd: $ whereis useradd useradd: /usr/sbin/useradd /usr/share/man/man8/useradd.8.gz $ _ So the command is present, but in a different directory. You can execute this command by typing: /usr/sbin/useradd 3. If step 2 results in no entries, then you must install the package. Insert the Linux CD, and install the package with the 'rpm -i' command. Note that this command requires you to be a super-user. If a command doesn't work the way you want it, then try these steps: 1. Have you given the correct arguments (the words you type after the name of the command) to the command? 2. Did you type them all right? 3. Some commands don't work for ordinary users. The useradd command above can be executed only by super-user. It makes no sense if everybody could add users, would it? You can press CTRL-c at any time in order to abort the execution of the current command. Press CTRL-u if you made too many typos. This file covered the basics of the Linux command-line environment, and has armed you with enough competence to start trying the commands. The next file will list the important commands, so that you can actually experiment and learn.