Title: Introduction to Unix CS 21
1Introduction to Unix CS 21
2Lecture Overview
-
- cp, mv, and rm
- Looking into files
- The file command
- head and tail
- cat and more
- What weve seen so far
3Homework 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
4The 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
5Example Of Usage
6Why Is This Useful?
- At first glance, this doesnt 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
7Example Of Advanced Usage
8Moving 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
9cp Usage
- The cp command makes a copy of a file
- Usage cp OLDFILE NEWFILE
10Examples Of cp
11Problems With cp?
12Commonly 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
13What 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
14A Graphical Representation Of Recursion
cp r /usr /temp
/
/home
15Keep 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
16mv 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
17Examples Of mv
18How 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
19rm Usage
- The rm command will remove a file
- This doesnt normally include directories
- Usage rm filename
20Examples Of rm
21Commonly 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
22Dangers Of rm
- Unix Is missing something you are probably used
to - rm is probably one of the most dangerous commands
in Unix
23Once Its Gone, Its 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
24Extreme Dangers Of rm
- rm rf
- Say Bye, bye to your home directory
- rm rf /
- You wont have permission to delete much, but
- If you are root, say goodbye to the entire
system! - rm rf .
- rm rf
- Dont look as dangerous, but you have to be
absolutely certain you know where youre at
25So 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
26Avoiding 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 dont delete the wrong file - Make copies of important files just in case
27Unix, The Multichoice OS
- Whats the difference?
- rm rf subdirectory/
- rmdir subdirectory/
28Multichoice Again
- Whats the difference?
- mv fileA fileB
- cp fileA fileB
- rm fileA
29Examining 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!
30The 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
31Example Different Types Of Files
32What 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 dont
want to read it! (trust me)
33The 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
youre looking for - Doesnt bombard you with a million line file
scrolling off the screen - Usage head FILE
34Common 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
35Examples Of head Usage
36The 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
37Examples Of tail Usage
38The 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
39Problem 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
40The more Command
- Works exactly like cat, but doesnt automatically
scroll the screen - Usage more FILE
- This is the first truly interactive program weve
seen - You can control how the program runs while it is
running
41More Example
- How do you scroll to the next screen?
- Hit the space bar
42Advanced 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
43less 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
44What About Binary Files?
45You 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
46The 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 ..
47Class Summary Continued
- Disk usage
- du
- Compressing files
- Symbolic Links
- Ownership and permissions
- chmod
- umask
48The Class So Far Continued
- Moving files
- cp
- mv
- Checking the contents and type of files
- file
- head and tail
- cat and more
49In 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
50Next Week
- Things really start to get interesting as we
start getting programs to work together and tie
everything together - Well look at more uses of wildcards and start
making Unix sentences with pipes and filters - Well look at the oddly named but strangely
powerful command grep