6862c8d7bfebbc733b92bd287b0b9e8d.ppt
- Количество слайдов: 20
Week 2 Introduction to UNIX OS Department of Computer Engineering 2001 -2002 Fall Prepared by Aykut Guven
Beginning. . . • You have been learning basic concepts of Unix since 3 weeks. This week we are going on to learn Unix commands. • This week , we will complete of learning basic and common used commands of Unix. • Please do these steps which are given below. .
Steps • • • Login to the system. Make sure you are in your HOME directory. (typing "cd" by itself and pressing return will put you in your home directory) Type the following command: "cp -f /home/files/myfirstscript. " Now type "cp -f. R /home/public_html. " Type in "cat myfirstscript". What happens? Now, type in "more myfirstcript" What do you see this time? Now, type in "less myfirstcript" What do you see this time? Try this command "ls -al /etc/" Did you get to read everything? Now, try this command: " ls -al /etc/ | more" The " | " is above the enter key on your keyboard. It is called a "pipe". Can you figure out what it does? Try the previous command using the command "less" instead of the "more". Does it work?
Steps • • • • Try the previous command without using the pipe character. What happens now? Type "cd" to make sure you are in your home directory. Do an "ls al" What is the information you see on the new file? Now, let's try to execute the script we copied over. Type ". /myfirstscript" Take note: that is a period followed by the forward slash, followed by the name of the program. What happens? Now, type in "chmod 755 myfirstscript" and try to execute the script again. What happens this time? Type in "mv myfirstscript mynewnamescript". Do an "ls -al" , what do you see? Let's rename the script back to it's original name. "mv mynewnamescript myfirstscript" Does it still run? (It should, but make sure. ) Type "cat lesson 1. html | grep UNIX". What do you see? Type "grep UNIX lesson 1. html". Now what do you see? Type in "man grep". Do you get an indication of what grep is used for? Finally, type "grep UNIX lesson 1. html > UNIXTest. File". Do an "ls" and look for a file called "UNIXTest. File". Type in "more UNIXTest. File". What do you see? Delete the UNIXTest. File. ("rm UNIXTest. File") Make sure you are in your home directory and type "cp lesson 1. html. /public_html".
UNIX Commands • • • Cat Concatenate files and print on standard output. Works like the DOS "TYPE" command. Simply put, "cat" can be use to read the contents of a file. Chmod Change the permissions on a file or directory Grep Find line matching a certain pattern. Very powerful search tool. Less Filter for viewing files and directories. Works the same as the "more" command, but lets you scroll backwards. It is a little bulkier than more. More Filter for viewing files and directories a screen at a time. Mv Move or rename file > A cool feature of Linux is that you can easily send the info that normally gets printed on the screen into a text file instead. This is called redirecting output. . For example, ls > new. File, will list all files in a directory and write them to a file called "new. File". If "new. File" already exists, the contents will be over-written, otherwise a new file will be created. >> Append the output of a program to a file. If the file does not already exist, it will be created. If it does already exist, the output will be written to the end of the file. | Called the "pipe". It is used to redirect the output of a command to another command.
• • • You will use the "cat" command quite often. You might be told to "cat the file", which means to read it using the cat command. For example, "cat lesson 1. html" will let you read the file, lesson 1. html. In a future lesson we will describe "chmod" and file permissions in greater detail. For now, just know that if you want to execute a file as a program, you should enter "chmod 755 filename". The "|" (pipe) character allows you to pipe data from one program to another program or command. For example, if you change directories to /dev and do an "ls", the listing goes by too quickly to see what is there. Try this yourself. Now, use the pipe function and pipe the results of ls to a paging program like more. Here's how: ls /dev/ | more Now you can scroll through the results of the "ls" command. "grep" performs searches in files and directories. It is often used with wild cards to find information. For example, cd into your public_html directory. If you type, "grep linux *", a search will be performed on ALL (*) files in the directory for the word "linux. " Then each line containing the word "linux" will be printed to the screen. If you want to save your results to a file, you can use the greater than character to redirect the output to a file. For example, "grep linux * > linux. Lines" will perform a search on all files in the directory for the word "linux" and save the output to a file called "linux. Lines". If the file already exists, it will be overwritten. To avoid this, you can use a 2 greater than signs to append the output to the end of the file.
The Lab Assignment • You have a file named "index. html" in your home directory. In a new file that you write with vi, please list 3 commands to quickly view the file. • Make a copy of the file and call it "my. Practice. html". • Search the file for all lines with the word "zip" in it and save them to a file call "mypracticeresults. txt". • Print the contents of mypracticeresults. txt to the screen. Attach a screen shot of this to the email you send me with the answers from question number 1.
File Permissions • Every file and directory in UNIX/Linux has read, write, and execute permissions. Read permission means that the file can be read but not modified or deleted. Write permission means that the file can be created, modified, or deleted. Execute permission means that the file can be executed. The execute permission is similar to a. exe file in Windows or DOS. For directories, execute permission means you can open the directory.
• Permissions There are 3 sets of permissions for every file or directory -- owner, group, and world. For each set, there are separate read, write, and execute permissions. The owner permissions are for the owner of the file or directory. The group permissions are for everyone in the group. The global permissions are for anyone. To see the current permissions, owner, and group for a file or directory, type the following commands: 1 -"cd" 2 -"ls -l" This will display the contents of your home directory in long format. The following is an example of what you might have seen with the above 2
commands drwxr--r-- 5 you public 4096 Feb 7 14: 33 Desktop drwxr--r-- 2 you public 4096 Apr 4 10: 55 linuxpractice -rwxr-xr-x 1 you public 223 Apr 6 11: 37 myfirstscript drwxr-xr-x 4 you public 4096 Apr 9 12: 03 public_html The third column (you) tells the owner of the file or directory, and the fourth column (public) is the name of the group for the file or directory. The permissions are listed in the first column. The first letter is whether the item is a directory or a file. If the first letter is a "d", then the item is a directory as in the first item listed above, Desktop. Notice, for the file myfirstscript, the first letter is "-". The next three letters are the permissions for the owner of the file, the next three letters apply to everyone in the group, and the last three letters are for everyone else. The read, write, and execute permissions are referred to as r, w, and x respectively. Thus, for the directory Desktop above, the owner you has read, write, and execute permissions to the directory Desktop, everyone in the group public has read permissions, and everyone else has read permissions. The only one who can modify or delete any file in this directory is the owner you (or the superuser- "root").
Changing Permissions "chmod" is a standard UNIX/Linux command that allows you to change the permissions of a file or directory. There are two arguments for chmod: the permissions and the file/directory name. The permission argument for chmod is based on numbers. 1 stands for execute. 2 stands for write. 4 stands for read. To set more than one permission on a file or directory, you just add up the permissions. For example, 7 means read, write, and execute permissions. chmod takes the permissions as the first argument in the order user, group, global. Thus, the command chmod 777 hello will change the permissions of the file hello to read, write and execute by user, group, and everyone else. Note: To change the permissions of a file/directory, you must be the owner of that file/directory. However, root can change permissions on any file or directory.
Changing Permissions One last topic that we should touch on here is the ownership commands. If you are ROOT, you can change the ownership of files using the "chown username filename" command. As with most commands, there are many options. You can learn about those options by typing "man chown" Example: • "chmod 777 Desktop" will allow EVERYBODY full access to read, write and execute. • "chmod 644 Desktop" will all the owner (you) read and write access, the group read access, and everybody else (world) read access too. • "chmod 700 Desktop" will give yourself full access while giving everybody else NOTHING.
Assignment • • Please answer the following questions: What is the command to make sure that the file "myfirstscript" is world executable, and at the same time you have full control and the group public has full control? If you see that the permissions for a file are "drwxr-xr-x", who can write to it? If you see that the permissions for a file are "-rwxrwxrwx", who can write to it? You see that the permissions on file great. Text are "-rw-------", what command would you issue to make this file have the same permissions as the file in question number 3? What's the command to issue to make sure everybody in your group and yourself have read and write access to the file great. Text but the world can only read it? You want everybody in the world to be able to execute your file, great. Program, what is the command to accomplish this. (note: a program usually produces an output that is read). For webmasters: Why do you suppose cgi scripts are usually chmod 755?
Archiving Utilities in UNIX & LINUX In this lesson, we are going to practice using the tar, gzip, and bzip 2 utilities. These archiving utilities are commonly used in Linux and UNIX systems. Every Operating System has its own archiving utilities for Back Up and Recovery.
TAR tar stands for Tape ARchive. It was originally designed for tape backups, but it is used to create a tar file anywhere on the filesystem. tar creates one "tar file" (also known as a "tarball") out of several files and directories. A tar file isn't compressed, it's just a heap of files assembled together in "one container". So, the tar file will take up the same amount of space as all the individual files combined, plus a little extra. A tar file can be compressed by using gzip or bzip 2. Here are some examples: tar -xvf example. tar Extract the contents of example. tar and display the files as they are extracted. tar -cf backup. tar /home/ftp/pub Create a tar file named backup. tar from the contents of the directory /home/ftp/pub tar -tvf example. tar list contents of example. tar to the screen
Practice • • • Log in to the linux box and make sure you are in the home directory. Do an ls -al to see what is in the home directory. We should have a directory called "Desktop". cd into that directory and look around. Type "cd" to bring yourself back to the home directory. Again, confirm that you are in the home directory. Type in the following command: "tar -cvf desktop. tar Desktop" (please pay attention to Ca. Se) Do an ls -al again to see what is in the directory now. You should see a file called desktop. tar Now type in "mv Desktop. old" to rename the Desktop directory. Do an ls -l to confirm that the directory name has been changed and there no longer is a directory named Desktop in the home directory. cd into the Desktop. old directory and confirm that the files are the same as what you saw in #2. cd back to your home directory (to move up (back) one directory, you can type in "cd. . " Now type in the command "tar -xvf desktop. tar" to extract the contents of the archive. Do an ls -al again. You should see the original Desktop directory. cd into it and make sure the files are in it. Remove the tar file and the Desktop. old directories if everything worked properly. ("rm desktop. tar", "rm -rf Desktop. old")
GZIP • • • gzip is the original UNIX ZIP format. It's common to first "tar" files and then compress them afterwards using gzip. These files are normally given the extentions. tar. gz to show that they are tar archives zipped up with gzip. You may also see the extension, . tgz. An archive that is compressed with gzip is compatible with Win. Zip and Pk. Zip. So, a file zipped up on a UNIX box can be unzipped with a Windows box. Here are some examples: To compress a file using gzip, execute the following command: gzip filename. tar (where filename. tar is the name of the file you wish to compress) The result of this operation is a file called filename. tar. gz. By default, gzip will delete the filename. tar file. To decompress a file using gzip, execute the following command: gzip -d filename. tar. gz The result of this operation is a file called filename. tar. By default, gzip will delete the filename. tar. gz file. You can also decompress the file using the command: gunzip filename. tar. gz This is the same as using the gzip -d command. There are many options that you can use with gzip. Do a man on the utility to learn more.
BZIP 2 bzip 2 and bunzip 2 are file compression and decompression utilities. The bzip 2 and bunzip 2 utilities are newer than gzip and gunzip and are rapidly gaining popularity. The bzip 2 utility is capable of greater compression ratios than gzip. Therefore, a bzip 2 file can be 10 -20% smaller than a gzip version of the same file. Usually, files that have been compressed by bzip 2 will have a. bz 2 extension. The utilities work very similar to gzip and gunzip. Here a couple of examples. To compress a file using bzip 2, execute the following command: bzip 2 filename. tar (where filename. tar is the name of the file you wish to compress) The result of this operation is a file called filename. tar. bz 2. By default, bzip 2 will delete the filename. tar file. To decompress a file using gzip, execute the following command: bunzip 2 -d filename. tar. bz 2 The result of this operation is a file called filename. tar. By default, bunzip 2 will delete the filename. tar. bz 2 file.
File Structure & Organization • UNIX/Linux filesystems are very different from Windows. UNIX/Linux is organized in a hierarchy, starting with "slash. " Slash is this: / The rest of the directories and subdirectories continue down from slash. For example, the directory, /etc/httpd/ , is a subdirectory in the /etc (pronounced "etsie") directory. /etc/ is a directory in slash. Unlike Windows that represents drives as letters (e. g. , a: , c: , e: ), all devices, including hard drives, cdroms, and other storage media in Linux all fit into the normal directory structure. Devices are mounted onto the file system tree and, as a user, you might not be able to tell if a directory is actually located on your own hard drive or on a hard drive of another computer. • Linux, allows for the great level of flexibility in terms of its file systems. You can literally put in four drives and "mount" them to various directories in your tree. Here are some common mounts you should know about:
• /(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. swap. Unless you have massive amounts of memory, you are going to need some swap space. In Windows, you have Win 386. swp; here, it's a separate drive partition. /boot. This 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. /usr. This 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. /dev. This is the directory where all of your devices are. There 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/sg 0 is your robotic arm. /etc. This 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. /var. Many of the system log files are here, as well as spools (mail, printer. . . )/bin. This directory is the home of binary executables. These include the common commands we have already learned like ls, cat, gzip and tar.