
UNIX T utorial T wo http://www.ee.surrey.ac.uk/Teaching/Unix/unix2.html UNIX Tutorial Two 2.1 Copying Files cp (copy) cp file1 file2 is the command which makes a copy of file1 in the current working directory and calls it file2 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. First, cd to your unixstuff directory. % cd ~/unixstuff Then at the U !" prompt, type, % cp /vol/examples/tutorial/science.txt . ote# $on%t forget the dot . at the end. &emem'er, in U !", the dot means the current directory. The a'o(e command means copy the file science.txt to the current directory, keeping the name the same. ) ote# The directory /vol/examples/tutorial/ is an area to which e(eryone in the school has read and copy access. !f you are from outside the Uni(ersity, you can gra' a copy of the file here. Use %File*+a(e ,s..% from the menu 'ar to sa(e it into your unixstuff directory.- Exercise 2a Create a 'ackup of your science.txt file 'y copying it to a file called science. a! 2.2 Mo(ing files mv (move) mv file1 file2 mo(es )or renames- file1 to file2 To mo(e a file from one place to another, use the m( command. This has the effect of mo(ing rather than copying the file, so you end up with only one file rather than two. !t can also 'e used to rename a file, 'y mo(ing the file to the same directory, 'ut gi(ing it a different name. We are now going to mo(e the file science.'ak to your 'ackup directory. First, change directories to your unixstuff directory )can you remem'er how/-. Then, inside the unixstuff directory, type 1 of 5 01/06/2009 05:05 PM UNIX T utorial T wo http://www.ee.surrey.ac.uk/Teaching/Unix/unix2.html % mv science.bak backups/. Type ls and ls 'ackups to see if it has worked. 2.0 &emo(ing files and directories rm (remove)" rm#ir (remove #irectory) To delete )remo(e- a file, use the rm command. ,s an example, we are going to create a copy of the science.txt file then delete it. !nside your unixstuff directory, type % cp science.txt tempfile.txt % ls % rm tempfile.txt % ls 1ou can use the rmdir command to remo(e a directory )make sure it is empty first-. Try to remo(e the ac!ups directory. 1ou will not 'e a'le to since U !" will not let you remo(e a non2empty directory. Exercise 2 Create a directory called tempstuff using mkdir , then remo(e it using the rmdir command. 2.3 $isplaying the contents of a file on the screen clear (clear screen) 4efore you start the next section, you may like to clear the terminal window of the pre(ious commands so the output of the following commands can 'e clearly understood. ,t the prompt, type % clear This will clear all text and lea(e you with the 5 prompt at the top of the window. cat (concatenate) The command cat can 'e used to display the contents of a file on the screen. Type# % cat science.txt ,s you can see, the file is longer than than the si6e of the window, so it scrolls past making it unreada'le. 2 of 5 01/06/2009 05:05 PM UNIX T utorial T wo http://www.ee.surrey.ac.uk/Teaching/Unix/unix2.html less The command less writes the contents of a file onto the screen a page at a time. Type % less science.txt 7ress the 8space$ ar9 if you want to see another page, and type 8%9 if you want to :uit reading. ,s you can see, less is used in preference to cat for long files. &ea# The head command writes the first ten lines of a file to the screen. First clear the screen then type % head science.txt Then type % head -5 science.txt What difference did the 2; do to the head command/ tail The tail command writes the last ten lines of a file to the screen. Clear the screen and type % tail science.txt <. =ow can you (iew the last 1; lines of the file/ 2.; +earching the contents of a file 'imple searc&in( usin( 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.txt 3 of 5 01/06/2009 05:05 PM UNIX T utorial T wo http://www.ee.surrey.ac.uk/Teaching/Unix/unix2.html then, still in less, type a forward slash 8/9 followed 'y the word to search /science ,s you can see, less finds and highlights the keyword. Type 8n9 to search for the next occurrence of the word. (rep (#on)t as! w&y it is calle# (rep) grep is one of many standard U !" utilities. !t searches files for specified words or patterns. First clear the screen, then type % grep science science.txt ,s you can see, grep has printed out each line containg the word science. >r has it //// Try typing % grep Science science.txt The grep command is case sensiti(e? it distinguishes 'etween +cience and science. To ignore upper*lower case distinctions, use the 2i option, i.e. type % grep -i science science.txt To search for a phrase or pattern, you must enclose it in single :uotes )the apostrophe sym'ol-. For example to search for spinning top, type % grep -i 'spinning top' science.txt +ome of the other options of grep are# $v display those lines that do >T match $n precede each matching line with the line num'er $c print only the total count of matched lines Try some of them and see the different results. $on%t forget, you can use more than one option at a time. For example, the num'er of lines without the words science or +cience is % grep -ivc science science.txt wc (wor# count) , handy little utility is the wc command, short for word count. To do a word count on science.txt, type 4 of 5 01/06/2009 05:05 PM UNIX T utorial T wo http://www.ee.surrey.ac.uk/Teaching/Unix/unix2.html % wc -w science.txt To find out how many lines the file has, type % wc -l science.txt +ummary *omman# Meanin( cp file1 file2 copy file1 and call it file2 mv file1 file2 move or rename file1 to file2 rm file remove a file rmdir directory remove a directory cat file display a file less file display a file a page at a time head file display the first few lines of a file tail file display the last few lines of a file grep 'keyword' file search a file for keywords wc file count num'er of lines/words*characters in file Amazon.com C Programming Language Sams Teach Yourself Unix in Numerical Recipes 3rd Edition Brian W. Kernighan... 24 Hours... William H. Press, ... New $42.75 Dave Taylor New $59.04 New $23.09 Privacy Information ..+tone'[email protected], A Bth >cto'er 2CCC 5 of 5 01/06/2009 05:05 PM.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages5 Page
-
File Size-