CSCI 330
THE UNIX SYSTEM
File operations
OPERATIONS ON REGULAR FILES
CSCI 330 - The UNIX System
Create
Edit
Display
Contents
Print
Others
CREATING NEW FILES
cat
vim,
emacs
nano,
etc.
See Text Editors
Section
CSCI 330 - The UNIX System
Create Regular
Files
Redirect
Command
Output
See shell
Section
3
CREATING A FILE WITH CAT
CSCI 330 - The UNIX System
Example:
% cat > myfile
This is line 1 of input
Line 2 of input
^d
%
4
DISPLAY CONTENTS OF TEXT FILES
cat
more
less
pg
head
CSCI 330 - The UNIX System
Display Text
File contents
tail
VIEWING CONTENTS OF TEXT FILES
CSCI 330 - The UNIX System
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
VIEWING CONTENTS OF TEXT FILES
CSCI 330 - The UNIX System
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
VIEWING CONTENTS OF TEXT FILES
CSCI 330 - The UNIX System
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
VIEWING CONTENTS OF TEXT FILES
CSCI 330 - The UNIX System
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
PRINTING FILES
lpr
send a file to the default printer
printers available:
csl or
frl or
ucl or
lpcsl
lpfrl
lpucl
(default)
CSCI 330 - The UNIX System
Example:
% lpr -P frl assign1.txt
10
PRETTY-PRINTING FILES
enscript
converts text file to PostScript, rtf or html
default: PostScript
sends output to printer
Example:
% enscript assign1.txt
CSCI 330 - The UNIX System
Options:
-P
to specify printer
-w
to select output language
-o
to specify output file
11
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
CSCI 330 - The UNIX System
12
OPERATIONS ON TEXT & OTHER FILES
Other File
Operations
Extract
contents
Compare
files
Count
words
Compress
contents
Sort
Unique
lines
Encrypt/
decrypt
CSCI 330 - The UNIX System
Combine
contents
13
COMBINING CONTENTS OF FILES
Method 1: To vertically concatenate the contents of
two or more files, use cat with output redirection (>)
all-file will contain the combined contents of file-1,
file-2, and file-3 in top-down (vertical) fashion
CSCI 330 - The UNIX System
Syntax: cat file-1 file-2 file-3 > all-file
14
COMBINING CONTENTS OF FILES
Method 2: To horizontally concatenate contents
(columns/fields) of two or more files, use paste
all-file will contain the combined contents of file-1
and file-2 in side-by-side (horizontal) fashion
CSCI 330 - The UNIX System
Syntax: paste file-1 file-2 > all-file
15
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
CSCI 330 - The UNIX System
Example:
% cut -d: -f 5 /etc/passwd
16
COMPARING FILES: COMM
The command named comm can be used to
compare lines that are common in two sorted files
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
CSCI 330 - The UNIX System
Syntax: comm [options] file-1 file-2
17
COMPARING FILES: DIFF
The command diff compares two files line by line
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)
CSCI 330 - The UNIX System
Syntax: diff [options] file-1 file-2
18
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
CSCI 330 - The UNIX System
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
FILE COMPRESSION
utilities to compress and uncompress files
common on Linux:
.gz
Example:
% gzip assign1.txt
% gunzip assign1.txt.gz
CSCI 330 - The UNIX System
gzip, gunzip
file extension:
Gzip and gunzip delete their inputs
So make a copy if you want one
20
COMPRESS FILE CONTENTS
Bzip2
Old
compress/uncompress (.Z)
Windows-compatible
CSCI 330 - The UNIX System
New, better compression
zip/unzip (.zip)
Do not delete their input!
21
SORTING FILES
To sort a text file in ascending or descending order,
use sort
Commonly used options:
-r sort in reverse order
-n numeric sort
-t field delimiter
-k field1[,field2]
-f ignore case
CSCI 330 - The UNIX System
Syntax: sort [options] file-name
22
REMOVING REPEATED LINES
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
CSCI 330 - The UNIX System
uniq
removes repeated lines from a sorted input file,
sending unique (unrepeated) lines to standard
output
23
USERS DISK QUOTA
quota is upper limit of
for each user account
The command: quota -v
displays the users disk usage and limits
CSCI 330 - The UNIX System
amount disk space
number of files
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