Introduction to Unix – CS 21

50
Introduction to Unix – CS 21 Lecture 4

description

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 - PowerPoint PPT Presentation

Transcript of Introduction to Unix – CS 21

Page 1: Introduction to Unix – CS 21

Introduction to Unix – CS 21

Lecture 4

Page 2: Introduction to Unix – CS 21

Lecture Overview * cp, mv, and rm Looking into files

The file command head and tail cat and more

What we’ve seen so far

Page 3: Introduction to Unix – CS 21

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

Page 4: Introduction to Unix – CS 21

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

Page 5: Introduction to Unix – CS 21

Example Of * Usage

Page 6: Introduction to Unix – CS 21

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

Page 7: Introduction to Unix – CS 21

Example Of Advanced * Usage

Page 8: Introduction to Unix – CS 21

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

Page 9: Introduction to Unix – CS 21

cp Usage The cp command makes a copy of

a file Usage: cp OLDFILE NEWFILE

Page 10: Introduction to Unix – CS 21

Examples Of cp

Page 11: Introduction to Unix – CS 21

Problems With cp?

Page 12: Introduction to Unix – CS 21

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

Page 13: Introduction to Unix – CS 21

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

Page 14: Introduction to Unix – CS 21

A Graphical Representation Of Recursion

/

/home

/usr/zzz

/usr

/usr/bin /usr/misc

cp –r /usr ~/temp

~/temp

~/temp/usr

Page 15: Introduction to Unix – CS 21

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

Page 16: Introduction to Unix – CS 21

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

Page 17: Introduction to Unix – CS 21

Examples Of mv

Page 18: Introduction to Unix – CS 21

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

Page 19: Introduction to Unix – CS 21

rm Usage The rm command will remove a file

This doesn’t normally include directories

Usage: rm filename

Page 20: Introduction to Unix – CS 21

Examples Of rm

Page 21: Introduction to Unix – CS 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

Page 22: Introduction to Unix – CS 21

Dangers Of rm

Unix Is missing something you are probably used to rm is probably one of the most

dangerous commands in Unix

Page 23: Introduction to Unix – CS 21

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

Page 24: Introduction to Unix – CS 21

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

Page 25: Introduction to Unix – CS 21

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

Page 26: Introduction to Unix – CS 21

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

Page 27: Introduction to Unix – CS 21

Unix, The Multichoice OS What’s the difference?

rm –rf subdirectory/ rmdir subdirectory/

Page 28: Introduction to Unix – CS 21

Multichoice Again What’s the difference?

mv fileA fileB cp fileA fileB rm fileA

Page 29: Introduction to Unix – CS 21

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!

Page 30: Introduction to Unix – CS 21

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

Page 31: Introduction to Unix – CS 21

Example Different Types Of Files

Page 32: Introduction to Unix – CS 21

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)

Page 33: Introduction to Unix – CS 21

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

Page 34: Introduction to Unix – CS 21

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

Page 35: Introduction to Unix – CS 21

Examples Of head Usage

Page 36: Introduction to Unix – CS 21

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

Page 37: Introduction to Unix – CS 21

Examples Of tail Usage

Page 38: Introduction to Unix – CS 21

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

Page 39: Introduction to Unix – CS 21

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

Page 40: Introduction to Unix – CS 21

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

Page 41: Introduction to Unix – CS 21

More Example

How do you scroll to the next screen? Hit the space bar

Page 42: Introduction to Unix – CS 21

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

Page 43: Introduction to Unix – CS 21

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

Page 44: Introduction to Unix – CS 21

What About Binary Files?

Page 45: Introduction to Unix – CS 21

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

Page 46: Introduction to Unix – CS 21

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 ..

Page 47: Introduction to Unix – CS 21

Class Summary Continued Disk usage

du Compressing files Symbolic Links Ownership and permissions

chmod umask

Page 48: Introduction to Unix – CS 21

The Class So Far Continued Moving files

cp mv

Checking the contents and type of files file head and tail cat and more

Page 49: Introduction to Unix – CS 21

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

Page 50: Introduction to Unix – CS 21

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