📄 u2
字号:
if you give thecommand.UL pwd ,it will print something like.P1/usr/your\(hyname.P2This says that you are currently in the directory.UL your-name ,which is in turn in the directory.UL /usr ,which is in turn in the root directorycalled by convention just.UL / .(Even if it's not called.UL /usron your system,you will get something analogous.Make the corresponding changes and read on.).PPIf you now type.P1ls /usr/your\(hyname.P2you should get exactly the same list of file namesas you get from a plain.UL ls :with no arguments,.UL lslists the contents of the current directory;given the name of a directory,it lists the contents of that directory..PPNext, try.P1ls /usr.P2This should print a long series of names,among which is your own login name.UL your-name .On many systems, .UL usris a directory that contains the directoriesof all the normal users of the system,like you..PPThe next step is to try.P1ls /.P2You should get a response something like this(although again the details may be different):.P1bindevetclibtmpusr.P2This is a collection of the basic directories of filesthatthe systemknows about;we are at the root of the tree..PPNow try.P1cat /usr/your\(hyname/junk.P2(if.UL junkis still around in your directory).The name.P1/usr/your\(hyname/junk.P2is called the.UL pathnameof the file thatyou normally think of as ``junk''.``Pathname'' has an obvious meaning:it represents the full name of the path you have to follow from the rootthrough the tree of directories to get to a particular file.It is a universal rule inthe.UC UNIXsystemthat anywhere you can use an ordinary filename,you can use a pathname..PPHere is a picture which may make this clearer:.P1 1.ft R.if t .vs 9p.if t .tr /\(sl.if t .tr ||.ce 100(root)/ | \e/ | \e/ | \e bin etc usr dev tmp / | \e / | \e / | \e / | \e / | \e/ | \e/ | \eadam eve mary/ / \e \e / \e junkjunk temp.ce 0.br.tr //.P2.LPNotice that Mary's.UL junkis unrelated to Eve's..PPThis isn't too exciting if all the files of interest are in your owndirectory, but if you work with someone elseor on several projects concurrently,it becomes handy indeed.For example, your friends can print your book by saying.P1pr /usr/your\(hyname/chap*.P2Similarly, you can find out what files your neighbor hasby saying.P1ls /usr/neighbor\(hyname.P2or make your own copy of one of his files by.P1cp /usr/your\(hyneighbor/his\(hyfile yourfile.P2.PPIf your neighbor doesn't want you poking around in his files,or vice versa,privacy can be arranged.Each file and directory has read-write-execute permissions for the owner,a group, and everyone else,which can be setto control access.See.UL ls (1)and.UL chmod (1)for details.As a matter of observed fact,most users most of the time find openness of morebenefit than privacy..PPAs a final experiment with pathnames, try.P1ls /bin /usr/bin.P2Do some of the names look familiar?When you run a program, by typing its name after the prompt character,the system simply looks for a file of that name.It normally looks first in your directory(where it typically doesn't find it),then in.UL /binand finally in.UL /usr/bin .There is nothing magic about commands like.UL cator.UL ls ,except that they have been collected into a couple of places to be easy to find and administer..PPWhat if you work regularly with someone else on common informationin his directory?You could just log in as your friend each time you want to,but you can also say``I want to work on his files instead of my own''.This is done by changing the directory that you arecurrently in:.P1cd /usr/your\(hyfriend.P2(On some systems,.UL cdis spelled.UL chdir .)Now when you use a filename in something like.UL cator.UL pr ,it refers to the file in your friend's directory.Changing directories doesn't affect any permissions associatedwith a file \(emif you couldn't access a file from your own directory,changing to another directory won't alter that fact.Of course,if you forget what directory you're in, type.P1pwd.P2to find out..PPIt is usually convenient to arrange your own filesso that all the files related to one thing are in a directory separatefrom other projects.For example, when you write your book, you might want to keep all the textin a directory called.UL book .So make one with.P1mkdir book.P2then go to it with.P1cd book.P2then start typing chapters.The book is now found in (presumably).P1/usr/your\(hyname/book.P2To remove the directory.UL book ,type.P1rm book/*rmdir book.P2The first command removes all files from the directory;the secondremoves the empty directory..PPYou can go up one level in the tree of files by saying.P1cd ...P2.UL .. '' ``is the name of the parent of whatever directory you are currently in.For completeness,.UL . '' ``is an alternate namefor the directory you are in..SHUsing Files instead of the Terminal.PPMost of the commands we have seen so far produce outputon the terminal;some, like the editor, also take their input from the terminal.It is universal in.UC UNIXsystemsthat the terminal can be replaced by a filefor either or both of input and output.As one example,.P1ls.P2makes a list of files on your terminal.But if you say.P1ls >filelist.P2a list of your files will be placed in the file.UL filelist(whichwill be created if it doesn't already exist,or overwritten if it does).The symbol.UL >means ``put the output on the following file,rather than on the terminal.''Nothing is produced on the terminal.As another example, you could combineseveral files into one by capturing the output of.UL catin a file:.P1cat f1 f2 f3 >temp.P2.PPThe symbol.UL >>operates very much like.UL >does,except that it means``add to the end of.''That is,.P1cat f1 f2 f3 >>temp.P2means to concatenate.UL f1 ,.UL f2 and.UL f3to the end of whatever is already in.UL temp ,instead of overwriting the existing contents.As with.UL > ,if .UL tempdoesn't exist, it will be created for you..PPIn a similar way, the symbol.UL <means to take the inputfor a program from the following file,instead of from the terminal.Thus, you could make up a script of commonly used editing commandsand put them into a file called.UL script .Then you can run the script on a file by saying.P1ed file <script.P2As another example, you can use.UL edto prepare a letter in file.UL let ,then send it to several people with.P1mail adam eve mary joe <let.P2.SHPipes.PPOne of the novel contributions ofthe.UC UNIXsystemis the idea of a.ulpipe.A pipe is simply a way to connect the output of one programto the input of another program,so the two run as a sequence of processes \(ema pipeline..PPFor example,.P1pr f g h.P2will print the files.UL f ,.UL g ,and.UL h ,beginning each on a new page.Suppose you wantthem run together instead.You could say.P1cat f g h >temppr <temprm temp.P2but this is more work than necessary.Clearly what we want is to take the output of.UL catandconnect it to the input of.UL pr .So let us use a pipe:.P1cat f g h | pr.P2The vertical bar .UL |means totake the output from.UL cat ,which would normally have gone to the terminal,and put it into.UL prto be neatly formatted..PPThere are many other examples of pipes.For example,.P1ls | pr -3.P2prints a list of your files in three columns.The program.UL wccounts the number of lines, words and characters inits input, and as we saw earlier,.UL whoprints a list of currently-logged on people,one per line.Thus.P1who | wc.P2tells how many people are logged on.And of course.P1ls | wc.P2counts your files..PPAny programthat reads from the terminalcan read from a pipe instead;any program that writes on the terminal can drivea pipe.You can have as many elements in a pipeline as you wish..PPMany.UC UNIXprograms are written so that they will take their input from one or more filesif file arguments are given;if no arguments are given they will read from the terminal,and thus can be used in pipelines..UL pris one example:.P1pr -3 a b c.P2prints files.UL a ,.UL band.UL cin order in three columns.But in.P1cat a b c | pr -3.P2.UL prprints the information coming down the pipeline,still inthree columns..SHThe Shell.PPWe have already mentioned once or twice the mysterious``shell,''which is in fact.UL sh (1).The shell is the program that interprets what you type ascommands and arguments.It also looks after translating.UL * ,etc.,into lists of filenames,and.UL < ,.UL > ,and.UL |into changes of input and output streams..PPThe shell has other capabilities too.For example, you can run two programs with one command lineby separating the commands with a semicolon;the shell recognizes the semicolon andbreaks the line into two commands.Thus.P1date; who.P2does both commands before returning with a prompt character..PPYou can also have more than one program running.ulsimultaneouslyif you wish.For example, if you are doing something time-consuming,like the editor scriptof an earlier section,and you don't want to wait around for the results before starting something else,you can say.P1ed file <script &.P2The ampersand at the end of a command linesays ``start this command running,then take further commands from the terminal immediately,''that is,don't wait for it to complete.Thus the script will begin,but you can do something else at the same time.Of course, to keep the output from interferingwith what you're doing on the terminal,it would be better to say.P1ed file <script >script.out &.P2which saves the output lines in a filecalled.UL script.out ..PPWhen you initiate a command with.UL & ,the systemreplies with a numbercalled the process number,which identifies the command in case you later wantto stop it.If you do, you can say.P1kill process\(hynumber.P2If you forget the process number,the command.UL pswill tell you about everything you have running.(If you are desperate,.UL kill\ 0will kill all your processes.)And if you're curious about other people,.UL ps\ awill tell you about.ulallprograms that are currently running..PPYou can say.P1 1(command\(hy1; command\(hy2; command\(hy3) &.P2to start three commands in the background,or you can start a background pipeline with.P1command\(hy1 | command\(hy2 &.P2.PPJust as you can tell the editoror some similar program to take its inputfrom a file instead of from the terminal,you can tell the shell to read a fileto get commands.(Why not? The shell, after all, is just a program,albeit a clever one.)For instance, suppose you want to set tabs onyour terminal, and find out the dateand who's on the system every time you log in.Then you can put the three necessary commands.UL tabs , (.UL date ,.UL who )into a file, let's call it.UL startup ,and then run it with.P1sh startup.P2This says to run the shell with the file.UL startupas input.The effect is as if you had typed the contents of.UL startupon the terminal..PPIf this is to be a regular thing,you can eliminate the need to type.UL sh :simply type, once only, the command.P1chmod +x startup.P2and thereafter you need only say.P1startup.P2to run the sequence of commands.The.UL chmod (1)command marks the file executable;the shell recognizes this and runs it as a sequence of commands..PPIf you want .UL startupto run automatically every time you log in,create a file in your login directory called.UL .profile ,and place in it the line.UL startup .When the shell first gains control when you log in,it looks for the .UL .profilefile and does whatever commands it finds in it.We'll get back to the shell in the sectionon programming.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -