500 likes | 588 Vues
Explore Unix basics such as file manipulation using cp, mv, and rm commands. Understand wildcard usage, moving files, file removal, and file types with examples. Learn how to avoid dangers when using rm command in Unix.
E N D
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 • 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
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
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
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
cp Usage • The cp command makes a copy of a file • Usage: cp OLDFILE NEWFILE
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
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
/usr ~/temp /usr/bin /usr/misc /usr/zzz ~/temp/usr A Graphical Representation Of Recursion cp –r /usr ~/temp / /home
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
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
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
rm Usage • The rm command will remove a file • This doesn’t normally include directories • Usage: rm filename
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
Dangers Of rm • Unix Is missing something you are probably used to • rm is probably one of the most dangerous commands in Unix
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
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
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
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
Unix, The Multichoice OS • What’s the difference? • rm –rf subdirectory/ • rmdir subdirectory/
Multichoice Again • What’s the difference? • mv fileA fileB • cp fileA fileB rm fileA
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!
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
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)
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
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
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
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
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
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
More Example • How do you scroll to the next screen? • Hit the space bar
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
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
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
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 ..
Class Summary Continued • Disk usage • du • Compressing files • Symbolic Links • Ownership and permissions • chmod • umask
The Class So Far Continued • Moving files • cp • mv • Checking the contents and type of files • file • head and tail • cat and more
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
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