S5 version Table of Contents (frame/ no frame)
(19) Files Previous Top Next

Files: Creating, Listing and Examining

Sidebar: standard I/O and redirection

Most commands send their output to "standard output". The shell provides a mechanism for redirecting this to a file, or to another program. The default is to the terminal. Many programs also read some input (default is the keyboard of the terminal). Again, there is a mechanism to tell any program to read its input from a file or another program instead.
> filename
send output to the named file, creating it if needed. This is most common way new files are created
>! filename
send output to the named file, creating it if needed, and forcing overwrite
>> filename
append output to the named file, creating it if needed
< filename
read input from the named file
prog1 | prog2
run "prog1" and send its output to the input of "prog2". Both programs execute in "parallel". Actually they are usually timeshared, but appear to both run simutaneously

These facilities are available to all programs, but it is up to the individual programs whether they respect the conventions.

Creating a file

echo " This is a test file" > test_file
This creates a file using "echo" and "redirecting" the output to a file.
ls -a > ls_file
This creates a file by taking the output(standard output) of the "ls" command and " redirecting" the output to a file.
ls -FR /people2 | wc > how_many
This creates a file by taking the standard output of the "ls" command and "piping" it in to the "wc" (word count) command and then redirecting" the output to a file.
touch empty_file
Creates a empty file! Useful way to signal an event.

Looking at files

more [file_name]
Will display the contents of a file one "page" at a time. Can also display the contents of a data "stream" one page at a time. Ex. ls -FR | more
more /usr/man/cat1/cc.1
The space bar will move you to the next page and the return key will move you down a line. A "q" will end the more (more can also search through a file see the man page).
cat /usr/man/cat1/cc.1
Streams the file to "standard output". What else can cat do??
head [file_name]
Display the first few lines of a file
tail [file_name].
Display the last few lines of a file
Most of these utilities are intended for examining text files, with lines separated by "Newline" characters. Most files in Unix tend to be text files. The internal structure of binary files is application specific, and with the exception of cat, these utilities are not very useful with them. Two utilities, strings and od are very useful for examining binary files outside of their specific applications.

Previous Top Next


slide20.src  last modified Jan 31, 2011 Introduction Table of Contents
(frame/no frame)
Printable
(single file)
© Dartmouth College