330 likes | 411 Views
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 ( *, ?, [] )
E N D
Introduction to UNIX E. Manipulating Files 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 (*, ?, []) 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
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
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
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
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
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
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
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
Wildcards - [ ] • Use Brackets to define a range: host% ls doc/memo.[1-5] doc/memo.1 doc/memo.3 doc/memo.5 Manipulating Files
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
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
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
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
File Commands - rm • To remove filenames with certain patterns: host% rm *junk host% rm *mb* Manipulating Files
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
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
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
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
Migrating Privileges • The cp command uses the umask privileges. • The mv command transfers existing privileges for a file. Manipulating Files
Protecting Your Files - chmod • Command syntax • host% chmod mode file ... • The option "mode" • defines all permissions or • changes existing permissions Manipulating Files
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
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
Printing Files • Local print commands • lpr, lp • lpq, lpc status • lprm, cancel • pr • Contact consult for output from mode Manipulating Files
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
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
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
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
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
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
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
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
End of Module Complete Manipulating Files Exercises Manipulating Files