<<

Chapter 1

Unix introduction

I’ll begin these musings with the briefest of brief introductions to the operat- ing system called . If you already know all about it, or if you’re using Windows, you can safely skip this chapter.

You execute unix commands through a . A shell is a program which lets the user input commands and then executes these commands. So, in order to start doing something, you need a shell. It could be an xterm. When you log into the DAIMI system, a shell probably starts automatically. You can usually scroll through the commands you have issued so far by using the up- and down- cursor keys. In the DAIMI system, your files and directories (folders) are organized in a structure. The root of this tree is your home : /users//, and its branches are subdirectories. Inside this and all its subdirectories, all your files reside. By convention, the tree is ori- ented upside down, so that ”moving up” in the tree means moving one level closer to the root. Thinking in terms of ancestral trees, each directory in the tree has a unique parent directory (the one it resides in) but may have several child directories (the ones it contains). Your shell opererates with a current (WD) which is the directory where you currently ”are”. When you begin, the WD is your root di- rectory. Any file or directory has a which uniquely identifies it in the tree, e.g., /users/chili/public html/PBI05/unix.html. A path can be absolute, like the one above, or it can be relative. If it’s relative, the WD is taken as a starting point, so the path Images/me in Nepal.jpg refers to the file me in Nepal.jpg inside the directory Images inside the current working directory. All files and directories have permissions associated with them: Permission to , , or ecxecute. You, as owner, can set these permissions. You can set different permissions for yourself and all others. The first of the following two tables present some commands which I strongly suggest you learn to master immediately. The second presents some very use- ful commands that you’ll probably eventually learn anyway. In every filename, you may use a * as a joker to mean ”any combination of characters”. Thus, ls *.py lists all files in the WD ending with .py.

1 2 CHAPTER 1. UNIX INTRODUCTION

Gives the absolute path to the current WD.

Changes the WD to .

cd .. Changes the WD to the parent directory of the cur- rent WD (move up one level).

mkdir

Makes a new directory inside the WD.

rmdir

Removes the (empty) directory .

more Prints the contents of the file

- press Space or the cursor keys to scroll. If you press / you can type a string, and when you press Enter, the (rest of the) file is searched for this string.

ls Lists the contents of the WD.

ls

Lists the contents of .

ls -l Like ls but lists the directory contents in tabular form with file information.

mv Moves the file/directory : If the directory exists, is moved there. If not, is renamed to .

cp Copies the file : If the directory exists, a copy of is created there. Other- wise, a copy of is created with the name .

rm Removes the file .

∼/ Shorthand for your root directory.

/ Same as the root directory of user .

chmod Changes the permissions for the file/directory according to which should be a sequence of letter/code/action(s) triplets:

• The letter a means all users, the letter u means user (you only)

• The code + means permission is granted, the code - means permission is denied

• The actions r, w, x mean read, write, execute, respectively

Thus, the mode a+rx means that all users are granted read and execute access. 3

| Pipes the output from into . I.e., the output from the first program is sent as input to the second program.

> Redirects the output from into the file - since it is redirected to the file rather than to your screen, you won’t see any output. If already exists, it will be overwritten! cat Prints the entire contents of the file . man Show documentation for the command .

| tee Saves the output from in the file (which is overwritten if it exists), but also show the output on your screen. top Lists the programs running on your computer currently demanding the most CPU power. Each program (job) name is in the right-most column, its owner is in the second column. If your computer is running very slowly because someone else is running a heavy job on it, you can send an email asking him/her to stop it. Type ’q’ to quit top. grep Report all lines in the file , or in all files in the directory , containing the string (put quotes around it if it contains special characters). grep -c As above but only report the number of lines containing the pattern string. grep -f Search in the file , or in all files in the di- rectory , for all patterns found in the file . Each line is interpreted as a pattern. grep is a very useful command; do a man grep to learn more.

| cut -d ”” Split each line in the output of the command -f into fields delimited at each occurrence of the character and report only those fields listed in . E.g., cat mylist.txt | cut -d ” ” -f2,4 lists the second and fourth fields of each line in the file mylist.txt after splitting it at each space.

| sort Sort the output from line by line.

| sort -u Sort output but ignore duplicate lines. 4 CHAPTER 1. UNIX INTRODUCTION

You can guess the remainder of a file or directory name you have started typing with the Tab key: Type the first few letters of the name and the press Tab; then your shell auto-completes the name. If there are several options, it will beep and expect more characters to be typed in. You can use . for the current working directory. This is nice, e.g. when you wish to copy or move something “here”. If you want to learn more or need further explanation, an excellent series of Unix tutorials for beginners is available at http://www.ee.surrey.ac. uk/Teaching/Unix/index.html The first five tutorials cover the most ba- sic stuff.