CSC 140 Introduction to IT Advanced File Processing

  • Slides: 24
Download presentation
CSC 140: Introduction to IT Advanced File Processing Part 2 CIT 140: Introduction to

CSC 140: Introduction to IT Advanced File Processing Part 2 CIT 140: Introduction to IT 1

Topics 1. 2. 3. 4. Searching for files: find Searching files: grep and egrep

Topics 1. 2. 3. 4. Searching for files: find Searching files: grep and egrep Cutting and pasting: cut and paste Encryption and decryption: tr and crypt CIT 140: Introduction to IT 2

Searching for Files: find path expression Search for files that match expression under path.

Searching for Files: find path expression Search for files that match expression under path. Example expressions: atime +x Was file accessed > x days ago? atime –x Was file accessed < x days ago? mtime +/ x Was file modified >/< x days ago? name x Does filename match x? perm x Does file match permissions x? size +/ x Is file larger/smaller than x blocks? type c Is file a regular file | link | directory? user n Does file belong to user n? CIT 140: Introduction to IT 3

Basic Find Options > find /usr/include -name stdio. h /usr/include/stdio. h > find /usr/include

Basic Find Options > find /usr/include -name stdio. h /usr/include/stdio. h > find /usr/include -name stdio. h -ls 1839412 rw r r 1 root bin 11461 Apr 6 2002 /usr/include/stdio. h > find /usr/include -type l /usr/include/iso/assert_iso. h /usr/include/iso/errno_iso. h. . . /usr/include/uil /usr/include/xil > find /usr/include -type d /usr/include/xfn. . . CIT 140: Introduction to IT 4

More Find Options > find /usr/include -mtime -7 > find /usr/include -mtime +7 /usr/include.

More Find Options > find /usr/include -mtime -7 > find /usr/include -mtime +7 /usr/include. . . /usr/include/xfn/FN_bindingset. h > find /usr/include -perm u=rw, g=rw, o=r > find /usr/include -perm u=rw, g=r, o=r /usr/include/zconf. h /usr/include/zlib. h. . . > find /usr -name '*. h' find: cannot read dir /usr/lost+found: Permission denied /usr/openwin/share/include/X 11/DPS/Color. SB. h /usr/openwin/share/include/X 11/DPS/Color. SBP. h. . . CIT 140: Introduction to IT 5

Combining find Expressions > find /usr/include -perm u=rw, g=r, o=r -user waldenj > find

Combining find Expressions > find /usr/include -perm u=rw, g=r, o=r -user waldenj > find /usr/include -perm u=rw, g=r, o=r -user root /usr/include/zconf. h. . . /usr/include/xfn/FN_compound_name. h > find /kernel -type f -user root -size +1000 -ls 303 696 rwxr xr x 2 root sys 701748 May 13 2003 /kernel/drv/ip 801 888 rwxr xr x 2 root sys 899456 May 13 2003 /kernel/drv/sparcv 9/ip 303 696 rwxr xr x 2 root sys 701748 May 13 2003 /kernel/strmod/ip 801 888 rwxr xr x 2 root sys 899456 May 13 2003 /kernel/strmod/sparcv 9/ip 384 1768 rwxr xr x 1 root sys 1794232 May 27 2003 /kernel/genunix CIT 140: Introduction to IT 6

Combining find Expressions with -o > find /usr ( -name gnome -o -name dict

Combining find Expressions with -o > find /usr ( -name gnome -o -name dict ) -ls 68869 1 drwxr xr x 3 root other 512 Dec 29 2003 /usr/demo/gnome 3862 1 drwxr xr x 2 root bin 512 Dec 29 2003 /usr/share/lib/dict 20512 1 drwxr xr x 33 root other 1024 Dec 29 2003 /usr/share/gnome 71884 2 drwxr xr x 4 root other 1536 Dec 29 2003 /usr/share/gnome/pixmaps/nautilus/gnome 3844 1 lrwxrwxrwx 1 root 16 Dec 29 2003 /usr/dict >. /share/lib/dict 26756 1 drwxr xr x 3 root other 512 Dec 29 2003 /usr/gnome 90435 2 rw r r 1 bin 1480 May 2 2003 /usr/local/share/terminfo/g/gnome CIT 140: Introduction to IT 7

Searching Files: grep [-i] [-l] [-n] [-v] pattern file 1 [file 2, . .

Searching Files: grep [-i] [-l] [-n] [-v] pattern file 1 [file 2, . . . ] Search for pattern in the file arguments. i l n v Ignore case of letters in files. Print only the names of files that contain matches. Print line numbers along with matching lines. Print only nonmatching lines. CIT 140: Introduction to IT 8

Simple Searches > grep catt /usr/dict/words backscatter. . . wildcatter > grep -c catt

Simple Searches > grep catt /usr/dict/words backscatter. . . wildcatter > grep -c catt /usr/dict/words 8 > grep -c -v catt /usr/dict/words 25135 > wc -l /usr/dict/words 25143 /usr/dict/words > grep -n catt /usr/dict/words 1814: backscatter. . . 24675: wildcatter > grep -l catt /usr/include/*. h /usr/include/exec_attr. h /usr/include/xti. h CIT 140: Introduction to IT 9

Regular Expressions ^ $ [a-z] [0 -9] [aeiou]. * a|b Beginning of line End

Regular Expressions ^ $ [a-z] [0 -9] [aeiou]. * a|b Beginning of line End of line Character range (all lower case) Character range (digits) Character range (vowels) Any character Zero or more of previous pattern Match a or b CIT 140: Introduction to IT 10

Regular Expression Searches > egrep '^dogg' /usr/dict/words dogging doggone > egrep 'dogg$' /usr/dict/words >

Regular Expression Searches > egrep '^dogg' /usr/dict/words dogging doggone > egrep 'dogg$' /usr/dict/words > egrep 'mann$' /usr/dict/words Boltzmann Hermann Neumann Riemann Schumann > egrep '^mann' /usr/dict/words manna mannequin mannerism CIT 140: Introduction to IT 11

Regular Expression Searches > egrep 'catt|dogg' /usr/dict/words backscatter cattail. . . dogging doggone. .

Regular Expression Searches > egrep 'catt|dogg' /usr/dict/words backscatter cattail. . . dogging doggone. . . wildcatter > egrep '^(catt|dogg)‘ /usr/dict/words cattail cattleman cattlemen dogging doggone CIT 140: Introduction to IT 12

Character classes > grep '[0 -9]' /usr/dict/words 10 th 1 st 2 nd 3

Character classes > grep '[0 -9]' /usr/dict/words 10 th 1 st 2 nd 3 rd 4 th 5 th 6 th 7 th 8 th 9 th > grep -c '^[xz]' /usr/dict/words 49 > grep -c '[xz]$' /usr/dict/words 196 CIT 140: Introduction to IT 13

How many words have 4 sequential vowels? > grep '[aeiou][aeiou]' /usr/dict/words aqueous Hawaiian obsequious

How many words have 4 sequential vowels? > grep '[aeiou][aeiou]' /usr/dict/words aqueous Hawaiian obsequious onomatopoeia pharmacopoeia prosopopoeia queue Sequoia CIT 140: Introduction to IT 14

Cutting Text: cut [-d char] [-f #] [-s] file 1 [file 2, . .

Cutting Text: cut [-d char] [-f #] [-s] file 1 [file 2, . . . ] Print specified fields of files. c#1 #2 d char f #1, #2 f #1 #2 s Print characters #1 through #2 of each line. Use specified character as field separator. (Tab is default separator. ) Print field #1 and field #2 Print all fields #1 through #2 Don’t print lines that don’t contain field separator. CIT 140: Introduction to IT 15

Cutting with space-separated fields > who longa pts/13 Oct 3 11: 11 (10. 20.

Cutting with space-separated fields > who longa pts/13 Oct 3 11: 11 (10. 20. 12. 102) waldenj pts/16 Oct 8 16: 23 (10. 20. 10. 77) trutat 1 pts/14 Oct 8 16: 51 (10. 20. 11. 151) > who | cut -c 1 -8 longa waldenj trutat 1 > who | cut -c 1 -14 longa pts waldenj pts trutat 1 pts > who | cut -d " " -f 1 longa waldenj trutat 1 CIT 140: Introduction to IT 16

Cutting : -separated fields > tail /etc/passwd | cut -d: -f 1, 6 cribbett:

Cutting : -separated fields > tail /etc/passwd | cut -d: -f 1, 6 cribbett: /export/home 7/cribbett matracia: /export/home 7/matracia muellerm: /export/home 7/muellerm nadaudm: /export/home 7/nadaudm kohuss: /export/home 7/kohuss cit 1403: /export/home 7/cit 1403 stevenso: /export/home 7/stevenso mousem: /export/home 7/mousem krugd: /export/home 1/krugd frankc: /export/home 7/frankc CIT 140: Introduction to IT 17

Pasting Text: paste [-d char] [-f #] [-s] file 1 [file 2, . .

Pasting Text: paste [-d char] [-f #] [-s] file 1 [file 2, . . . ] Horizontally concatenate files. d char f #1, #2 f #1 #2 s Use specified character as field separator. (Tab is default separator. ) Print field #1 and field #2 Print all fields #1 through #2 Don’t print lines that don’t contain field separator. CIT 140: Introduction to IT 18

Cutting and Pasting > who >who. txt > cut -d " " -f 1

Cutting and Pasting > who >who. txt > cut -d " " -f 1 who. txt >users. txt > cat users. txt longa waldenj trutat 1 > cut -c 25 -31 who. txt >cut. txt > cat cut. txt Oct 3 Oct 8 > paste users. txt cut. txt longa Oct 3 waldenj Oct 8 trutat 1 Oct 8 CIT 140: Introduction to IT 19

Encryption • Encryption is the process of encoding a message so that its meaning

Encryption • Encryption is the process of encoding a message so that its meaning is not obvious. • Decryption is the reverse process, transforming an encrypted message back into its original form. CIT 140: Introduction to IT 20

ROT 13 (“ROTate by 13 places”) is a simple Caesar cipher used for obscuring

ROT 13 (“ROTate by 13 places”) is a simple Caesar cipher used for obscuring text by replacing each letter with the letter thirteen places down the alphabet. Not secure, but used to obscure quiz or puzzle answers so you don’t accidentally read them. CIT 140: Introduction to IT 21

ROT 13 in UNIX > tr A-Za-z N-ZA-Mn-za-m <days. txt >rot 13. txt >

ROT 13 in UNIX > tr A-Za-z N-ZA-Mn-za-m <days. txt >rot 13. txt > cat rot 13. txt Fhaqnl Zbaqnl Ghrfqnl Jrqarfqnl Guhefqnl Sevqnl Fngheqnl > tr A-Za-z N-ZA-Mn-za-m <rot 13. txt Sunday Monday Tuesday Wednesday Thursday Friday Saturday CIT 140: Introduction to IT 22

Encrypting Files: crypt key <plaintext. txt >ciphertext. txt Encrypt the file plaintext. txt using

Encrypting Files: crypt key <plaintext. txt >ciphertext. txt Encrypt the file plaintext. txt using the specified key and store the result in ciphertext. txt. crypt key <ciphertext. txt Decrypt the file ciphertext. txt using the specified key, which must be identical to the key used to originally encrypt the file. CIT 140: Introduction to IT 23

Example of crypt > crypt hsk 45$ <days. txt >crypt. txt > cat crypt.

Example of crypt > crypt hsk 45$ <days. txt >crypt. txt > cat crypt. txt ~¿RNiÊV·w. VæT®Ýz}ðqpà¾r´ 6 àÐÚ©ÖÓ'ÔÙóéö` 'îõkóòÇ > crypt hsk 45$ <crypt. txt Sunday Monday Tuesday Wednesday Thursday Friday Saturday > crypt foobar <crypt. txt º´ Z Æ'98Åéïkai£ 7`Íôãâ^w²ª® 9 Z*A 53=QeúSU 9Ð _¾ø 6`!>ü CIT 140: Introduction to IT 24