310 likes | 495 Views
Unix Files. Week Three Non-Directory Files. Week 3 Agenda. Pass back Lab 2 Take Roll Announcements: ACM meeting this Wednesday Jan 30, 11:30 a.m – 12:30 p.m, 111 Nicks Hall. Lecture on files Q & A. Objectives. Reinforce the Unix directory structure.
E N D
Unix Files Week Three Non-Directory Files
Week 3 Agenda • Pass back Lab 2 • Take Roll • Announcements: • ACM meeting this Wednesday Jan 30, 11:30 a.m – 12:30 p.m, 111 Nicks Hall. • Lecture on files • Q & A
Objectives • Reinforce the Unix directory structure. • Reinforce the Unix directory commands. • Introduce the following Unix file commands: • touch • cat • chmod • rm • cp • mv • Introduce the concept of redirection
Non-directory Files • File commands – Unix commands that deal with file contents • touch • cat • chmod • rm • cp • mv
File/Directory Naming(review) • Up to 255 characters in length • Case sensitive (jan not same as Jan) • Recommended characters are: • Upper/lower case (A-Z, a-z) • Numbers (0-9) • Underscore (_) • Period (.) • Comma (,)
touch [-options] filename • Creates a file entry (empty file) or • Modifies the time stamp for an existing file • Options: • -a change the access time • -c do not create a new file (update an existing file) • -m change the modification time (useful for rescheduling builds)
Creating a File (touch) • Creates a file entry (empty file) or • Modifies the time stamp for an existing file • Format: touch [-options] filename • Options: • -a change the access time • -c do not create a new file (update and existing file) • -m change the modification time (useful for rescheduling builds)
touch Example touchnewfile • Creates a file entry called newfile containing nothing (zero bytes)
Create a New Text File (cat) • Allows for the creation of a data file • Format: cat>newfile • usertypes in data from the keyboard, • to terminate input, press ctl-d
cat Example Creating Contents cat > mydata first line second line … last line <ctrl-d> • Creates a file called mydata with typed in data (first line, second line, etc.)
List the Contents of an Existing File (cat) • Displays the contents of a data file • Contents of existingfile are displayed upon screen • Format: cat [-options] existingfile • Options: • -n Precede each line output with its line number. • -b Number the lines, as -n, but omit the line numbers from blank lines.
cat Example Listing Contents catmydata first line second line … last line • List the contents of a file called mydata • Notice the absence of the redirect symbol (>)
chmod [options] mode file • Changes the access mode for a given file; only the owner of the file or the SuperUser can change its access mode. • Options • -R recursively • Can use octal permission numbers • r – 4 : read permission • w – 2 : write permission • x – 1 : execute permission* • For directories execute permission allows users to access a directory without reading it. This is important for creating html directories, allowing web users to view web content within a directory, but not be able to read the directory itself. *
chmod Examples • using octal permissions • chmod 751 myfile • myfile permissions would show as drwxr-x--x give user read (4), write (2), and execute (1) (4+2+1=7) permission give group read (4) and execute (1) (4+1=5) permission give everyone execute (1) permission { { { { directory other user group
chmod [options] mode file • Class of user: • u User (you) • g Group • o Other (all others) • a All u, g, and o • Permission Values: • r Read • w Write • x Execute • Operations: • + add permission • - remove permission • = set permission (replacing existing)
chmod Examples • Using options
Remove a file (rm) • Deletes a file or files By default does not offer confirmation!!! • Format: rm [options] files • -i : interactive,prompts for confirmation before removing any files • -r or –R : Recursively remove directories and subdirectories in the argument list. The directory will be emptied of files and removed.
rm Examples • rmmyfile # removes a file called myfile • rm * # removes all files in the current directory Please note that the pound sign (#) is the symbol used to indicate comments in a Unix command
Copy Files (cp) • Copies a file or files Will overwrite an existing file • Format cp [options] existfile newfile • Options: • -i : interactive,prompts for confirmation before copying onto existing files
cp Examples # copies existfile to newfile cpexistfilenewfile # copy the passwd file from the /etc directory # to the current directory(.) cp /etc/passwd .
Move (Rename) a File (mv) • Renames a file or moves it from one directory to another directory. Will overwrite an existing file • Format: mv [options] oldname newname • Options: • -i : Interactive, prompts before overwriting another file.
mv Examples # rename existfile to newfile mvexistfilenewfile # move the passwd file from the /etc #directory to the current directory * mv /etc/passwd . *This command cannot be done as we do not have write permissions on the file.
Redirection • Redirection deals with changing the source or destination of the standard input and output. • Standard (I/0): • input : the keyboard • redirect is the less than (<) symbol • output: the computer screen • redirect is: • greater than symbol (>) to create a new output file (if file exists writes over it) • two greater than symbols (>>) append to existing file or if not an existing file create a new one
Redirection Examples cat <abc >def3 cat >abc cat >>abc cat abc1 cat <abc2 1.This assumes the file abc does exist. 2.This example is merely a demonstration of redirecting from a file. It is the equivalent of: cat abc 3. This is equivalent to: cp abc def
Redirection • Please remember that redirection can be used with any command that uses the standard input or standard output, not just the cat command! • For example: # Produce a long listing of all of the contents of # the parent directory and send them to the file # dircontents ls –al .. > dircontents
Summary • use rm –r with caution! It will delete all the files in the directory (including subdirectories and their contents), without asking!
Review • Reinforced the Unix directory structure. • Reinforced the Unix directory commands. • Introduced the following Unix file commands: • touch • cat • chmod • rm • cp • mv • Introduced the concept of redirection
Next Time • Please review the following commands in the text: • head • tail • more • less • sort • uniq • wc • tee • cut • We will also introduce pipes (|)
Unix Files Non-Directory Files