Presentation on theme: "UNIX AN INTRODUCTION Compiled by :"— Presentation transcript:
1UNIX AN INTRODUCTION Compiled by : S. Agarwal, Lecturer & Systems InchargeSt. Xavier’s Computer Centre,St. Xavier’s CollegeKolkata.Compiled by S. Agarwal, SXCC, Kolkata.
2What is UNIX : UNIX is an operating system. An operating system is the program that controls all the other parts of a computer system, both the hardware and the software. It allocates the computer's resources and schedules tasks. It allows you to make use of the facilities provided by the system. Every computer requires an operating system.Compiled by S. Agarwal, SXCC, Kolkata.
3What is LINUX :Linux is a free Unix-type operating system originally created by Linus Torvalds with the assistance of developers around the world. Developed under the GNU General Public License , the source code for Linux is freely available to everyone. The commands & functions of linux are similar to unix.Compiled by S. Agarwal, SXCC, Kolkata.
4Features of UNIX :UNIX is a multi-user, multi-tasking operating system. Multiple users may have multiple tasks running simultaneously. This is very different than PC operating systems.UNIX is a machine independent operating system. Not specific to just one type of computer hardware. Designed from the beginning to be independent of the computer hardware.UNIX is a software development environment. Was born in and designed to function within this type of environment.Compiled by S. Agarwal, SXCC, Kolkata.
5The UNIX operating system is made up of three parts; the kernel, the shell and the programs. The kernel of UNIX is the hub of the operating system: it allocates time and memory to programs and handles the filestore and communications in response to system calls.Compiled by S. Agarwal, SXCC, Kolkata.
6The shellThe shell acts as an interface between the user and the kernel. When a user logs in, the login program checks the username and password, and then starts another program called the shell. The shell is a command line interpreter (CLI). It interprets the commands the user types in and arranges for them to be carried out. The commands are themselves programs: when they terminate, the shell gives the user another prompt (% on our systems).Compiled by S. Agarwal, SXCC, Kolkata.
7The shell is not only an interpreter for your interactive commands, which you type at the prompt. It is also a powerful programming language, which allows you to write shell scripts, to ``batch'' several shell commands together in a file. MS-DOS users will recognize the similarity to ``batch files''. Use of shell scripts is a very powerful tool, which will allow you to automate and expand your usage of UNIX.Compiled by S. Agarwal, SXCC, Kolkata.
8Bourne shell (sh)This is the original Unix shell written by Steve Bourne of Bell Labs. It is available on all UNIX systems.This shell does not have the interactive facilites provided by modern shells such as the C shell and Korn shell. The Bourne shell does provide an easy to use language with which you can write shell scripts.Compiled by S. Agarwal, SXCC, Kolkata.
9There are several types of shells in the UNIX world There are several types of shells in the UNIX world. The two major types are the ``Bourne shell'' and the ``C shell''.The Bourne shell uses a command syntax like the original shell on early UNIX systems. The name of the Bourne shell on most UNIX systems is /bin/sh (where sh stands for ``shell'').The C shell uses a different syntax, somewhat like the programming language C, and on most UNIX systems is named /bin/csh.Compiled by S. Agarwal, SXCC, Kolkata.
10Under Linux, there are several variations of these shells available. The two most commonly used are the Bourne Again Shell, or ``Bash'' (/bin/bash), and Tcsh (/bin/tcsh).Bash is a form of the Bourne shell with many of the advanced features found in the C shell. Because Bash supports a superset of the Bourne shell syntax, any shell scripts written in the standard Bourne shell should work with Bash.For those who prefer to use the C shell syntax, Linux supports Tcsh, which is an expanded version of the original C shell.Compiled by S. Agarwal, SXCC, Kolkata.
11Files and processes Everything in UNIX is either a file or a process. A process is an executing program identified by a unique PID (process identifier).A file is a collection of data. They are created by users using text editors, running compilers etc.Examples of files:· a document (report, essay etc.)· the text of a program written in some high-level programming language· instructions comprehensible directly to the machine and incomprehensible to a casual user, for example, a collection of binary digits (an executable or binary file);· a directory, containing information about its contents, which may be a mixture of other directories (subdirectories) and ordinary files.Compiled by S. Agarwal, SXCC, Kolkata.
12Accessing a UNIX System There are many ways that you can access a UNIX system. The main mode of access to a UNIX machine is through a terminal, which usually includes a keyboard, and a video monitor. For each terminal connected to the UNIX system, the kernel runs a process called a tty that accepts input from the terminal, and sends output to the terminal. Tty processes are general programs, and must be told the capabilities of the terminal in order to correctly read from, and write to, the terminal. If the tty process receives incorrect information about the terminal type, unexpected results can occur.Compiled by S. Agarwal, SXCC, Kolkata.
13ConsoleEvery UNIX system has a main console that is connected directly to the machine. The console is a special type of terminal that is recognized when the system is started. Some UNIX system operations must be performed at the console. Typically, the console is only accessible by the system operators, and administrators.Compiled by S. Agarwal, SXCC, Kolkata.
14Logging In and Logging Out To ensure security and organization on a system with many users, UNIX machines employ a system of user accounts. The user accounting features of UNIX provide a basis for analysis and control of system resources, preventing any user from taking up more than his or her share, and preventing unauthorized people from accessing the system. Every user of a UNIX system must get permission by some access control mechanism.Compiled by S. Agarwal, SXCC, Kolkata.
15Logging inLogging in to a UNIX system requires two pieces of information: A username, and a password. When you sit down for a UNIX session, you are given a login prompt that looks like this:login:Type your username at the login prompt, and press the return key. The system will then ask you for your password. When you type your password, the screen will not display what you type.Compiled by S. Agarwal, SXCC, Kolkata.
16Your username Your password Your username is assigned by the person who creates your account. Your username must be unique on the system where your account exists since it is the means by which you are identified on the system.Your passwordWhen your account is created, a password is assigned. The first thing you should do is change your password, using the passwd utility. To change your password, type the commandpasswdCompiled by S. Agarwal, SXCC, Kolkata.
17Logging Out When you're ready to quit, type the command exitBefore you leave your terminal, make sure that you see the login prompt, indicating that you have successfully logged out. If you have left any unresolved processes, the UNIX system will require you to resolve them before it will let you log out. Some shells will recognize other commands to log you out, like "logout" or even "bye".Compiled by S. Agarwal, SXCC, Kolkata.
18UNIX FILESYSTEMThe UNIX filesystem is heirarchical (resembling a tree structure). The tree is anchored at a place called the root, designated by a slash "/". Every item in the UNIX filesystem tree is either a file, or a directory. A directory is like a file folder. A directory can contain files, and other directories. A directory contained within another is called the child of the other. A directory in the filesystem tree may have many children, but it can only have one parent. A file can hold information, but cannot contain other files, or directories.Compiled by S. Agarwal, SXCC, Kolkata.
20/(Called root), this is equivalent to C:\ in the DOS/Windows world. You cannot run a Linux system without the root partition. All other partitions are a subset of the root partition./bootThis contains the necessary stuff to start the machine, including the base kernel. This partition is optional, but usually present. You will not need to mess around in here if you have a running system./usrThis is the directory where global executables are stored. It can be read-only, if you want. Generally speaking, most software is installed here by default./devThis is the directory where all of your devices are. There are a few useful examples for you to know. /dev/hda is the first ide hard drive. /dev/hdb is the second. /dev/sda would be the first SCSI drive, and /dev/sg0 is your robotic arm./etcThis is where most configuration files are stored. You will spend a lot of time in here if you are an administrator. Most files require "root" access to change./varMany of the system log files are here, as well as spools (mail, printer...)/binThis directory is the home of binary executables. These include the common commands we have already learned like ls, cat, gzip and tar.Compiled by S. Agarwal, SXCC, Kolkata.
21To decribe a specific location in the filesystem heirarchy, you must specify a "path." The path to a location can be defined as an absolute path from the root anchor point, or as a relative path, starting from the current location. When specifying a path, you simply trace a route through the filesystem tree, listing the sequence of directories you pass through as you go from one point to another. Each directory listed in the sequence is separated by a slash.UNIX provides the shorthand notation of "." to refer to the current location, and ".." to refer to the parent directory.Compiled by S. Agarwal, SXCC, Kolkata.
22/users/admin/jon ../admin/jon The absolute path to the directory named "jon" would be:/users/admin/jonThe relative path from the directory named "student" to the directory named "jon" in the tree diagram would be:../admin/jonCompiled by S. Agarwal, SXCC, Kolkata.
23UNIX COMMANDSCompiled by S. Agarwal, SXCC, Kolkata.
24ls (list) % ls (short for list) When you first login, your current working directory is your home directory. Your home directory has the same name as your user-name, and it is where your personal files and subdirectories are saved.To find out what is in your home directory, type% ls (short for list)The ls command lists the contents of your current working directory.Compiled by S. Agarwal, SXCC, Kolkata.
25There may be no files visible in your home directory, in which case, the UNIX prompt will be returned. Alternatively, there may already be some files inserted by the System Administrator when your account was created.ls does not, in fact, cause all the files in your home directory to be listed, but only those ones whose name does not begin with a dot (.) Files beginning with a dot (.) are known as hidden files and usually contain important program configuration information. They are hidden because you should not change them unless you are very familiar with UNIX!!!Compiled by S. Agarwal, SXCC, Kolkata.
26% ls –a ls can take options: To list all files in your home directory including those whose names begin with a dot, type% ls –als can take options:-a is an example of an option. The options change the behaviour of the command. There are online manual pages that tell you which options a particular command can take, and how each option modifies the behaviour of the command.Compiled by S. Agarwal, SXCC, Kolkata.
27The characters * and ? % ls list* % ls *list % ls ?list The character * is called a wildcard, and will match against none or more character(s) in a file (or directory) name. For example, in your unixstuff directory, type% ls list*This will list all files in the current directory starting with list....% ls *listThis will list all files in the current directory ending with ....listThe character ? will match exactly one character. So ls ?ouse will match files like house and mouse, but not grouse.% ls ?listCompiled by S. Agarwal, SXCC, Kolkata.
28mkdir (make directory) To make a subdirectory called unixstuff in your current working directory type% mkdir unixstuffTo see the directory you have just created, type% lsCompiled by S. Agarwal, SXCC, Kolkata.
29cd (change directory) % cd unixstuff The command cd directory means change the current working directory to 'directory'. The current working directory may be thought of as the directory you are in, i.e. your current position in the file-system tree.To change to the directory you have just made, type% cd unixstuffType ls to see the contents (which should be empty)Compiled by S. Agarwal, SXCC, Kolkata.
30In every directory there are two special directories called (. ) and ( In UNIX, (.) means the current directory, so typing% cd .means stay where you are (the unixstuff directory).This may not seem very useful at first, but using (.) as the name of the current directory will save a lot of typing, as we shall see later in the tutorial.(..) means the parent of the current directory, so typing% cd ..will take you one directory up the hierarchy (back to your home directory).Note: typing cd with no argument always returns you to your home directory. This is very useful if you are lost in the file system.Compiled by S. Agarwal, SXCC, Kolkata.
31pwd (print working directory) Pathnames enable you to work out where you are in relation to the whole file-system. For example, to find out the absolute pathname of your home-directory, type cd to get back to your home-directory and then type% pwd/user/eebeng99/ee91abCompiled by S. Agarwal, SXCC, Kolkata.
32~ (your home directory) Home directories can also be referred to by the tilde ~ character. It can be used to specify paths starting at your home directory.% ls ~/unixstuffwill list the contents of your unixstuff directory, no matter where you currently are in the file system.What do you thinkCompiled by S. Agarwal, SXCC, Kolkata.
33SUMMARY ls ls -a mkdir cd directory cd cd ~ cd .. pwd list files and directoriesls -alist all files and directoriesmkdirmake a directorycd directorychange to named directorycdchange to home-directorycd ~cd ..change to parent directorypwddisplay the path of the current directoryCompiled by S. Agarwal, SXCC, Kolkata.
34Copy files :cp (copy)cp file1 file2 is the command which makes a copy of file1 in the current working directory and calls it file2Compiled by S. Agarwal, SXCC, Kolkata.
35Move files :mv (move)mv file1 file2 moves (or renames) file1 to file2. To move a file from one place to another, use the mv command. This has the effect of moving rather than copying the file, so you end up with only one file rather than two.It can also be used to rename a file, by moving the file to the same directory, but giving it a different name.Compiled by S. Agarwal, SXCC, Kolkata.
36rm (remove), rmdir (remove directory) To delete (remove) a file, use the rm command.Inside unixstuff directory, type% cp science.txt tempfile.txt % ls (to check if it has created the file) % rm tempfile.txt % ls (to check if it has deleted the file)You can use the rmdir command to remove a directory (make sure it is empty first).Compiled by S. Agarwal, SXCC, Kolkata.
37clear (clear screen) % clear You may clear the terminal window of the previous commands so the output of the following commands can be clearly understood.At the prompt, type% clearThis will clear all text and leave you with the % prompt at the top of the window.Compiled by S. Agarwal, SXCC, Kolkata.
38Displaying the contents of a file on the screen cat (concatenate) The command cat can be used to display the contents of a file on the screen. Type:% cat science.txtIf the file is longer than than the size of the window, it scrolls past making it unreadable. lessThe command less writes the contents of a file onto the screen a page at a time. Type% less science.txtPress the [space-bar] to see another page, type [q] to quit reading. As you can see, less is used in preference to cat for long files.Compiled by S. Agarwal, SXCC, Kolkata.
39head tail % head science.txt % tail science.txt The head command writes the first ten lines of a file to the screen.First clear the screen then type% head science.txttailThe tail command writes the last ten lines of a file to the screen.Clear the screen and type% tail science.txt Compiled by S. Agarwal, SXCC, Kolkata.
40Simple searching using less Using less, you can search though a text file for a keyword (pattern). For example, to search through science.txt for the word 'science', type% less science.txtthen, still in less (i.e. don't press [q] to quit), type a forward slash [/] followed by the word to search/scienceless finds and highlights the keyword. Type [n] to search for the next occurrence of the word.Compiled by S. Agarwal, SXCC, Kolkata.
41grepgrep is one of many standard UNIX utilities. It searches files for specified words or patterns. First clear the screen, then type% grep science science.txtgrep prints out each line containg the word science.% grep Science science.txtThe grep command is case sensitive; it distinguishes between Science and science.To ignore upper/lower case distinctions, use the -i option% grep -i science science.txtCompiled by S. Agarwal, SXCC, Kolkata.
42% grep -i 'spinning top' science.txt To search for a phrase or pattern, you must enclose it in single quotes (the apostrophe symbol). For example to search for spinning top, type% grep -i 'spinning top' science.txtSome of the other options of grep are:-v display those lines that do NOT match -n precede each maching line with the line number -c print only the total count of matched linesTry some of them and see the different results. Don't forget, you can use more than one option at a time, for example, the number of lines without the words science or Science is% grep -ivc science science.txtCompiled by S. Agarwal, SXCC, Kolkata.
43wc (word count) % wc -w science.txt % wc -l science.txt A handy little utility is the wc command, short for word count. To do a word count on science.txt, type% wc -w science.txtTo find out how many lines the file has, type% wc -l science.txtCompiled by S. Agarwal, SXCC, Kolkata.
44SUMMARY cp file1 file2 mv file1 file2 rm file rmdir directory cat file copy file1 and call it file2mv file1 file2move or rename file1 to file2rm fileremove a filermdir directoryremove a directorycat filedisplay a filemore filedisplay a file a page at a timehead filedisplay the first few lines of a filetail filedisplay the last few lines of a filegrep 'keyword' filesearch a file for keywordswc filecount number of lines/words/characters in fileCompiled by S. Agarwal, SXCC, Kolkata.
45REDIRECTIONIf you run the cat command without specifing a file to read, it reads the standard input (the keyboard), and on receiving the'end of file' (^D), copies it to the standard output (the screen).In UNIX, we can redirect both the input and the output of commands.type cat without specifing a file to read% catThen type a few words on the keyboard and press the [Return] key.Finally hold the [Ctrl] key down and press [d] (written as ^D for short) to end the input.Compiled by S. Agarwal, SXCC, Kolkata.
46pear banana apple ^D (Control D to stop) Redirecting the output :We use the > symbol to redirect the output of a command. For example, to create a file called list1 containing a list of fruit, type % cat > list1Then type in the names of some fruit.Press [Return] after each one.pear banana apple ^D (Control D to stop)The cat command reads the standard input (the keyboard) and the > redirects the output, which normally goes to the screen, into a file called list1To read the contents of the file, type% cat list1Compiled by S. Agarwal, SXCC, Kolkata.
47peach grape orange ^D (Control D to stop) The form >> appends standard output to a file. So to add more items to the file list1, type% cat >> list1Then type in the names of more fruitpeach grape orange ^D (Control D to stop)To read the contents of the file, type% cat list1Compiled by S. Agarwal, SXCC, Kolkata.
48% cat list1 list2 > biglist To join (concatenate) list1 and list2 into a new file called biglist. Type% cat list1 list2 > biglistWhat this is doing is reading the contents of list1 and list2 in turn, then outputing the text to the file biglistTo read the contents of the new file, type% cat biglistCompiled by S. Agarwal, SXCC, Kolkata.
49carrot beetroot artichoke ^D (control d to stop) The command sort alphabetically or numerically sorts a list. Type% sortThen type in the names of some vegetables. Press [Return] after each one.carrot beetroot artichoke ^D (control d to stop)The output will beartichoke beetroot carrotCompiled by S. Agarwal, SXCC, Kolkata.
50Redirecting the input : Using < you can redirect the input to come from a file rather than the keyboard. For example, to sort the list of fruit, type% sort < biglistand the sorted list will be output to the screen.To output the sorted list to a file, type,% sort < biglist > slistUse cat to read the contents of the file slistCompiled by S. Agarwal, SXCC, Kolkata.
51Pipes : % who % who > names.txt % sort < names.txt % who | sort To see who is on the system with you, type% whoOne method to get a sorted list of names is to type,% who > names.txt % sort < names.txtThis is a bit slow and you have to remember to remove the temporary file called names when you have finished. What you really want to do is connect the output of the who command directly to the input of the sort command. This is exactly what pipes do. The symbol for a pipe is the vertical bar |For example, typing% who | sortwill give the same result as above, but quicker and cleaner.To find out how many users are logged on, type% who | wc -lCompiled by S. Agarwal, SXCC, Kolkata.
52SUMMARY command > file command >> file command < file redirect standard output to a filecommand >> fileappend standard output to a filecommand < fileredirect standard input from a filecommand1 | command2pipe the output of command1 to the input of command2cat file1 file2 > file0concatenate file1 and file2 to file0sortsort datawholist users currently logged ina2ps -Pprinter textfileprint text file to named printerlpr -Pprinter psfileprint postscript file to named printerCompiled by S. Agarwal, SXCC, Kolkata.
53On-line Manuals % man wc % whatis wc There are on-line manuals which gives information about most commands. The manual pages tell you which options a particular command can take, and how each option modifies the behaviour of the command. Type man command to read the manual page for a particular command.For example, to find out more about the wc (word count) command, type% man wcAlternatively% whatis wcgives a one-line description of the command, but omits any information about options etc.Compiled by S. Agarwal, SXCC, Kolkata.
54Apropos % apropos keyword % apropos copy When you are not sure of the exact name of a command,% apropos keywordwill give you the commands with keyword in their manual page header. For example, try typing% apropos copyCompiled by S. Agarwal, SXCC, Kolkata.
55File and directory permissions UNIX supports access control. Every file and directory has associated with it ownership, and access permissions. Furthermore, one is able to specify those to whom the permissions apply.Permissions are defined as read, write, and execute. The read, write, and execute permissions are referred to as r, w, and x, respectively.Those to whom the permissions apply are the user who owns the file, those who are in the same group as the owner, and all others. The user, group, and other permissions are referred to as u, g, and o, respectively.(groups: UNIX allows users to be placed in groups, so that the control of access is made simpler for administrators. )Compiled by S. Agarwal, SXCC, Kolkata.
56The meaning of file and directory permissions Read permissionFor a file, having read permission allows you to view the contents of the file. For a directory, having read permission allows you to list the directory's contents.Write permissionFor a file, write permission allows you to modify the contents of the file. For a directory, write permission allows you to alter the contents of the directory, i.e., to add or delete files.Execute permissionFor a file, execute permission allows you to run the file, if it is an executable program, or script. Note that file execute permission is irrelevant for nonexecutable files. For a directory, execute permission allows you to cd to the directory, and make it your current working directory.Compiled by S. Agarwal, SXCC, Kolkata.
57Viewing permissionsTo see the permissions on a file, use the ls command, with the -l option.Execute the commandls -l /etc/passwdto view the information on the system password database. The output should look similar to this:-rw-r--r-- 1 root sys Apr 17 12:05 /etc/passwdThe first 10 characters describe the access permissions. The first dash indicates the type of file (d for directory, s for special file, - for a regular file). The next three characters ("rw-") describe the permissions of the owner of the file: read and write, but no execute. The next three characters ("r--") describe the permissions for those in the same group as the owner: read, no write, no execute. The next three characters describe the permissions for all others: read, no write, no execute.Compiled by S. Agarwal, SXCC, Kolkata.
58Setting permissionsUNIX allows you to set the permissions on files that you own. The command to change the file permission mode is chmod. Chmod requires you to specify the new permissions you want, and specify the file or directory you want the changes applied to.To set file permissions, you may use to the "rwx" notation to specify the type of permissions, and the "ugo" notation to specify those the permissions apply to.To define the kind of change you want to make to the permissions, use the plus sign (+) to add a permission, the minus sign (-) to remove a permission, and the equal sign (=) to set a permission directly.Compiled by S. Agarwal, SXCC, Kolkata.
59The commandchmod g=rw- ~/.shrcchanges the file permissions on the file .shrc, in your home directory. Specifically, you are specifying group read access and write access, with no execute access.To change the permissions on the .shrc file in your home directory so that group and others have read permission only.type the commandchmod go=r-- ~/.shrcCompiled by S. Agarwal, SXCC, Kolkata.