A Small Linux Primer for Lab Members
To start, don’t be scared of Linux. In fact, you may already be dealing with a Linux based OS. For example, all Mac computers these days are on their “OSX” operating system, which is is a Linux variant. And any phone that is android powered is on a Linux base. So now that we have that out of the way, let’s get to some fun stuff….The best way to get used to Linux is to jump on and try it.
Switching the computers on and off
Rule one is don’t switch Linux computers off. Linux computers can run reliably for days, weeks or months at a time without needing a reboot. This may be a bit of a getting used to for MAC/Windows users. More importantly, Linux is a multi-user operating system. This means that while you are physically working on a particular Linux computer, other users may also be logged in through the network. Switching off the computer may cause some angry words from other users that were using, or planning to use, the computer over the network. A good way to see who may be logged into the computer you are working on is to open up a terminal and type “who” at the command prompt.
Given the above, you should normally not have to switch a Linux computer on. If the computer seems to be switched off, then first make sure it really is switched off. Someone may just have switched off the monitor or the screen may use a black screen saver. It is best to ask the person responsible for the computer whether it is okay to switch it on. To switch on the computer just power it up and after a minute or two you should be ready to log on.
Logging on and off
If a computer is not in use you should see either a blank screen (the screen saver in more recent versions of the Fedora Linux OS) or a login window. If you see the login window, you can select your login ID and start working. If you see the blank screen saver, you can move the mouse and you will be prompted for a password (if it was you who locked the screen), or you will be given the chance to “switch” users…that is login to the computer while the other person is still logged in. If this is the case, general courtesy is to ask the other person’s permission to use that system before logging in. Screens on systems for general use should not be locked.
To log off you click on your user name at the top right of the screen and select the Log out or Lock Screen options. Click the “Yes”-button or hit return to confirm that you want to lock the screen/log out. A blank screen with login window will appear, ready to log in the next user.
Our computers use the “GNOME” desktop, a component of RedHat/Fedora Linux flavours, and is one of many choices. Why Fedora/GNOME? Like many things in software etc, I’ve been using it for years, so there is a familiarity about it. Others may suggest Ubuntu or Debian as “better” linux variants, but each have their own strengths and drawbacks. A strength of GNOME that I’ve found is that you can extensively personalize it; the current version of GNOME is v3, which is quite similar to either Mac OSX or newer versions of Windows (7, 8 & 10). If your passion is to customize your desktop, I leave it up to you to figure out how to do it. The basic installation has a “Favorites” bar on the left side of the screen, and a task bar at the top with some buttons which you can explore. From the task bar, you can select either “Applications” or “Windows”, which will bring up the application icons or windows in use, respectively. It is often useful to work in several windows if you are doing multiple tasks (for example email/net surfing in the top window and crystallographic work in a lower window). Windows in GNOME3 tile vertically, so you can move applications “up” or “down”. One can also use “Alt + Tab” to get an application selector for all active applications, which is another way to quickly navigate the desktop.
The favorites bar will have not only several standard applications, but can also have user defined applications added to it. It will have things like a net browser (Firefox), email (Evolution/Thunderbird), and file browser, but you can also add things from the applications list, such as the “terminal”, which is very handy to have as a favorite when you are doing multiple tasks; it is from a terminal that the real power of a Linux system is achieved (discussed below).
One of the strengths of the Linux system (and Unix systems in general) is the ability to do things from the command line. We don’t tend to think this with all the point and touch stuff we deal with now … think our I-Pads and phones. But working from the command line is a very efficient way to do things, and so it is something you should at least not be scared of.
The file system
Local file system
In Windows you can have multiple drives (A:, B:, C:, etc), each with files, directories, sub-directories… In Linux all files are stored in a single “file system” starting with the root directory which is indicated as “/”. Within the root directory there are a large number of sub-directories. The bin, boot, dev, etc, lib, lost+found, opt, proc, root, sbin, tmp, usr, and var directories are important for the operating system but not of real interest to the user. The mnt directory is used to access floppy drives and the CDROM which will be discussed later. Finally each computer will have a home directory where the user(s) of that computer keep their files.
A User’s Home Directory
When you login to a Linux box through the graphical interface, you will end up in your home directory, which is /home/USER – For the user “gerald”, this would be /home/gerald. The first time you log in the system will install several OS-determined “standard” directories. These are Desktop, Documents, Downloads, Music, Pictures, Public, Templates, and Videos, and the contents of which are fairly self-explanatory – for instance /Downloads is /home/USER/Downloads and is the default location for things you download from the web in Firefox. A note about GNOME3 is that the Desktop directory is not really used as in previous versions of GNOME; the Public directory is where one would put the files for a web-page, however the computers in the lab are not setup to allow website hosting, so it is redundant.
It is a good idea when one first logs in is to set up a few directory that are recognizable to you for certain things. For example, it is a good idea to have a data directory in which all the lab work can be put, using appropriate sub-directories. Think of it as keeping things organized, and your home directory uncluttered. This works in well in theory, but takes some thinking in practice (and a cleanup every now and then).
Network file system
In addition to the standard directories listed above, there can (and will) be several more, each with the name of a computer in the lab. These directories give access to the /home directory of the corresponding computer. Although these directories are stored on a hard disk (HDD) on a different computer, they behave identical to the ones on your local HDD thanks to the Network File System (NFS) software. For consistency, there is also a directory with the name of the local computer (e.g. a directory /thorin on the PC thorin). On a computer named XXX, /home and /XXX both refer to the same directory. One particularly important directory that is over NFS is /backups. This is where the nightly backups of a user’s home directory, X-ray data (from both the lab’s Micromax 007HF and any synchrotron data), and wet-lab data (from the PCs) are stored, and can be used to rescue your data if something goes sideways.
Navigating the file system
To move around the file system you use the cd (Change Directory) command. For instance:
|cd /||Move to the root directory|
|cd /home||Move to the local /home directory|
|cd /thorin||Move to the /home directory on thorin|
|cd /backups||Move to the /backups directory on gondolin|
|cd /backups/gerald||Move to the /backups/gerald directory on gondolin|
The examples above all start with the “/” symbol and you will move to whatever directory you specified. If you are already in a directory, let’s say /thorin/gerald, then you can jump to a sub-directory, let’s say /thorin/gerald/data, by typing either:
|cd /thorin/gerald/dataor cd data|
The latter is an example of a relative pathname; relative to the current directory that is. In each directory there are always two files named “.” and “..“. The former is an alias to the current directory and the latter is an alias to the parent directory. To go from directory /thorin/gerald/data to /thorin/gerald you can therefore use either of the following:
cd /thorin/gerald or cd ..
Finally, each user will have a home directory which is a sub-directory of /home of the computer of that user. When you log in you will automatically start out in your home directory. If at any time you will want to return to your home directory you just type cd.
We have already discussed the cd command to move around the file system. Here are a few more that you will be using all the time. (The command is printed bold. You have to replace the example file names in italics by real ones).
|ls||List the contents of the current directory|
|mv oldname newname||Move (rename) a file|
|rm junk||Remove (delete) a file|
|cp original replica||Copy a file|
|mkdir dirname||Make a new directory|
|rmdir junkdir||Remove an empty directory. To remove a directory with its contents and all subdirectories you use rm -rf junkdir, but you better be sure before you do this as there is no way back.|
|ln -s realname linkname||Make a symbolic link. After this command you can use realname and linkname interchangeably.|
|history||List all commands you have given so far|
|df||Show how much space is left on the hard disk(s)|
|du||Show how much space is use by the current directory and all its subdirectories|
|man command||Display a manual page explaining the use of the requested command. Use this to find out more about the commands above|
The vi text editor
A frequent task will be to inspect, create, and/or modify text files. The cool way to do this is with the program emacs. Although I have promised myself to learn this for some time, it still hasn’t happened, so you are on your own on this one. The “windowsy” way of doing it is with gedit. This is easy but very slow on large files and not nearly as versatile. The classical unix way of doing it is with the program vi. This is fast, powerful and, after some getting used to, efficient.
To start, just type vi file.txt. This brings up the text of the file named file.txt. From this point on you can navigate through the document, edit the document, issue commands, or search for a word or phrase. Each of these classes of actions require you to be in the appropriate “mode”. These will be explained below.
In command mode you can issue commands to navigate through your text file, to alter your document or to take other actions. Many commands consist of pressing one or more characters, possibly preceded by a number. Adding the number acts to repeat the command the requested number of times. Here are the commands to move around:
|h||move to left (you can also use the left cursor)|
|j||move to down (you can also use the down cursor)|
|k||move to up (you can also use the up cursor)|
|l||move to right (you can also use the right cursor)|
|(HDD)||(You can prepend a number to move that number of lines or characters.E.g. 3j will move three lines down)|
|w||move to the start of the next word|
|b||move to the start of the current word|
|#G||move to line number # (If you don’t give a number, it will go to the end of the file)|
|$||jump to the end of the line|
|^||jump to the beginning of the line|
Here are the commands to cut, copy, and paste text. Note that many consist of the character “d” followed by a move command listed above. All characters between the current cursor position and the place moved to will be deleted.
|#x||cut # characters and place them into the paste buffer|
|#dd||delete # lines of text and place them into the paste buffer|
|#dw||delete # words of text and place them into the paste buffer|
|d$||delete to end of line and place them into the paste buffer|
|d^||delete to beginning of line and place them into the paste buffer|
|dG||delete to the end of file|
|d#G||delete from current line to line number #|
|#y||copy # words starting at the cursor position into the paste buffer|
|#yy||copy # lines starting at the current line into the paste buffer|
|y$||copy to the end of file into the paste buffer|
|y^||copy to the beginning of line into the paste buffer|
|p||insert the paste buffer content before the cursor position|
|P||insert the paste buffer content after the cursor position|
To find a particular word or phrase in the document just type /XXX where XXX is the word you are looking for. If you know the word XXX is a the beginning or end of the line you can use /^XXX and /XXX$, respectively.
To actually type in new text you have to go into “insert mode”. You do this by giving any of the following commands.
|i||Start inserting at the cursor position|
|I||Start inserting at the beginning of the line|
|a||Start inserting after the cursor position|
|A||Start inserting at the end of the line|
|o||Create and start inserting into a new line below the current one|
|O||Create and start inserting into a new line above the current one|
|r||Replace (overwrite) the one character under the cursor by the next character to be typed|
|R||Replace (overwrite) the text starting at the cursor position|
After giving any of these commands you have entered insert mode (except for “r” which returns to command mode after you changed the one character). In insert mode you can start typing. In “modern” versions of vi, such as the one on linux, you can use the cursors to walk around while typing. You can also delete text you just typed with the backspace key. However, you cannot delete text that had been typed previously. For that you have to use the delete commands given above. When you are ready typing in text you press the Escape button to return to command mode.
The final set of commands you could call “colon-commands”. That means you first type a colon followed by the command. These commands are normally use for more complicated operations such as:
|:w||write the text to disk, overwriting the original file|
|:w XXX||write the text to a file named XXX (XXX should not yet exist)|
|:w! XXX||(over)write the text to an existing file named XXX|
|:r XXX||read in file XXX and insert its context below the current line|
|:q||quit vi (all changes must have been saved)|
|:q!||quit vi even if you have not saved your changes|
|:wq||save and quit (you can also use ZZ)|
|:s /XXX/YYY||substitute the first occurrence of string XXX with YYY in the current line|
|:s /XXX/YYY/g||substitute all occurrences of string XXX with YYY in the current line|
|%s /XXX/YYY/g||substitute all occurrences of string XXX with YYY in all lines|
There are many more things to learn about vi, but with the above you can do most things that you’ll come across. Use the man page for vi to find out more.
Please note that this page is under reconstruction, and is updated on a sort-or regular basis. It has been adapted from Bart Hazes’ linux introduction with permission.