tag:blogger.com,1999:blog-55642160455023640382024-03-05T01:37:20.374-08:00Learn Unix EasilySekharhttp://www.blogger.com/profile/03821544440692032816noreply@blogger.comBlogger48125tag:blogger.com,1999:blog-5564216045502364038.post-44340090238962701532008-04-22T20:48:00.001-07:002008-04-22T20:48:51.100-07:00What is UNIX?<p>UNIX is an operating system which was first developed in the 1960s, and has been under constant development ever since. By operating system, we mean the suite of programs which make the computer work. It is a stable, multi-user, multi-tasking system for servers, desktops and laptops. </p> <p>UNIX systems also have a graphical user interface (GUI) similar to Microsoft Windows which provides an easy to use environment. However, knowledge of UNIX is required for operations which aren't covered by a graphical program, or for when there is no windows interface available, for example, in a telnet session.</p>Sekharhttp://www.blogger.com/profile/03821544440692032816noreply@blogger.com0tag:blogger.com,1999:blog-5564216045502364038.post-39525859742394340302008-04-22T20:47:00.002-07:002008-04-22T20:48:15.882-07:00Types of UNIX<p>There are many different versions of UNIX, although they share common similarities. The most popular varieties of UNIX are Sun Solaris, GNU/Linux, and MacOS X. </p> <p>Here in the School, we use Solaris on our servers and workstations, and Fedora Linux on the servers and desktop PCs.</p>Sekharhttp://www.blogger.com/profile/03821544440692032816noreply@blogger.com0tag:blogger.com,1999:blog-5564216045502364038.post-29990380875728154002008-04-22T20:45:00.000-07:002008-04-22T20:47:20.513-07:00The UNIX operating systemThe UNIX operating system is made up of three parts; the kernel, the shell and the programs.<br /><br /><span style="font-size:130%;"><span style="font-weight: bold;">The kernel<br /><br /></span></span>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.<br /><br />As an illustration of the way that the shell and the kernel work together, suppose a user types<span style="font-style: italic;"> rm myfile </span>(which has the effect of removing the file <span style="font-weight: bold;">myfile</span>). The shell searches the filestore for the file containing the program rm, and then requests the kernel, through system calls, to execute the program <span style="font-style: italic;">rm </span>on <span style="font-weight: bold;">myfile</span>. When the process <span style="font-style: italic;">rm myfile</span> has finished running, the shell then returns the UNIX prompt % to the user, indicating that it is waiting for further commands.<br /><span style="font-size:130%;"><br /><br /><span style="font-weight: bold;">The shell<br /><br /></span></span>The 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).<br /><br />The adept user can customise his/her own shell, and users can use different shells on the same machine. Staff and students in the school have the <span style="font-weight: bold;">tcsh shell</span> by default.<br /><br />The tcsh shell has certain features to help the user inputting commands.<br /><br />Filename Completion - By typing part of the name of a command, filename or directory and pressing the [<span style="font-weight: bold;">Tab</span>] key, the tcsh shell will complete the rest of the name automatically. If the shell finds more than one name beginning with those letters you have typed, it will beep, prompting you to type a few more letters before pressing the tab key again.<br /><br />History - The shell keeps a list of the commands you have typed in. If you need to repeat a command, use the cursor keys to scroll up and down the list or type history for a list of previous commands.Sekharhttp://www.blogger.com/profile/03821544440692032816noreply@blogger.com0tag:blogger.com,1999:blog-5564216045502364038.post-58885142414094117182008-04-22T20:43:00.000-07:002008-04-22T20:45:02.362-07:00Files and processesEverything in UNIX is either a file or a process. <br /><br />A process is an executing program identified by a unique PID (process identifier). <br /><br />A file is a collection of data. They are created by users using text editors, running compilers etc. <br /><br />Examples of files: <br /><br /><ul><li>a document (report, essay etc.) </li><br /><li>the text of a program written in some high-level programming language </li><br /><li>instructions comprehensible directly to the machine and incomprehensible to a casual user, for example, a collection of binary digits (an executable or binary file); </li><br /><li>a directory, containing information about its contents, which may be a mixture of other directories (subdirectories) and ordinary files. </li></ul>Sekharhttp://www.blogger.com/profile/03821544440692032816noreply@blogger.com0tag:blogger.com,1999:blog-5564216045502364038.post-7605432429843921772008-04-22T04:00:00.000-07:002008-12-10T03:19:56.257-08:00The Directory StructureAll the files are grouped together in the directory structure. The file-system is arranged in a hierarchical structure, like an inverted tree. The top of the hierarchy is traditionally called root (written as a slash / )<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2QshBLWyZxbIhLnroLy0pg9bGoHtuyXt_cBV7y9uaAbEA-48wkAuBlxDuCFW4PHM4k0lABcATCkfcvMiGYVt9oSL-6S8PME_6digYTDXdBm5wjl2IMTj6sns5vxagCkgGfnU2DimOywk/s1600-h/unix-tree.png"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2QshBLWyZxbIhLnroLy0pg9bGoHtuyXt_cBV7y9uaAbEA-48wkAuBlxDuCFW4PHM4k0lABcATCkfcvMiGYVt9oSL-6S8PME_6digYTDXdBm5wjl2IMTj6sns5vxagCkgGfnU2DimOywk/s320/unix-tree.png" alt="" id="BLOGGER_PHOTO_ID_5192009699602062610" border="0" /></a><br />In the diagram above, we see that the home directory of the undergraduate student "<span style="font-weight: bold;">ee51vn</span>" contains two sub-directories (<span style="font-weight: bold;">docs </span>and <span style="font-weight: bold;">pics</span>) and a file called <span style="font-weight: bold;">report.doc</span>.<br /><br />The full path to the file <span style="font-weight: bold;">report.doc</span> is "<span style="font-weight: bold;">/home/its/ug1/ee51vn/report.doc</span>"Sekharhttp://www.blogger.com/profile/03821544440692032816noreply@blogger.com0tag:blogger.com,1999:blog-5564216045502364038.post-63490332708346042162008-04-22T03:39:00.000-07:002008-12-10T03:19:56.560-08:00Starting an UNIX terminalTo open an UNIX terminal window, click on the "Terminal" icon from the drop-down menus.<br /><br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjg_IUKPwdSkwOFPxLHiP5SrpWQG7NBYIoDL_ekd6raRjb7uYGHwhO2KXr_ou9j4FJ9a1mVPu2NJKwdAkXVZ0U_uXUgZ52mY1CVAC79xNw32mUeQZcyLw-N930QA6cAecJuBXxtpNqdIJ4/s1600-h/1.gif"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjg_IUKPwdSkwOFPxLHiP5SrpWQG7NBYIoDL_ekd6raRjb7uYGHwhO2KXr_ou9j4FJ9a1mVPu2NJKwdAkXVZ0U_uXUgZ52mY1CVAC79xNw32mUeQZcyLw-N930QA6cAecJuBXxtpNqdIJ4/s320/1.gif" alt="" id="BLOGGER_PHOTO_ID_5192017658176462146" border="0" /></a><br /><br />An UNIX Terminal window will then appear with a % prompt, waiting for you to start entering commands.<br /><br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhklQsiuDU08FLj-IfpndKYi-QUo4ukEoPjx-R6_fyO1aRmMOrvjf8HBFi3fTHwoS_BPSqptjR4Dml1K0r950Sw2fdx03W4GThatTylbbxBaeTpeKDgAq9-I39ooym-ReEdKnxFlhrAQg4/s1600-h/2.gif"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhklQsiuDU08FLj-IfpndKYi-QUo4ukEoPjx-R6_fyO1aRmMOrvjf8HBFi3fTHwoS_BPSqptjR4Dml1K0r950Sw2fdx03W4GThatTylbbxBaeTpeKDgAq9-I39ooym-ReEdKnxFlhrAQg4/s320/2.gif" alt="" id="BLOGGER_PHOTO_ID_5192018096263126354" border="0" /></a>Sekharhttp://www.blogger.com/profile/03821544440692032816noreply@blogger.com0tag:blogger.com,1999:blog-5564216045502364038.post-22592532919660724762008-04-22T03:14:00.000-07:002008-12-10T03:19:56.872-08:001.1 Listing files and directories<span style="font-size:130%;"><span style="font-weight: bold;">ls (list)<br /><br /></span></span>When you first login, your current working directory is your home directory. Your home directory has the same name as your user-name, for example, <span style="font-weight: bold;">ee91ab</span>, and it is where your personal files and subdirectories are saved.<br /><br />To find out what is in your home directory, type<br /><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">% ls </span><br /><br />The ls command lists the contents of your current working directory.<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEir2pEq432IjjXdXPfwg7V3TH-f6JDOsOPLeYoVp_WuyBhvDqOCrwnMoluETu-GezwNGguntTK75Jo333byfm9YFSl5Xv9LwIs8DSxn14ll6uQPm0jlaZH7D84pnP45W-2DfUA13IGXgkw/s1600-h/unix-xterm11.gif"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEir2pEq432IjjXdXPfwg7V3TH-f6JDOsOPLeYoVp_WuyBhvDqOCrwnMoluETu-GezwNGguntTK75Jo333byfm9YFSl5Xv9LwIs8DSxn14ll6uQPm0jlaZH7D84pnP45W-2DfUA13IGXgkw/s320/unix-xterm11.gif" alt="" id="BLOGGER_PHOTO_ID_5192011129826172194" border="0" /></a><br />There 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.<br /><br />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!!!<br /><br />To list all files in your home directory including those whose names begin with a dot, type<br /><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">% ls -a</span><br /><br />As you can see, ls -a lists files that are normally hidden.<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi7ovbkI_7w9rIAmABtFJYnZ3uE1sexL3COi7uQ9UG02tLJxIuio-rRTPCK5eG_FowpiJcR5gBYDpAymHF9D1hB-8hoHLzIINm9_KA3Cu6w3d4oXDH8Hf0UG_bn_Y1nryhshipprtG-pWY/s1600-h/unix-xterm12.gif"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi7ovbkI_7w9rIAmABtFJYnZ3uE1sexL3COi7uQ9UG02tLJxIuio-rRTPCK5eG_FowpiJcR5gBYDpAymHF9D1hB-8hoHLzIINm9_KA3Cu6w3d4oXDH8Hf0UG_bn_Y1nryhshipprtG-pWY/s320/unix-xterm12.gif" alt="" id="BLOGGER_PHOTO_ID_5192011366049373490" border="0" /></a><br /><br />ls is an example of a command which can take options: <span style="font-weight:bold;">-a</span> 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. (See later in this tutorial)Sekharhttp://www.blogger.com/profile/03821544440692032816noreply@blogger.com0tag:blogger.com,1999:blog-5564216045502364038.post-59829889697261572772008-04-22T03:13:00.001-07:002008-04-22T03:13:47.682-07:001.2 Making Directories<span style="font-size:130%;"><span style="font-weight: bold;">mkdir (make directory)<br /><br /></span></span>We will now make a subdirectory in your home directory to hold the files you will be creating and using in the course of this tutorial. To make a subdirectory called unixstuff in your current working directory type<br /><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">% mkdir unixstuff </span><br /><br />To see the directory you have just created, type<br /><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">% ls </span>Sekharhttp://www.blogger.com/profile/03821544440692032816noreply@blogger.com0tag:blogger.com,1999:blog-5564216045502364038.post-6634050396502302632008-04-22T03:12:00.000-07:002008-04-22T03:13:18.656-07:001.3 Changing to a different directory<span style="font-size:130%;"><span style="font-weight: bold;">cd (change directory)<br /><br /></span></span>The command cd directory means change the current working directory to '<span style="font-style: italic;">directory</span>'. The current working directory may be thought of as the directory you are in, i.e. your current position in the file-system tree.<br /><br />To change to the directory you have just made, type<br /><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">% cd unixstuff </span><br /><br />Type ls to see the contents (which should be empty)Sekharhttp://www.blogger.com/profile/03821544440692032816noreply@blogger.com0tag:blogger.com,1999:blog-5564216045502364038.post-27669859552044525062008-04-22T03:10:00.000-07:002008-04-22T03:11:55.926-07:001.4 The directories . and ..Still in the <span style="font-weight: bold;">unixstuff </span>directory, type<br /><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">% ls -a </span><br /><br />As you can see, in the <span style="font-weight: bold;">unixstuff </span>directory (and in all other directories), there are two special directories called (.) and (..)<br /><span style="font-size:130%;"><br /><span style="font-weight: bold;">The current directory (.)<br /><br /></span></span>In UNIX, (.) means the current directory, so typing<br /><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">% cd . </span><br /><br /><span style="font-style: italic; color: rgb(255, 0, 0);"><span style="font-weight: bold;">NOTE:</span> there is a space between cd and the dot</span><br /><br />means stay where you are (the <span style="font-weight: bold;">unixstuff </span>directory).<br /><br />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.<hr /><br /><span style="font-size:130%;"><span style="font-weight: bold;">The parent directory (..)<br /><br /></span></span>(..) means the parent of the current directory, so typing<br /><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">% cd .. </span><br /><br />will take you one directory up the hierarchy (back to your home directory). Try it now.<br /><br />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.Sekharhttp://www.blogger.com/profile/03821544440692032816noreply@blogger.com0tag:blogger.com,1999:blog-5564216045502364038.post-89227128331940709652008-04-22T03:07:00.000-07:002008-12-10T03:19:56.912-08:001.5 Pathnames<span style="font-size:130%;"><span style="font-weight: bold;">pwd (print working directory)<br /><br /></span></span>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<br /><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">% pwd </span><br /><br />The full pathname will look something like this -<br /><br /><span style="font-style: italic;">/home/its/ug1/ee51vn</span><br /><br />which means that <span style="font-weight: bold;">ee51vn </span>(your home directory) is in the sub-directory <span style="font-weight: bold;">ug1 </span>(the group directory),which in turn is located in the <span style="font-weight: bold;">its </span>sub-directory, which is in the <span style="font-weight: bold;">home </span>sub-directory, which is in the top-level root directory called " / " .<br /><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2QshBLWyZxbIhLnroLy0pg9bGoHtuyXt_cBV7y9uaAbEA-48wkAuBlxDuCFW4PHM4k0lABcATCkfcvMiGYVt9oSL-6S8PME_6digYTDXdBm5wjl2IMTj6sns5vxagCkgGfnU2DimOywk/s1600-h/unix-tree.png"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2QshBLWyZxbIhLnroLy0pg9bGoHtuyXt_cBV7y9uaAbEA-48wkAuBlxDuCFW4PHM4k0lABcATCkfcvMiGYVt9oSL-6S8PME_6digYTDXdBm5wjl2IMTj6sns5vxagCkgGfnU2DimOywk/s320/unix-tree.png" alt="" id="BLOGGER_PHOTO_ID_5192009699602062610" border="0" /></a>Sekharhttp://www.blogger.com/profile/03821544440692032816noreply@blogger.com0tag:blogger.com,1999:blog-5564216045502364038.post-91712244352355669872008-04-22T03:05:00.000-07:002008-04-22T03:07:05.905-07:001.6 More about home directories and pathnames<span style="font-size:130%;"><span style="font-weight: bold;">Understanding pathnames<br /><br /></span></span>First type cd to get back to your home-directory, then type<br /><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">% ls unixstuff</span><br /><br />to list the conents of your unixstuff directory.<br /><br />Now type<br /><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">% ls backups</span><br /><br />You will get a message like this -<br /><br /><span style="font-style: italic;">backups: No such file or directory </span><br /><br />The reason is, <span style="font-weight: bold;">backups </span>is not in your current working directory. To use a command on a file (or directory) not in the current working directory (the directory you are currently in), you must either cd to the correct directory, or specify its full pathname. To list the contents of your backups directory, you must type<br /><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">% ls unixstuff/backups </span><hr /><br /><span style="font-size:130%;"><span style="font-weight: bold;">~ (your home directory)<br /><br /></span></span>Home directories can also be referred to by the tilde ~ character. It can be used to specify paths starting at your home directory. So typing<br /><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">% ls ~/unixstuff</span><br /><br />will list the contents of your unixstuff directory, no matter where you currently are in the file system.<br /><br />What do you think<br /><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">% ls ~</span><br /><br />would list?<br /><br />What do you think<br /><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">% ls ~/.. </span><br /><br />would list?Sekharhttp://www.blogger.com/profile/03821544440692032816noreply@blogger.com0tag:blogger.com,1999:blog-5564216045502364038.post-85418246759486549152008-04-22T00:05:00.000-07:002008-04-22T00:06:04.301-07:001.7 Summary<table border="2" bordercolor="#cccccc" cellpadding="2" cellspacing="0" width="100%"> <tbody><tr> <th>Command</th> <th>Meaning</th> </tr> <tr> <td>ls</td> <td>list files and directories</td> </tr> <tr> <td>ls -a</td> <td>list all files and directories</td> </tr> <tr> <td>mkdir</td> <td>make a directory</td> </tr> <tr> <td>cd directory</td> <td>change to named directory</td> </tr> <tr> <td>cd</td> <td>change to home-directory</td> </tr> <tr> <td>cd ~</td> <td>change to home-directory</td> </tr> <tr> <td>cd ..</td> <td>change to parent directory</td> </tr> <tr> <td>pwd</td> <td>display the path of the current directory</td> </tr></tbody></table>Sekharhttp://www.blogger.com/profile/03821544440692032816noreply@blogger.com0tag:blogger.com,1999:blog-5564216045502364038.post-24053448716496939282008-04-22T00:00:00.000-07:002008-04-22T00:02:53.614-07:002.1 Copying Files<span style="font-size:130%;"><span style="font-weight: bold;">cp (copy)<br /><br /></span></span><span style="font-style: italic;">cp file1 file2</span> is the command which makes a copy of <span style="font-weight: bold;">file1 </span>in the current working directory and calls it <span style="font-weight: bold;">file2 </span><br /><br />What we are going to do now, is to take a file stored in an open access area of the file system, and use the cp command to copy it to your unixstuff directory.<br /><br />First, cd to your <span style="font-weight: bold;">unixstuff </span>directory.<br /><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">% cd ~/unixstuff</span><br /><br />Then at the UNIX prompt, type,<br /><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">% cp /vol/examples/tutorial/science.txt . </span><br /><br /><span style="font-style: italic; color: rgb(255, 0, 0);"><span style="font-weight: bold;">Note:</span> Don't forget the dot . at the end. Remember, in UNIX, the dot means the current directory.</span><br /><br />The above command means copy the file <span style="font-weight: bold;">science.txt</span> to the current directory, keeping the name the same.<br /><br /><span style="color: rgb(255, 0, 0); font-style: italic;">(<span style="font-weight: bold;">Note:</span> The directory</span><span style="font-weight: bold; color: rgb(255, 0, 0); font-style: italic;"> /vol/examples/tutorial/</span><span style="color: rgb(255, 0, 0); font-style: italic;"> is an area to which everyone in the school has read and copy access. If you are from outside the University, you can grab a copy of the file here. Use 'File/Save As..' from the menu bar to save it into your </span><span style="font-weight: bold; color: rgb(255, 0, 0); font-style: italic;">unixstuff </span><span style="color: rgb(255, 0, 0); font-style: italic;">directory.)</span>Sekharhttp://www.blogger.com/profile/03821544440692032816noreply@blogger.com0tag:blogger.com,1999:blog-5564216045502364038.post-44119457623664050322008-04-21T23:59:00.000-07:002008-04-22T00:00:37.498-07:002.2 Moving files<span style="font-size:130%;"><span style="font-weight: bold;">mv (move)<br /><br /></span></span><span style="font-style: italic;">mv file1 file2</span> moves (or renames) <span style="font-weight: bold;">file1 </span>to <span style="font-weight: bold;">file2 </span><br /><br />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.<br /><br />It can also be used to rename a file, by moving the file to the same directory, but giving it a different name.<br /><br />We are now going to move the file science.bak to your backup directory.<br /><br />First, change directories to your unixstuff directory (can you remember how?). Then, inside the <span style="font-weight: bold;">unixstuff </span>directory, type<br /><br /><span style="color: rgb(51, 51, 255); font-weight: bold;">% mv science.bak backups/.</span><br /><br />Type ls and ls backups to see if it has worked.Sekharhttp://www.blogger.com/profile/03821544440692032816noreply@blogger.com0tag:blogger.com,1999:blog-5564216045502364038.post-89563745971907456832008-04-21T23:55:00.000-07:002008-04-21T23:57:30.245-07:002.3 Removing files and directories<span style="font-weight: bold;font-size:130%;" >rm (remove), rmdir (remove directory)<br /><br /></span>To delete (remove) a file, use the rm command. As an example, we are going to create a copy of the <span style="font-weight: bold;">science.txt</span> file then delete it.<br /><br />Inside your <span style="font-weight: bold;">unixstuff </span>directory, type<br /><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">% cp science.txt tempfile.txt<br /></span><span style="font-weight: bold; color: rgb(51, 51, 255);">% ls<br /></span><span style="font-weight: bold; color: rgb(51, 51, 255);">% rm tempfile.txt<br /></span><span style="font-weight: bold; color: rgb(51, 51, 255);">% ls</span><br /><br />You can use the <span style="font-style: italic;">rmdir </span>command to remove a directory (make sure it is empty first). Try to remove the <span style="font-weight: bold;">backups </span>directory. You will not be able to since UNIX will not let you remove a non-empty directorySekharhttp://www.blogger.com/profile/03821544440692032816noreply@blogger.com0tag:blogger.com,1999:blog-5564216045502364038.post-87064481292959845162008-04-21T23:52:00.000-07:002008-04-21T23:55:18.763-07:002.4 Displaying the contents of a file on the screen<span style="font-size:130%;"><span style="font-weight: bold;">clear (clear screen)<br /><br /></span></span>Before you start the next section, you may like to clear the terminal window of the previous commands so the output of the following commands can be clearly understood.<br /><br />At the prompt, type<br /><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">% clear </span><br /><br />This will clear all text and leave you with the % prompt at the top of the window.<hr /><br /><span style="font-size:130%;"><span style="font-weight: bold;">cat (concatenate)<br /><br /></span></span>The command cat can be used to display the contents of a file on the screen. Type:<br /><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">% cat science.txt </span><br /><br />As you can see, the file is longer than than the size of the window, so it scrolls past making it unreadable<hr /><br /><span style="font-size:130%;"><span style="font-weight: bold;">less</span></span><br />The command less writes the contents of a file onto the screen a page at a time. Type<br /><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">% less science.txt </span><br /><br />Press the [space-bar] if you want to see another page, and type <span style="font-weight: bold;">[q]</span> if you want to quit reading. As you can see, less is used in preference to cat for long files.<hr /><br /><span style="font-size:130%;"><span style="font-weight: bold;">head<br /><br /></span></span>The head command writes the first ten lines of a file to the screen.<br /><br />First clear the screen then type<br /><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">% head science.txt </span><br /><br />Then type<br /><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">% head -5 science.txt</span><br /><br />What difference did the -5 do to the head command?<hr /><br /><span style="font-size:130%;"><span style="font-weight: bold;">tail<br /><br /></span></span>The tail command writes the last ten lines of a file to the screen.<br /><br />Clear the screen and type<br /><br /><span style="color: rgb(51, 51, 255); font-weight: bold;">% tail science.txt </span><br /><br /><span style="font-weight: bold;">Q.</span> How can you view the last 15 lines of the file?Sekharhttp://www.blogger.com/profile/03821544440692032816noreply@blogger.com0tag:blogger.com,1999:blog-5564216045502364038.post-50761830966180371102008-04-21T23:44:00.000-07:002008-04-21T23:51:35.160-07:002.5 Searching the contents of a file<span style="font-size:130%;"><span style="font-weight: bold;">Simple searching using less<br /><br /></span></span>Using less, you can search though a text file for a keyword (pattern). For example, to search through <span style="font-weight: bold;">science.txt</span> for the word '<span style="font-weight: bold;">science</span>', type<br /><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">% less science.txt </span><br /><br />then, still in less, type a forward slash [/] followed by the word to search<br /><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">/science</span><br /><br />As you can see, less finds and highlights the keyword. Type [n] to search for the next occurrence of the word.<hr /><br /><span style="font-size:130%;"><span style="font-weight: bold;">grep (don't ask why it is called grep)<br /><br /></span></span>grep is one of many standard UNIX utilities. It searches files for specified words or patterns. First clear the screen, then type<br /><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">% grep science science.txt </span><br /><br />As you can see, grep has printed out each line containg the word science.<br /><br />Or has it ????<br /><br />Try typing<br /><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">% grep Science science.txt </span><br /><br />The grep command is case sensitive; it distinguishes between Science and science.<br /><br />To ignore upper/lower case distinctions, use the -i option, i.e. type<br /><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">% grep -i science science.txt </span><br /><br />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<br /><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">% grep -i 'spinning top' science.txt </span><br /><br />Some of the other options of grep are:<br /><br /><span style="font-weight: bold;">-v</span> display those lines that do NOT match<br /><span style="font-weight: bold;">-n</span> precede each matching line with the line number<br /><span style="font-weight: bold;">-c</span> print only the total count of matched lines<br /><br /><br />Try 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<br /><br /><span style="font-weight: bold;"><span style="color: rgb(51, 51, 255);">% grep -ivc science science.txt</span> </span><hr /><br /><span style="font-size:130%;"><span style="font-weight: bold;">wc (word count)<br /><br /></span></span>A handy little utility is the wc command, short for word count. To do a word count on science.txt, type<br /><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">% wc -w science.txt </span><br /><br />To find out how many lines the file has, type<br /><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">% wc -l science.txt </span>Sekharhttp://www.blogger.com/profile/03821544440692032816noreply@blogger.com0tag:blogger.com,1999:blog-5564216045502364038.post-24669585138659859352008-04-21T23:39:00.000-07:002008-04-21T23:43:57.543-07:002.6 Summary<table border="2" bordercolor="#999999" cellpadding="2" cellspacing="0" width="100%"> <tbody><tr> <th>Command</th> <th>Meaning</th> </tr> <tr> <td>cp file1 file2</td> <td>copy file1 and call it file2</td> </tr> <tr> <td>mv file1 file2</td> <td>move or rename file1 to file2</td> </tr> <tr> <td>rm file</td> <td>remove a file</td> </tr> <tr> <td>rmdir directory</td> <td>remove a directory</td> </tr> <tr> <td>cat file</td> <td>display a file</td> </tr> <tr> <td>less file</td> <td>display a file a page at a time</td> </tr> <tr> <td>head file</td> <td>display the first few lines of a file</td> </tr> <tr> <td>tail file</td> <td>display the last few lines of a file</td> </tr> <tr> <td>grep 'keyword' file</td> <td>search a file for keywords</td> </tr> <tr> <td>wc file</td> <td>count number of lines/words/characters in file</td> </tr></tbody></table>Sekharhttp://www.blogger.com/profile/03821544440692032816noreply@blogger.com0tag:blogger.com,1999:blog-5564216045502364038.post-55755477365053242582008-04-18T04:37:00.000-07:002008-04-18T04:39:32.416-07:003.1 RedirectionMost processes initiated by UNIX commands write to the standard output (that is, they write to the terminal screen), and many take their input from the standard input (that is, they read it from the keyboard). There is also the standard error, where processes write their error messages, by default, to the terminal screen.<br /><br />We have already seen one use of the cat command to write the contents of a file to the screen.<br /><br />Now type cat without specifing a file to read<br /><br /><strong><span style="color:#3333ff;">% cat<br /></span></strong><br />Then type a few words on the keyboard and press the [<strong>Return</strong>] key.<br /><br />Finally hold the [<strong>Ctrl</strong>] key down and press [<strong>d</strong>] (written as <strong>^D</strong> for short) to end the input.<br /><br />What has happened?<br /><br />If 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' <strong>(^D</strong>), copies it to the standard output (the screen).<br /><br />In UNIX, we can redirect both the input and the output of commands.Sekharhttp://www.blogger.com/profile/03821544440692032816noreply@blogger.com0tag:blogger.com,1999:blog-5564216045502364038.post-33862798397049906172008-04-18T04:25:00.000-07:002008-04-18T04:37:26.776-07:003.2 Redirecting the OutputWe use the > symbol to redirect the output of a command. For example, to create a file called list1 containing a list of fruit, type<br /><br /><strong><span style="color:#3333ff;">% cat > list1<br /></span></strong><br />Then type in the names of some fruit. Press [<strong>Return</strong>] after each one.<br /><br /><em>pear<br />banana<br />apple<br />^D {this means press [Ctrl] and [d] to stop}<br /></em><br />What happens is the cat command reads the standard input (the keyboard) and the > redirects the output, which normally goes to the screen, into a file called <strong>list1</strong><br /><br />To read the contents of the file, type<br /><br /><strong><span style="color:#3333ff;">% cat list1<br /></span></strong><hr><br /><br /><strong><span style="font-size:130%;">Appending to a file</span></strong><br /><strong><span style="font-size:130%;"></span></strong><br />The form >> appends standard output to a file. So to add more items to the file list1, type<br /><br /><strong><span style="color:#3333ff;">% cat >> list1<br /></span></strong><br />Then type in the names of more fruit<br /><br /><em>peach<br />grape<br />orange<br />^D (Control D to stop)<br /></em><br />To read the contents of the file, type<br /><br /><strong><span style="color:#3333ff;">% cat list1</span></strong><br /><br />You should now have two files. One contains six fruit, the other contains four fruit.<br /><br />We will now use the cat command to join (concatenate) <strong>list1</strong> and <strong>list2</strong> into a new file called <strong>biglist</strong>. Type<br /><br /><span style="color:#3333ff;"><strong>% cat list1 list2 > biglist</strong><br /></span><br />What this is doing is reading the contents of <strong>list1</strong> and <strong>list2</strong> in turn, then outputing the text to the file <strong>biglist</strong><br /><br />To read the contents of the new file, type<br /><br /><strong><span style="color:#3333ff;">% cat biglist</span></strong>Sekharhttp://www.blogger.com/profile/03821544440692032816noreply@blogger.com0tag:blogger.com,1999:blog-5564216045502364038.post-35913593309757831772008-04-18T04:08:00.000-07:002008-04-18T04:24:22.292-07:003.3 Redirecting the InputWe use the < symbol to redirect the input of a command.<br /><p>The command sort alphabetically or numerically sorts a list. Type </p><p><strong><span style="color:#3333ff;">% sort</span> </strong></p><p>Then type in the names of some animals. Press [Return] after each one. </p><p><em>dog<br />cat<br />bird<br />ape<br />^D (control d to stop)</em></p><p>The output will be </p><p><em>ape<br />bird<br />cat<br />dog </em></p><p>Using < you can redirect the input to come from a file rather than the keyboard. For example, to sort the list of fruit, type </p><p><strong><span style="color:#3333ff;">% sort < biglist</span> </strong></p><p>and the sorted list will be output to the screen. </p><p>To output the sorted list to a file, type, </p><p><strong><span style="color:#3333ff;">% sort < biglist > slist</span> </strong></p><p>Use cat to read the contents of the file slist</p><br /><p></p>Sekharhttp://www.blogger.com/profile/03821544440692032816noreply@blogger.com0tag:blogger.com,1999:blog-5564216045502364038.post-9851332251449922742008-04-18T04:06:00.000-07:002008-04-18T04:42:44.793-07:003.4 PipesTo see who is on the system with you, type<br /><br /><strong><span style="color:#3333ff;">% who</span></strong><br /><br />One method to get a sorted list of names is to type,<br /><br /><strong><span style="color:#3333ff;">% who > names.txt<br />% sort <></strong></span><br /><br />This 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<br /><br />For example, typing<br /><br /><strong><span style="color:#3333ff;">% who sort</span><br /></strong><br />will give the same result as above, but quicker and cleaner.<br /><br />To find out how many users are logged on, type<br /><br /><strong><span style="color:#3333ff;">% who wc -l</span></strong><br /><span style="color:#3333ff;"></span>Sekharhttp://www.blogger.com/profile/03821544440692032816noreply@blogger.com0tag:blogger.com,1999:blog-5564216045502364038.post-42774713536412746802008-04-18T04:00:00.000-07:002008-04-18T04:02:16.972-07:003.5 Summary<table bordercolor="#999999" cellspacing="0" cellpadding="2" width="100%" border="2"><tbody><tr><th>Command</th><th>Meaning</th></tr><tr><td>command > file</td><td>redirect standard output to a file</td></tr><tr><td>command >> file</td><td>append standard output to a file </td></tr><tr><td>command <><td>redirect standard input from a file</td></tr><tr><td>command1 command2</td><td>pipe the output of command1 to the input of command2</td></tr><tr><td>cat file1 file2 > file0</td><td>concatenate file1 and file2 to file0</td></tr><tr><td>sort</td><td>sort data</td></tr><tr><td>who</td><td>list users currently logged in</td></tr></tbody></table>Sekharhttp://www.blogger.com/profile/03821544440692032816noreply@blogger.com0tag:blogger.com,1999:blog-5564216045502364038.post-68386770178882777722008-04-18T03:48:00.000-07:002008-04-18T03:50:30.776-07:004.1 Wildcards<p><strong><span style="font-size:130%;">The * wildcard</span></strong> </p><p>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 </p><p><strong><span style="color:#3333ff;">% ls list*</span></strong></p><p>This will list all files in the current directory starting with list.... </p><p>Try typing </p><p><strong><span style="color:#3333ff;">% ls *list</span></strong></p><p>This will list all files in the current directory ending with ....list</p><strong><span style="font-size:130%;">The ? wildcard</span></strong><br /><br />The character ? will match exactly one character.<br /><br />So <strong>?ouse</strong> will match files like <strong>house</strong> and <strong>mouse</strong>, but not <strong>grouse</strong>.<br /><br />Try typing<br /><br /><strong><span style="color:#3333ff;">% ls ?list</span></strong>Sekharhttp://www.blogger.com/profile/03821544440692032816noreply@blogger.com0