1 / 33

Introduction to UNIX

Introduction to UNIX. E. Manipulating Files. Basic File Manipulation. Performance Objectives 1. View a selected file ( cat, more ) 2. View leading or trailing segments of a file ( head, tail ) 3. Copy and move files ( cp, mv ) 4. Use Wild Card Characters ( *, ?, [] )

Download Presentation

Introduction to UNIX

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. Introduction to UNIX E. Manipulating Files Manipulating Files

  2. Basic File Manipulation Performance Objectives 1. View a selected file (cat, more) 2. View leading or trailing segments of a file (head, tail) 3. Copy and move files (cp, mv) 4. Use Wild Card Characters (*, ?, []) 5. Identify file permission attributes (rwx) 6. Set file permissions (chmod, umask) 7. Print Files (lpr, lpq, cancel, pr) 8. Record the Screen Image (script) 9. Locate a file (find) Manipulating Files

  3. Basic File Manipulation Performance Objectives- continued 10. Archive files (tar) 11. Check spelling (ispell, look) 12. Search for specified string in a file (grep) 13. Order a file by a specified column (sort) Manipulating Files

  4. Viewing a File - cat • Three forms: • Read each file in sequence and display it on standard output. • host% cat file1 file2 • Combine files and place result in another. • host % cat file1 file2 > file3 • Make a file from standard input (until ^d) • host% cat >file Manipulating Files

  5. Viewing a File - more • View a file in a controlled manner. • more [options] filename • host% more passwd • Note more --- xx% in the lower left corner. • <RETURN> moves one line at a time. • SPACEBAR moves one screen at a time. • Use /keyword to advance to desired information. Manipulating Files

  6. Viewing the Head or Tail • View the first 10 lines of a file: host% head passwd • Use -20 to see 20 lines host% head -20 passwd • View the last 10 lines of a file: host% tail passwd • Use -f to continuously monitor a file host% tail -f /var/log/syslog Manipulating Files

  7. Wild Cards - * • Wildcards permit selection criteria • Note the following list: host% ls Aprmbox Junmbox bin misc_memos Augmbox Marmbox calendar newjunk Decmbox Maymbox doc_memo newsrc Febmbox Novmbox junk out Janmbox Octmbox login test.f Julmbox Sepmbox mbox test.out Manipulating Files

  8. Wild Cards - * (Con’t) • Use an asterisk * to list filenames containing certain patterns. host% ls *junk junk newjunk host% ls *mb No match Manipulating Files

  9. Wild Cards - * (Con’t) • Carefully observe the placement of asterisks • host% ls *mb* Aprmbox Janmbox Maymbox mbox Augmbox Julmbox Novmbox Decmbox Junmbox Octmbox Febmbox Marmbox Sepmbox Manipulating Files

  10. Wildcards - [ ] • Use Brackets to define a range: host% ls doc/memo.[1-5] doc/memo.1 doc/memo.3 doc/memo.5 Manipulating Files

  11. Wildcards - ? • Use the question mark to define a specific number of character positions: host% ls test.??? test.out • Note that this ls did not display the file test.f Manipulating Files

  12. File Commands - cp • The copy command copies a file to a new location: host% cp passwd docs/newfile host% cp -i ~dhk/file2 docs/newfile overwrite newfile? host% cp ~dhk/passwd . Manipulating Files

  13. File Commands - mv • Command syntax: host% mv oldfile newfile host% mv -i ~dhk/file2 calendar remove calendar? host% mv calendar docs • Note possible use of set noclobber in .cshrc. Manipulating Files

  14. File Commands - rm • Command syntax: host% rm oldfile host% rm -i newfile rm: remove newfile? host% rm -r * • Removes ALL files and directories - BE CAREFUL Manipulating Files

  15. File Commands - rm • To remove filenames with certain patterns: host% rm *junk host% rm *mb* Manipulating Files

  16. Protecting Your Files • Use the -l option to reveal the permissions: host% ls -l drwxr-xr-x 2 ths 512 Oct 23 1985 bin -rw-r--r-- 1 ths 129 Nov 20 1985 complex.f -rw------- 1 ths 129 Jul 2 10:05 mbox • File Permissions • r = read • w = write • x = execute/search Manipulating Files

  17. Protecting Your Files • Who has permission? d rwx r-x r-x 2 ths 512 Oct 23 1985 bin • - --- --- --- u g o other (world) group user (owner) Manipulating Files

  18. Protecting Your Files • chmod changes permissions after a file has been created. • umask in your .cshrc sets the file creation mask for all files. Manipulating Files

  19. Protecting Your Files - umask • Command syntax: host% umask value • Octal values deny access privileges 1 x 4 r 7 rwx 2 w 5 rx 3 wx 6 rw • default value 22 Group and Others have no write permission. • most restrictive 77 Group and Others have no permissions. Manipulating Files

  20. Migrating Privileges • The cp command uses the umask privileges. • The mv command transfers existing privileges for a file. Manipulating Files

  21. Protecting Your Files - chmod • Command syntax • host% chmod mode file ... • The option "mode" • defines all permissions or • changes existing permissions Manipulating Files

  22. Protecting Your Files - chmod • chmod uses the following syntax: chmod ug+w filename who op permission u = r g + w o - x a Manipulating Files

  23. Protecting Your Files - chmod • Consider the following examples: -rw-r--r-- 1 ths 129 Nov 20 1985 memos drwxr-xr-x 2 ths 512 Oct 23 1985 pri.dir -rw------- 1 ths 129 Jul 2 10:05 mbox • Give write permission to group and others. host% chmod go+w memos • Remove read/search from group. host% chmod g-rx pri.dir • Read permission for all. host% chmod a=r mbox Manipulating Files

  24. Printing Files • Local print commands • lpr, lp • lpq, lpc status • lprm, cancel • pr • Contact consult for output from mode Manipulating Files

  25. Capturing screen input-script • To capture everything that shows up on the screen, use script host.21% script errors Script started, file is errors host.1% CC program.c -o program.x . . . host.2% exit Script done, file is errors host.22% Manipulating Files

  26. Finding Files with find • find will search a directory tree • You need to have search privileges for tree find . -name a.out -print • Searches can take a long time, background them find / -name crack -print > crack.found & • See man page for other options Manipulating Files

  27. Archiving files with tar • Tape Archive (tar) • Used to create one file from a directory tree • Create a tar file tar cvf /tmp/newfile.tar . • Expand a tar file tar xvf product.tar • May have to uncompress it first uncompress tex.tar.Z ; tar xvf tex.tar Manipulating Files

  28. Check spelling - spell and look • Most Unix systems have both • Not as good as word processors • Dumps words that are not in its spelling list spell project.report • Now how do you spell seperate? look sep Manipulating Files

  29. Use of Filters • A filter takes its input from a file, performs some operation on that input, and writes the result to standard output. • Common UNIX filters are • grep • more • sort • wc Manipulating Files

  30. Global Regular Expression Parser • grep - Global Regular Expression Parser Searches a file for a specified string: host% grep ths passwd ths:6q3y/n8TQp2WU:1059:8010:Ted Spitzmiller, 081347,8010x33d:/home/sunclass1/ths:/bin/csh • wc - Word Count, shows the number of lines, words, and characters host% wc project.report Manipulating Files

  31. Use of Filters - sort • Performs an alphanumeric sort of a file. host% sort [options] [file(s)] • Example of use: host% ls -l > xRoutes the result to file "x" host% sort xSorts on first field (blank delimiter) 0 -rw------- 1 ths 0 Jun 4 09:30 dead.letter 0 -rw-r--r-- 1 ths 0 Apr 30 14:34 x 1 -rwx------ 1 ths 31 Dec 17 1993 startAR* 1 -rwx-----x 1 ths 115 Nov 25 1991 cfs.size* 1 drwx------ 2 ths 512 Feb 29 1996 News/ 1 drwx--x--x 2 ths 512 Aug 15 1996 cfs.dir/ 1 drwxr-xr-x 2 ths 512 Apr 22 14:52 complit/ 2 -rw------- 1 ths 1117 Apr 8 1996 travel 2 -rw-r--r-- 1 ths 1609 Feb 13 09:32 team.mmet 2 -rwx--x--x 1 ths 1690 Apr 28 1993 openwin-init* 2 drwx------ 2 ths 1536 Nov 28 1995 html/ 2 drwxr-xr-x 2 ths 1536 Apr 7 12:01 bits2/ Manipulating Files

  32. Use of Sort: • Note what happens with some options: host ls -l | sort + 4 total 25 3 -rw------- 1 ths 2238 Jan 29 1992 login.matrix 4 -rw------- 1 ths 3156 Mar 27 1992 file.mat 4 -rw------- 1 ths 3187 Mar 20 1992 mail.mat 7 -rw------- 1 ths 6280 Nov 26 1991 login.bak 7 -rw------- 1 ths 6497 Jan 17 1992 logmatrix.bak Manipulating Files

  33. End of Module Complete Manipulating Files Exercises Manipulating Files

More Related