250 likes | 371 Views
Lecture 2. The Structure of Directories Filename Metacharacters Handling Special Characters Input / Output Redirection Building Complex Commands. The Structure of Directories. A file cc.txt in directory bb under directory aa can be represented as / aa /bb/cc.txt
E N D
Lecture 2 The Structure of Directories Filename Metacharacters Handling Special Characters Input / Output Redirection Building Complex Commands
The Structure of Directories • A file cc.txt in directory bb under directory aa can be represented as /aa/bb/cc.txt • . -Current directory % cd . #still in current directory % ./prog #run ‘prog’ in current dir • ..-Parent directory % cd .. #change to parent directory
Several Important Directories • Tree-like Structure • / The top level • /bin • Contains executable files • /etc • Configuration files • Some executable shell files • /home personal work directory, default place when login.
Cont. • /usr • /usr/inlcudeand /usr/lib • /usr/bin/ -contains more executables • /sbin • Contains executables which can only be executed by the super user (root)
Some Commands • pwd --current full directory path • mkdir -- create a directory • ls ---- list the files in a directory • -l,-m,-1,-R, --sort, --format • tree • tree / –L 1
Access Rights For A File • A file: Who has what permissions?
Access Rights For A File • Permissions are represented by rwx • r mean read • w means write • x means executable • Permissions can be representated by 0-7
Cont. • For example:what is permission 5? • 5 in binary format is : • 101 Can run Can read No write
Access Rights Groups • Three groups of Access rights • Owner • The members within a group • Others • File permission also defined who has what permission, using 3 numbers, for example: 764 Others: can only read owner can: read, write, run Owner group can: read, write
Commands related to the access right of a file • chown change the owner of a file • Example: chownuser_namefile_name • chgrp: change the group of a file • Example: chgrpgroup_namefile_name
Other commands related to the access right of a file • chmod:changepermissions • User: u, g, o, a • Access rights: r, w, x. • chmodu+rwx,g+rx,o+rxfile_name • chmod 755 file_name • Remove permission: chmod o-x file_name
Wildcards / Metacharacters for filename abbreviation • Shell understands wildcards • Metacharacters: * ? [ ] ~ • * -matches anything %ls*.doc # list all files end with .doc • ? –matches single character %ls?.doc # matches a.doc, b.doc, but not ab.doc
Wildcards / Metacharacters for filename abbreviation • […] –matches any of the enclosed charactors %ls [ab].doc # only matches a.doc or b.doc %ls *[cx].doc # math files end with c.doc or x.doc • ~ - is a shortcut of your home directory %cd ~ # change to home directory %cd ~/music # change to /home/username/music directory
Back Quota • `command`- used for command substitution • Example: • set d = `date` • Now d contains the output of date command
Special Characters • Turn off the special meanings by quoting/escaping • “”: disable the special meaning for all chars except $ ` (back quota) and ! (bang) • ‘ ’: disable the special meaning for everything but ! and newline • \: escapes any immediately following char
Quoting – Turn Off Special Chars • What do we get by this command? % echo here is a star * #will print all files here is a star file1 file2 … • To turn off special meaning - surround a string with double quotes: % echo here is a star "*" % echo “here is a star *” % echo ‘here is a star *’ • Create a directory ‘Program Files’ % mkdir Program Files #create two files % mkdir Program\ Files #works % mkdir ‘Program Files’ #works
Programs and Standard I/O Program • Standard Output • (STDOUT) • Screen • Input to another program • A file • Standard Input • (STDIN) • Keyboard • Output from another program • A file Standard Error (STDERR)
File Descriptor • A file descriptor (FD) is an abstract indicator for accessing a file. It’s an Integer in UNIX
Input/Output Redirection • When a file descriptor is assigned to something other than a terminal, it is called I/O redirection • Input redirection: < • Output redirection: > and >> %wc –l # read your keyboard input and count # of lines %wc –l <a_file #wc count the lines in a_file %echo “a” #print hi on screen %echo “a” > a_file #save ‘a’ in a_file %echo “b” >>a_file #append ‘b’ in a_file
Pipes • A pipe is a holder for a stream of data. • A pipe can be used to hold the output of one program and feed it to the input of another. • Separate 2 commands with the “|” character. • % prog1 | prog2 #prog2 use prog1’s output prog1 prog2 STDOUT STDIN
Redirection/Pipe Examples %ls -1 | wc %ls –l |grep *.doc | wc –l >Num_of_files.txt #save the number of files end with .doc in num_of_files.txt
Building Complex Commands • cmd & : launch cmd in background • cmd1; cmd2: run commands in sequence • cmd1 `cmd2`: use cmd2 output as arguments to cmd1 (backquotes) • cmd1 && cmd2: AND, execute cmd2 only if cmd1 finishes with non-zero status, i.e. if comd1 succeeds • cmd1 || cmd2: OR, execute cmd2 only cmd1 finishes with the status of zero, i.e. if cmd1 fails
Self-Test % wc < nums > wcNums % ls | wc > wcLs % echo `whoami` | grep –q john && echo “I’m Alex” % echo `whoami` | grep -q john || echo "I'm Alex" I'm Alex
Related Commands • ls #-l,-m,-1,-R, --sort, --format • pwd • * ? [ ] ~ • tree • echo • chmod #-R • chown • chgrp • wc # -l, -c, -w • date #+%Y-%m-%d, +%a, +%b, --date=“next sun”
Recommended Reading • Chapter 1, section 1.6 (excluding section 1.6.7) • Chapter 9, sections 9.4, 9.7, 9.8, 9.11, 9.12 • Don’t worry if any examples use syntax we haven’t seen yet (like for variable arrays). Just make sure you understand the main ideas.