1 / 50

Introduction to Unix – CS 21

Introduction to Unix – CS 21. Lecture 4. Lecture Overview. * cp, mv, and rm Looking into files The file command head and tail cat and more What we’ve seen so far. Homework and Quiz. Homework #1 now available at http://www.cs.ucr.edu/~villarre/cs21/homework1.html

mai
Download Presentation

Introduction to Unix – CS 21

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Unix – CS 21 Lecture 4

  2. Lecture Overview • * • cp, mv, and rm • Looking into files • The file command • head and tail • cat and more • What we’ve seen so far

  3. Homework and Quiz • Homework #1 now available at http://www.cs.ucr.edu/~villarre/cs21/homework1.html • Due next Thursday (January 20th) at the beginning of class • Quiz #1 next Thursday (January 20th) • Everything covered up through today will be fair game • Homework assignments, lab assignments, reading assignments, and lecture material

  4. The First Wildcard • * (asterisk) • Wildcards are just like wild cards in poker • They can be anything (within reason) • In Unix, this means that * can be replaced by anything in the current directory • * actually gets replaced by everything in the current directory

  5. Example Of * Usage

  6. Why Is This Useful? • At first glance, this doesn’t seem to mean much • After all, ls without the * will list all the files anyway • You can specify a little more than every file • *.txt will match all files that end in .txt

  7. Example Of Advanced * Usage

  8. Moving Files Around • There are a couple of essential commands to move files around • cp • mv • Remember, you can use touch to create an empty file to play around with

  9. cp Usage • The cp command makes a copy of a file • Usage: cp OLDFILE NEWFILE

  10. Examples Of cp

  11. Problems With cp?

  12. Commonly Used Flags With cp • -i flag • Asks if you want to write over a file that already exists • -r flag • Recursively will copy all files and subdirectories

  13. What Does Recursion Mean? • The same program gets called over and over again • In this context, cp gets called on all files in all subdirectories • Parent directories and other files “above” the current directory are not affected

  14. /usr ~/temp /usr/bin /usr/misc /usr/zzz ~/temp/usr A Graphical Representation Of Recursion cp –r /usr ~/temp / /home

  15. Keep In Mind All Of The Permissions • You will only be able to copy a file that you have read permission on • You will only be able to create a file in a directory that you have write permission in

  16. mv Usage • The mv command will move a file from one location to a new location • Usage: mv OldFile NewFile • You can also think of this as a rename command

  17. Examples Of mv

  18. How Does This Compare To Windows? • Windows will let you drag and drop files from one location to another • Right click if you would like to copy • A little progress bar shows up animating the file being moved over to the new folder • Other than that, it is exactly the same

  19. rm Usage • The rm command will remove a file • This doesn’t normally include directories • Usage: rm filename

  20. Examples Of rm

  21. Commonly Used Flags • -i • Verify the delete • -f • Force the removal of a file • If you have permission, the file is gone, regardless of any warnings that might pop up • “Yeah, yeah, just do it” • Overrides the –i flag • -r • Recursively delete files

  22. Dangers Of rm • Unix Is missing something you are probably used to • rm is probably one of the most dangerous commands in Unix

  23. Once It’s Gone, It’s Gone… • There is no way to get a file that has been removed back • Only run rm if you are absolutely sure you want to remove a file • The –i flag provides a little protection • Prompts the user if they are really sure they want to delete the file

  24. Extreme Dangers Of rm • rm –rf ~ • Say “Bye, bye” to your home directory • rm –rf / • You won’t have permission to delete much, but… • If you are root, say goodbye to the entire system! • rm –rf . • rm –rf * • Don’t look as dangerous, but you have to be absolutely certain you know where you’re at

  25. So Why Use rm –rf Then? • The most destructive command in Unix, why would you ever want to use it? • Just so happens that you WILL want to remove large portions of your files at some time (most likely many times) • Much easier to run “rm – rf” than delete each file individually

  26. Avoiding The Dangers Of rm • The best way is to make sure you are always using the –i flag and only use –f when you are certain • Always check where your current directory is so you don’t delete the wrong file • Make copies of important files just in case

  27. Unix, The Multichoice OS • What’s the difference? • rm –rf subdirectory/ • rmdir subdirectory/

  28. Multichoice Again • What’s the difference? • mv fileA fileB • cp fileA fileB rm fileA

  29. Examining Files Closer • As previously stated, everything in Unix is a file • But different files have different uses • How do you tell what type a file is? • Example: In Windows, a *.doc file is a Word Document • No such restriction is enforced in Unix • A *.doc file might even be an executable!

  30. The file Command • A helpful command to get you started in your quest for knowledge: file • Checks the first few bytes of the file in question and takes its best guess as to what type of file it is • Sometimes file gets it wrong, but most of the time it is pretty good

  31. Example Different Types Of Files

  32. What If I Want To Actually Look Inside The File Myself? • If the file is a text file, several options exist • head • tail • cat • more • If the file is a binary (executable), you don’t want to read it! (trust me)

  33. The head Command • Print out the first few lines of a text file • 10 by default • Provides a quick way to see if this is the file you’re looking for • Doesn’t bombard you with a million line file scrolling off the screen • Usage: head FILE

  34. Common Flags For The head Command • -6 • Only print out the first 6 lines • Actually, any number works here the same way • What counts as a line? • Everything up until the new line terminator

  35. Examples Of head Usage

  36. The tail Command • Pretty much the opposite of head • Prints out the last few lines of a file • 10 by default • Usage: tail FILE • Just like head, -NUM • Prints out the last NUM lines

  37. Examples Of tail Usage

  38. The cat Command • The cat command will print out an entire file to the screen • Usage: cat FILE • Cat? • Short for concatenate • This command can be used to print out multiple files one right after the other • cat FILE1 FILE2

  39. Problem With cat • If the file is very large, it will scroll off the screen too fast to read • No way to read a scrolling file without stopping the program • Cntrl-C will kill a running program

  40. The more Command • Works exactly like cat, but doesn’t automatically scroll the screen • Usage: more FILE • This is the first truly interactive program we’ve seen • You can control how the program runs while it is running

  41. More Example • How do you scroll to the next screen? • Hit the space bar

  42. Advanced more usage • Return will move you one line • NUM followed by return will move you NUM lines • Example: 5 [Return] displays the next 5 lines • q will quit more without finishing

  43. less Is more • A better version of more exists: less • Allows all of the same options as more • Allows easier moving through the file • Arrow keys and page up, page down will move you both forward and backward • Which you choose to use is, of course, up to you

  44. What About Binary Files?

  45. You Now Should Have Enough Info To Cause Some Damage • You now have the ability to: • Wander about the system • Create simple files and change all the properties of these files • Copy and move files around • Check out what type of files you are looking at and read the interesting ones • Delete files you no longer want

  46. The Class So Far… • History of Unix and the hacker connection • Logging on and getting help • man • Environment variables • The Unix Directory Structure • ls • cd • pushd and popd • Relative and absolute pathnames • . And ..

  47. Class Summary Continued • Disk usage • du • Compressing files • Symbolic Links • Ownership and permissions • chmod • umask

  48. The Class So Far Continued • Moving files • cp • mv • Checking the contents and type of files • file • head and tail • cat and more

  49. In Lab Today • You will practice setting permissions and the effect they have • chmod and umask • Start creating simple files and moving them around the system • Read files using head, tail, cat, and more

  50. Next Week • Things really start to get interesting as we start getting programs to work together and tie everything together • We’ll look at more uses of wildcards and start making Unix “sentences” with pipes and filters • We’ll look at the oddly named but strangely powerful command: grep

More Related