june 1, 1999manipulating files1 introduction to unix e. manipulating files
TRANSCRIPT
June 1, 1999 Manipulating Files 2
Basic File Manipulation
Performance Objectives1. 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)
June 1, 1999 Manipulating Files 3
Basic File Manipulation
Performance Objectives- continued10. 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)
June 1, 1999 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
June 1, 1999 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.
June 1, 1999 Manipulating Files 6
Viewing the Head or Tail
• View the first 10 lines of a file:host% head passwd
• Use -20 to see 20 lineshost% head -20 passwd
• View the last 10 lines of a file:host% tail passwd
• Use -f to continuously monitor a filehost% tail -f /var/log/syslog
June 1, 1999 Manipulating Files 7
Wild Cards - *
• Wildcards permit selection criteria
• Note the following list:host% lsAprmbox Junmbox bin misc_memosAugmbox Marmbox calendar newjunkDecmbox Maymbox doc_memo newsrcFebmbox Novmbox junk outJanmbox Octmbox login test.fJulmbox Sepmbox mbox test.out
June 1, 1999 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
June 1, 1999 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
June 1, 1999 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
June 1, 1999 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
June 1, 1999 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 .
June 1, 1999 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.
June 1, 1999 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
June 1, 1999 Manipulating Files 15
File Commands - rm
• To remove filenames with certain patterns:host% rm *junk
host% rm *mb*
June 1, 1999 Manipulating Files 16
Protecting Your Files
• Use the -l option to reveal the permissions:host% ls -ldrwxr-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
June 1, 1999 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
user (owner)
group
other (world)
June 1, 1999 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.
June 1, 1999 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.
June 1, 1999 Manipulating Files 20
Migrating Privileges
• The cp command uses the umask privileges.
• The mv command transfers existing privileges for a file.
June 1, 1999 Manipulating Files 21
Protecting Your Files - chmod
• Command syntaxhost% chmod mode file ...
• The option "mode" defines all permissions or changes existing permissions
June 1, 1999 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
June 1, 1999 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
June 1, 1999 Manipulating Files 24
Printing Files
• Local print commands lpr, lp lpq, lpc status lprm, cancel pr
• Contact consult for output from mode
June 1, 1999 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%
June 1, 1999 Manipulating Files 26
Finding Files with find
• find will search a directory tree
• You need to have search privileges for treefind . -name a.out -print
• Searches can take a long time, background them
find / -name crack -print > crack.found &
• See man page for other options
June 1, 1999 Manipulating Files 27
Archiving files with tar
• Tape Archive (tar)
• Used to create one file from a directory tree
• Create a tar filetar cvf /tmp/newfile.tar .
• Expand a tar filetar xvf product.tar
• May have to uncompress it firstuncompress tex.tar.Z ; tar xvf tex.tar
June 1, 1999 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 listspell project.report
• Now how do you spell seperate?look sep
June 1, 1999 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
June 1, 1999 Manipulating Files 30
Global Regular Expression Parser• grep - Global Regular Expression Parser
Searches a file for a specified string:host% grep ths passwdths:6q3y/n8TQp2WU:1059:8010:Ted Spitzmiller, 081347,8010x33d:/home/sunclass1/ths:/bin/csh
• wc - Word Count, shows the number of lines, words, and charactershost% wc project.report
June 1, 1999 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 > x Routes the result to file "x" host% sort x Sorts 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/
June 1, 1999 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