GNU/Linux Basics for Physics 105AL and COSMOS Peter Jay Salzman
p@dirac.org
2002-10-2 ver 0.1 2000 Peter Jay Salzman Students taking Phys 105AL and COSMOS at UCD will be using Linux for the coursework. This tutorial was designed to bring students who have little or no experience with GNU/Linux up to speed. It certainly is not meant to be comprehensive or even completely accurate; for that, you can buy a book. I take a more pragmatic approach which will concentrate on the types of things a Phys 105 or COSMOS student needs to know.
What Is GNU/Linux? Linux is a free implementation of the Unix operating system written by Linus Torvalds. Techically, Linux only refers to a single program called the kernel which controls the computer hardware. The operating system consists of the Linux kernel, along with a huge collection of other programs like ls and gcc. These other programs were written by the Free Software Foundation (FSF) and are called the GNU operating system. The Linux kernel along with the GNU operating system is called GNU/Linux. Confusingly, many people shorten this to just `Linux'. There are a number of companies that package the GNU/Linux operating system. You can buy GNU/Linux from them (or download it for free). All these companies use the same basic ingredients: the Linux kernel and the GNU operating system. However, they differ somewhat from each other in the way things are ``arranged''. The different flavors of Linux are called distributions (or distros for short). The most popular ones are Redhat (is popular among businesses), Mandrake (popular among home users in the USA), Suse (popular among home and business users outside the USA) and Debian (popular among computer geeks). Although I used to use Redhat and Suse, I now use Debian. If you know your way around Unix, you know your way around Linux. It's also similar to DOS, so if you know DOS, you're ahead of the game. There are numerous books, FAQ's and Internet documents on Unix, so I won't get into the details. The best thing to do is to ask a friend if you're truly lost. I'll highlight the main points of what you need to get started. You should've received a login and password for lifshitz or the room 106 computers from the Physics Department. If not, you need to speak with your teaching assistant, professoror, or Mike Hannon. Find an empty computer in room 106. To log into the computer, enter your login name at the login: prompt. Your login is probably the first letter of your first name followed by your last name. Enter your password at the password: prompt. Click "OK" when the motd (message of the day) pop-up window appears. After a few seconds, you should see a desktop which looks vaguely like, but different from, Microsoft Windows. Linux has two major desktop managers, called GNOME and KDE. You're staring at the GNOME desktop. Now What? What you're staring at is not so different from a DOS prompt. There are a few basic commands that you'd need to know to get started. Here they are: ls filename list a file in your current directory ls directory list all the files in directory directory cp file1 file2 copy file1 to file2 rm file1 delete file1 mv file1 file2 rename file1 to file2 mv file1 directory move file1 into directory directory mkdir directory make a new directory called directory That's pretty much it. There are tons of small details which you'll pick up as you get familiar with Linux. For example, rm file1 file2 file3 will delete all the files, file1, file2, and file3 listed after the rm command. You'll pick these things up as you go along. Linux was written by programmers who were mostly concerned with making Linux stable, useful and fast to use. Anything that you think is a good idea has already been implemented under Linux. There are other things that you'll have to know, like how to use the vi/vim editor or how to compile your programs. But these are covered in other tutorials I've written. Directory Structure You'll have to be familiar with the Linux directory structure to do anything useful. The directory structure is similar to DOS, so if you know DOS, you're set. The directory structure is hierarchial. The top directory is called the "root" directory and is called / (the forward slash). Below it are other directories, for example, bin is a directory in the root directory: /bin and ls is a file in bin: The only thing that's a bit confusing is that the slash / is a separator between directories and files, except when its at the beginning. When / is at the beginning, it stands for the root directory. As one last example, the document I'm typing is on my home computer. It is: /home/p/TA/basic_linux home is a directory in the root directory. p is a directory in home. TA is a directory in p. And basic_linux is a file in p. When you specify a file using all the directories, starting at /, it's called a "pathname". So one way of specifying basic_linux is either by it's name: basic_linux or by its full pathname: /home/p/TA/basic_linux There are two files in every directory: . means your current directory .. means the directory before the current directory. So to be perverse, here are two more ways to refer to basic_linux: ./basic_linux ../TA/basic_linux since . here means /home/p/TA (the current directory) and .. means /home/p (the directory above the current directory. Can you see why the following also refers to basic_linux? ../../../home/p/../p/TA/./basic_linux If you can understand this, then you're definitely doing well! You can find out what directory you're currently in with the pwd: $ pwd /home/p/Academia/TA/Tutorials/basic_linux Try it. Now try typing cd .. and use pwd to see what directory you're in now. $ cd .. $ pwd /home/p/Academia/TA/Tutorials Now type pwd again and see if it prints what you'd expect. To return to the last directory you were in, you can use the cd -. $ cd - $ pwd /home/p/Academia/TA/Tutorials/basic_linux Directories or Files? If you type ls, you may see something like: $ ls MP3 notes Mail nsmail News personal Office51 pinochle.html a.out pppdman anint.nb spherical_expanding_box.nb Believe it or not, some of these are files and some are directories. That's because in Unix (and Windows too) directories are actually files. Special files, but still files. How can you tell which ones are files and which are directories? Type ls -F. You'll see something like: $ ls -F MP3 notes/ Mail/ nsmail/ News/ personal/ Office51/ pinochle.html a.out* pppdman anint.nb spherical_expanding_box.nb Anything with a * is an executable, something you can run. Anything followed by a / is a directory. Everything else that you'll see is a normal file. Moral to the story is use "ls -F" instead of just "ls". You can also get clues by the filename suffix. Under Unix, suffixes aren't as tightly bound to filetypes as they are under Windows (technically not true anymore. If you run GNOME under Linux, they ARE as tightly bound as they are under Windows). This means that index.html doesn't necessarily have to be a web page, but chances are VERY good that it is. With the exception of GNOME, filename suffixes are hints to the user, not the application. If you're totally stumped by what exactly a file is, you can use the "file" command. I'll illustrate: $ file pppdman pppdman: International language text $ file a.out a.out: ELF 32-bit LSB executable, Intel 80386, version 1, dynamically linked (uses shared libs), not stripped $ file admin admin: directory The $ in this example is my prompt. Running Executables Just like DOS, Linux commands like ls and cp are programs that reside on the computer. Most of the main system commands (like ls and cp) reside in /bin. So when you type "ls" you're actually running an executable named ls in the /bin directory. During this course, you'll be writing programs, and these are executables too (once you compiled them). In fact, in principle, your programs will be very much like the ls and cp executables. They'll just do different things. To run an executable, simply type its name. Like you do with ls. An important question is: How does the computer know where to look for the executable? How does the computer KNOW that ls is located in /bin? After all, Linux has to know where ls is in order to run it. This is a highly non trivial problem. My computer, a small home PC, contains 61486 directories. There are two ways Linux looks for your executable. The first way is if you specify a full pathname. So, if you type /bin/ls or /usr/sbin/useradd Linux will look for the ls executable in /bin and the useradd executable in /usr/sbin. By the way, if you type ./ls or ./useradd then that's equivalent to specifying a full pathname since . means "the current directory". In this case, Linux will look for ls and useradd in your current directory. If you DON'T give a full pathname, like: ls or useradd then Linux looks for the executable in your PATH. PATH is a variable which is very similar to the PATH variable in DOS. PATH holds a bunch of directories separated by colons. Type: echo $PATH on my system, I get: $ echo $PATH /bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/X11R6/bin:. When I type "ls", Linux looks for an executable name "ls" first in /bin. If it doesn't find "ls" there, it'll look in /bin. If it doesn't find "ls" in /bin, it'll look in /sbin. And so on. It'll keep going down the list till it hits the last directory in the PATH, which is "." (see that?). And remember, "." means your current directory. Suppose you write a program, compile it and now you have an executable named "integrate" in your current directory. To run it, simply type its name. If you see something like: # integrate bash: integrate: command not found then it means one of two things: integrate is not in your current directory. integrate IS in your current directory, but . isn't in your PATH. Type "ls" to make sure integrate is in your current directory. If it isn't, use cd to change into the directory containing integrate. If integrate is in your current directory, then you have to do one of two things: Specify the full pathname for integrate. For example, ./integrate or /home/mydirectory/integrate Add . in your PATH. To add . in your PATH, type PATH=$PATH:. Making Your Changes Permanent Everytime you log into Linux, it runs a file called .bash_profile. Suppose you want to permanently add . to your PATH. Add the following line to .bash_profile: PATH=$PATH:. Suppose you always want to type ls but have it do ls -F instead. Add the following line to .bash_profile: alias ls='ls -F'