Download presentation
Presentation is loading. Please wait.
Published byRolf McCoy Modified over 9 years ago
1
CSCI 330 T HE UNIX S YSTEM File operations
2
OPERATIONS ON REGULAR FILES 2 CSCI 330 - The UNIX System Create Edit Display Contents Display Contents Print Others
3
CREATING NEW FILES 3 CSCI 330 - The UNIX System Create Regular Files Create Regular Files vim, emacs nano, etc. cat Redirect Command Output Redirect Command Output See Text Editors Section See shell Section
4
CREATING A FILE WITH CAT 4 CSCI 330 - The UNIX System Example: % cat > myfile This is line 1 of input Line 2 of input ^d %
5
DISPLAY CONTENTS OF TEXT FILES 5 CSCI 330 - The UNIX System Display Text File contents Display Text File contents cat more less pg head tail
6
VIEWING CONTENTS OF TEXT FILES command “cat” can be used to display/concatenate one or more files, displaying the output all at once Example: Display the contents of file “assign1.txt” % cat assign1.txt 6 CSCI 330 - The UNIX System
7
VIEWING CONTENTS OF TEXT FILES “more”, “less” or "pg" display the contents of one or more files one page at a time Space bar – to advance to next page b – to go back a page Enter Key – to advance to next line Example: Display the contents of file “assign1.txt” one page at a time % less assign1.txt 7 CSCI 330 - The UNIX System
8
VIEWING CONTENTS OF TEXT FILES “head” displays the beginning portion of indicated file(s); the default head size is 10 lines. Example: Display first 20 lines of file “assign1.txt” % head -20 assign1.txt 8 CSCI 330 - The UNIX System
9
VIEWING CONTENTS OF TEXT FILES “tail” displays the ending portion of indicated file(s); the default tail size is 10 lines. Example: Display last 10 lines of file “assign1.txt” % tail assign1.txt % tail -10 assign1.txt 9 CSCI 330 - The UNIX System
10
PRINTING FILES “lpr” send a file to the default printer printers available: csl or lpcsl(default) frl or lpfrl ucl or lpucl Example: % lpr -P frl assign1.txt 10 CSCI 330 - The UNIX System
11
PRETTY-PRINTING FILES “enscript” converts text file to PostScript, rtf or html default: PostScript sends output to printer Example: % enscript assign1.txt Options: -Pto specify printer -wto select output language -oto specify output file 11 CSCI 330 - The UNIX System
12
CHECKING PRINTING STATUS Syntax: lpq [options] Commonly used options: -P printer shows print jobs on specific printer -U user-id shows print jobs for specific user -l long format of listing -a shows print jobs on all printers Also: “lprm” to remove unwanted print job 12 CSCI 330 - The UNIX System
13
OPERATIONS ON TEXT & OTHER FILES 13 CSCI 330 - The UNIX System Other File Operations Other File Operations Combine contents Combine contents Extract contents Extract contents Compare files Compare files Count words Compress contents Compress contents Sort Unique lines Unique lines Encrypt/ decrypt Encrypt/ decrypt
14
COMBINING CONTENTS OF FILES Method 1: To vertically concatenate the contents of two or more files, use cat with output redirection (>) Syntax: cat file-1 file-2 file-3 > all-file “all-file” will contain the combined contents of file-1, file-2, and file-3 in top-down (vertical) fashion 14 CSCI 330 - The UNIX System
15
COMBINING CONTENTS OF FILES Method 2: To horizontally concatenate contents (columns/fields) of two or more files, use paste Syntax: paste file-1 file-2 > all-file “all-file” will contain the combined contents of file-1 and file-2 in side-by-side (horizontal) fashion 15 CSCI 330 - The UNIX System
16
EXTRACTING CONTENTS OF FILES To extract one or more fields from a file, use cut fields are delimited by special character default: TAB, change via –d option common: “:” must specify list of fields to be extracted option -f Example: % cut -d: -f 5 /etc/passwd 16 CSCI 330 - The UNIX System
17
COMPARING FILES: COMM The command named “comm” can be used to compare lines that are common in two sorted files Syntax: comm [options] file-1 file-2 The output contains three columns: Column1 contains lines unique to file-1 Column 2 contains lines unique to file-2 Column 3 contains lines common to both files 17 CSCI 330 - The UNIX System
18
COMPARING FILES: DIFF The command diff compares two files line by line Syntax: diff [options] file-1 file-2 If file-1 and file-2 are the same, no output is produced If file-1 and file-2 are not the same, diff reports a series of commands that can be used to convert the first file to the second file (via the “patch” command) 18 CSCI 330 - The UNIX System
19
DETERMINING FILE SIZE Recall: The “ls” command with the option “-l” gives the file size in bytes Use “wc” to display the size of files as number of lines, words, and characters Syntax: wc file-list Commonly used options: -l display the number of lines -w display the number of words -c display the number of characters 19 CSCI 330 - The UNIX System
20
FILE COMPRESSION utilities to compress and uncompress files common on Linux: gzip, gunzip file extension:.gz Example: % gzip assign1.txt % gunzip assign1.txt.gz Gzip and gunzip delete their inputs So make a copy if you want one 20 CSCI 330 - The UNIX System
21
COMPRESS FILE CONTENTS Bzip2 New, better compression Old compress/uncompress (.Z) Windows-compatible zip/unzip (.zip) Do not delete their input! 21 CSCI 330 - The UNIX System
22
SORTING FILES To sort a text file in ascending or descending order, use sort Syntax: sort [options] file-name Commonly used options: -r sort in reverse order -n numeric sort -t field delimiter -k field1[,field2] -f ignore case 22 CSCI 330 - The UNIX System
23
REMOVING REPEATED LINES “uniq” removes repeated lines from a sorted input file, sending unique (unrepeated) lines to standard output Syntax: uniq sorted-file-name Commonly used options: -c place a count of repeated lines at beginning of each output line -d display the repeated lines -u display the lines that are not repeated 23 CSCI 330 - The UNIX System
24
USER’S DISK QUOTA quota is upper limit of amount disk space number of files for each user account The command: quota -v displays the user’s disk usage and limits 2 kinds of limits: Soft limit: ex. 10MB May be exceeded for one week System will remind you when you log on Hard limit: ex. 12MB Cannot be exceeded 24 CSCI 330 - The UNIX System
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.