1 / 25

Lecture 2

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

bisa
Download Presentation

Lecture 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 2 The Structure of Directories Filename Metacharacters Handling Special Characters Input / Output Redirection Building Complex Commands

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

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

  4. Cont. • /usr • /usr/inlcudeand /usr/lib • /usr/bin/ -contains more executables • /sbin • Contains executables which can only be executed by the super user (root)

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

  6. Access Rights For A File • A file: Who has what permissions?

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

  8. Cont. • For example:what is permission 5? • 5 in binary format is : • 101 Can run Can read No write

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

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

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

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

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

  14. Back Quota • `command`- used for command substitution • Example: • set d = `date` • Now d contains the output of date command

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

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

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

  18. File Descriptor • A file descriptor (FD) is an abstract indicator for accessing a file. It’s an Integer in UNIX

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

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

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

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

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

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

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

More Related