Outline
Working with files and directories Working with file editor pico
To be done by the next class Assignment 1.
05/24/11 unix-lect02-file1.pdf 1 Review: Major Components of the Unix OS
Kernel The master control program to control both hardware and software Schedules tasks and provides multitasking and multi-user operations Manages resources Shell Interprets user commands Passes user commands to the kernel for execution (executes programs) File System Information organized as files and directories (special files) Utilities Software tools provided as part of the OS
05/24/11 unix-lect02-file1.pdf 2 Review: Syntax of UNIX Command Lines
A UNIX command may have arguments, which can be an option or a filename The general format for UNIX command line is command option(s) filename(s) A command is typically in lowercase Options modify the way in which a command works Options are often single letters prefixed with a dash Some of options can be made from complete words There are also special characters (such ‘>’, ‘<‘, ‘&’, ‘|’)
05/24/11 unix-lect02-file1.pdf 3 Files
The most common entity users manipulate (directly or indirectly) is files (and directories) Files A well defined repository of information saved on a storage device Program or component of a program Arbitrary text An executable (binary text) A UNIX operating system itself consists of a set of files and directories
05/24/11 unix-lect02-file1.pdf 4 File Examples
The linux operating system on shell.cs.fsu.edu is saved in files -rw-r--r-- 1 root root 1401588 Jun 9 2005 /boot/vmlinuz-2.6.9-11.ELsmp -rw-r--r-- 1 root root 390536 Jul 11 2005 /boot/initrd-2.6.9-11.ELsmp.img The tcsh shell program is a file -rwxr-xr-x 1 root root 307488 Feb 17 2005 /bin/tcsh The cpuinfo of shell.cs.fsu.edu is a file -r--r--r-- 1 root root 0 Jan 8 17:17 /proc/cpuinfo The usernames and their information are saved in a file -rw-r--r-- 1 root root 1473 Dec 22 2005 /etc/passwd The information about the file systems is a file -rw-r--r-- 1 root root 1212 Sep 8 15:37 /etc/fstab 05/24/11 unix-lect02-file1.pdf 5 Directories
Special files called directories contain pointers to other files In other words, a directory is a file that contains a list of files and subdirectories A directory is changed when a file name is changed or a file is deleted Structure of directories Hierarchically structured like an inverted tree / is the starting directory or “root”
05/24/11 unix-lect02-file1.pdf 6 Example Set of Directories and Files /
bin home etc
tcsh majors grads faculty fstab
… nienaber …
… research teaching …
vision cop3353 …
notes homework
05/24/11 unix-lect02-file1.pdf 7 unix-lect01-intro.ppt unix-lect02-file1.ppt Identifying Files and Directories
Pathnames Identifying a file by following a sequence of directories until you reach the file/directory / is also the separator among the subdirectories and final file Examples /bin/tcsh /etc/fstab /home/grads/nienaber/teaching/cop3353/notes/unix-lect02- file1.ppt
05/24/11 unix-lect02-file1.pdf 8 Current Working Directory
In order to be able to specify a file name or directory more efficiently, the UNIX (actually the shell program) also remembers the directory where you currently work You can then identify a file or directory relative to the current working directory Suppose the current working directory is “home/grads/nienaber”, then: “teaching” would refer to the same directory as: /home/grads/nienaber/teaching
05/24/11 unix-lect02-file1.pdf 9 More on Pathnames
Absolute pathnames start at root /home/grads/nienaber /bin/tcsh Relative pathnames start at the current working directory
Special symbols for the current directory and parent directory “..” refers to the parent directory (the directory “above”) “.” is the current directory Referencing user directories ~nienaber is the absolute pathname to the user’s home directory “nienaber” (directory “/home/grads/nienaber” in this example) ~/ is shorthand for the absolute path to your own user directory For me, it is the same as ~nienaber 05/24/11 unix-lect02-file1.pdf 10 Pathname Examples
/
bin home etc
tcsh majors grads faculty fstab
… nienaber …
… research teaching …
vision cop3353
notes homework
05/24/11 unix-lect02-file1.pdf 11 unix-lect01-intro.ppt unix-lect02-file1.ppt Potential Exam Questions
True/false For any file, the absolute path to the file always starts with /. [T/F] For any directory, the absolute path to the directory is always the same. [T/F] For any file or directory, its relative pathname depends on the current working directory and is not unique. [T/F] Using relative pathnames only, some files and directories can not be referenced when the current working directory is at the bottom of the directory tree (note / is at the top of the tree). [T/F] 05/24/11 unix-lect02-file1.pdf 12 Common UNIX File Commands
ls List files and subdirectories cat View file content more/less View file content (pause each screen) touch Create a file / update its time stamp cp Copy file to a new file mv Move file to a new directory, rename file rm Delete a file
05/24/11 unix-lect02-file1.pdf 13 Common Directory Commands
pwd Display absolute pathname to current working directory
mkdir Create a directory
rmdir Remove a directory
cd Change the current working directory
05/24/11 unix-lect02-file1.pdf 14 Command Examples
pwd Prints the absolute pathname of the current working directory ls –l The ls command lists the files and subdirectories in a directory. The ‘l’ flag gives detailed information. touch The touch commands creates an empty file or updates the timestamp of an existing file cp The cp command copies the contents of a file or directory to another file or directory (two parameters). The ‘i’ flag asks before it replaces an existing file; the r flag recursively copies all subdirectories also
05/24/11 unix-lect02-file1.pdf 15 Command Examples – cont.
mv The mv command renames a file (it takes two arguments) mv oldfilename newfilename If the second argument is a directory it moves it to that directory cd Changes the current working directory to another one cd notes cd .. cd ../notes Command --help Most commands have a summary of options via --help (or – h) Manual pages give more detailed information 05/24/11 unix-lect02-file1.pdf 16 Characters in Filenames
File names can contain any characters except “/”, but it is recommended that you use upper or lower case letters, numbers, and “-” “.” For example, although a file name could contain a space or spaces confusing name commands using this would not work correctly unless you tell the shell to not break an argument at the spaces by quoting the filename or using special characters rm “confusing name” (or rm confusing\ name)
05/24/11 unix-lect02-file1.pdf 17 File Permissions
Three types of running programs (or processes) can access a file User (or owner): process spawned by the user who created the file Group: process spawned by a member of the same group of a file Other: process spawned by anyone else Permission types Read: access to a file or list files of a directory Write: modify / remove / write to a file (directory) Execute: run a file as a program or enter a directory
05/24/11 unix-lect02-file1.pdf 18 Example Outputs
Current permissions can be viewed using “ls –l” First line is the number of disk blocks (1 block is 1024 (or 512) bytes) taken up by all the files
05/24/11 unix-lect02-file1.pdf 19 Columns in the Display from “ls –l” First entry in a line is the mode The first character is d for directory, else - for a normal file The remain 9 characters in groups of threes are r, w, x permissions for user, group and other respectively (- indicates not having that permission) Second entry indicates number of links to the file (usually 1 for a file and 2 for a directory) Third entry is the user id of owner and the fourth entry is the group id Fifth entry is the number of bytes of the file Sixth entry is the date the file was last modified The last entry is the file/directory name
05/24/11 unix-lect02-file1.pdf 20 Potential Exam Questions
True/false In order to read homework1.txt, a user must have read permission and execute permission for all directories along its absolute path. [F] Without writing permssion to the current working directory, a user can not delete a file from it. [T]
05/24/11 unix-lect02-file1.pdf 21 Changing Permissions
Using the chmod command to set/change permissions Numeric (as octal numbers) Directly set the permissions for u, g, o using each 3 bit value as an octal value chmod 754 unix-lect02-file1.ppt will set to 111 101 100 or rwx r-x r-- chmod 700 unix-lect02-file1.ppt will set to 111 000 000 or rwx ------ chmod 644 unix-lect02-file1.ppt will set to 110 100 100 or rw- r-- r--
05/24/11 unix-lect02-file1.pdf 22 Changing Permissions - cont.
Symbolic Format: chmod mode,[mode]
05/24/11 unix-lect02-file1.pdf 23 Changing Permissions - cont.
Symbolic Format: chmod mode,[mode]
05/24/11 unix-lect02-file1.pdf 24 Conversion Between Numeric and Symbolic Permissions
Symbolic Binary Numeric --- 000 0 --x 001 1 -w- 010 2 -wx 011 3 r-- 100 4 r-x 101 5 rw- 110 6 rwx 111 7
05/24/11 unix-lect02-file1.pdf 25 Potential Exam Question
The most recent permission of homework1.txt is set using “chmod 0751”, Give its permission (using the permission format given by “ls –l”) after chmod ug-w,o+r homework1.txt
05/24/11 unix-lect02-file1.pdf 26 Potential Exam Question
The most recent permission of homework1.txt is set using “chmod 0751”, Give its permission (using the permission format given by “ls –l”) after chmod ug-w,o+r homework1.txt
It will be –r-xr-xr-x
05/24/11 unix-lect02-file1.pdf 27 Wildcards
Multiple files can be specified using wildcards An asterisk “*” matches any number of characters in a filename con* will match con, condor, constant.exe *.c will match all files that end in .c rm * will remove all the files in a directory A “?” matches any single character in a filename b?t will match bit, bot, bat. It will not match bt or boot Square brackets “[]” will match any one of the characters in the brackets. A hyphen “-” can be used to match any of a range of consecutive characters. [bhr]at will match bat, hat and rat chap[5-8].c will match chap5.c, chap6.c, chap7.c and chap8.c
05/24/11 unix-lect02-file1.pdf 28 Wildcard Examples
Common UNIX utility programs are saved in /bin and /usr/bin We can find out utilities starting with l by ls /bin/l* /usr/bin/l* We can find out the shell programs installed by (by convention shell programs end with sh) ls /bin/*sh We find out files related to networking by ls /etc/*host*
05/24/11 unix-lect02-file1.pdf 29 Dot Files
“ls *” actually does not list all the files There are hidden files (starting with ‘.’, also called dot files (but excluding “.” “..”)) that shell and other programs to use to customize settings and configurations “ls –a *” will list also the dot files You can also list the dot files by using “ls .*” Note that these dot files will prevent rmdir to remove a directory
05/24/11 unix-lect02-file1.pdf 30 Dot Files – cont.
For example, suppose subdirectory atmp2 contains the following files
05/24/11 unix-lect02-file1.pdf 31 Dot Files – cont.
For example, suppose subdirectory atmp2 contains the following files
You can use “rm –r atmp2” or remove the dot files first using “rm atmp2/.[a-z]*” 05/24/11 unix-lect02-file1.pdf 32 Potential Exam Questions
True/false “ls *[0-9]” will include all the following files: [T/F] file1 file2 file3.h file4 file5.o file6.cpp - “ls *[0-9].*” will include all the files given above. [T/F] Suppose that temp is a directory under the current working directory, after running “ls temp”, there is no output, however, “rmdir temp” fails with the following message rmdir: `temp': Directory not empty Explain why.
05/24/11 unix-lect02-file1.pdf 33 Editors
• Common text editors that are available (none have many of the features available on word processors) for plain text files such as programs, shell scripts, etc. – vi (vee-eye) • Available on almost all Unix machines • Fairly powerful and sophisticated – emacs (ee-macs) • Also widely available • Powerful and popular – pico • Easier to learn but simpler and not as powerful 05/24/11 unix-lect02-file1.pdf 34 Starting pico
• The command "pico" at a shell prompt will start the "pico" text editor with an empty buffer $ pico • Specifying a file name will have "pico" open that file (or start a new file) $ pico testfile1 • Basic Command – Arrow keys are used to navigate around the document – Typing will insert text at the point of the cursor – The caret sysmbol (^) indicates you must press and hold the control (ctrl) key first, then press the command key – Some available commands are at the bottom of the pico window – ^o writes “out” the text to a file (a prompt will let you specify the name)
05/24/11 – ^x exits pico unix-lect02-file1.pdf 35 Marking and cutting and pasting in pico
• You cannot use your mouse in "pico" (actually, the mouse works to cut and paste because of the SSHClient program, but you must learn how to work without it) • ^^ (ctrl-shift-^) begins marking text at the current cursor position • Use the arrow keys to mark text • ^k cuts text (kills), • ^u then brings the text back at the current cursor position
05/24/11 unix-lect02-file1.pdf 36 pico command summary
(arrows) Move cursor (bksp) Move cursor left one space, deleting character ^a Move to beginning of line ^b Move back one character (same as left arrow) ^e Move to end of line ^f Move forward one character (same as right arrow) ^n Move to next line (same as down arrow) ^p Move to previous line (same as up arrow) ^v Move forward one page ^y Move back one page ^(space) Move to next word
05/24/11 unix-lect02-file1.pdf 37 pico command summary continued
^c Shows current position ^d Delete character at current position ^g Display help file (^V and ^Y to scroll through) ^h Delete previous character (same as bksp) ^i Insert TAB character (same as tab) ^j Justify paragraph ^^ Begin selecting text at current cursor position ^k Cut selected text ^l Redraw screen ^o Output current buffer to a file (save) ^r Insert text from a file ^u Undelete last line, series of lines, or marked block you deleted. Can also "unjustify" ^w Search file for text ^x Exit pico 05/24/11 unix-lect02-file1.pdf 38 Next Time
Creating files using vi and emacs and checking file contents
To be done by the next class Assignment #1
05/24/11 unix-lect02-file1.pdf 39