The operating system is responsible for maintaining the file system. The file system maintained by a UNIX system consists of a hierarchy of files. Two types of files will be of interest to us: directories (folders) and regular files, i.e. files that are not directories. Regular files may hold data or programs. They may consist of text or be binary files that are not human-readable.
There are also hidden files. These files have a name
beginning with a dot (.). To see them use the command ls -a
or ls –all
(ls -all
on a Mac). Both files and
directories can be hidden. Usually these items contain data on the
configuration of your system and its applications.
You are used to working with regular files and directories in Windoze or MacOSX. Things in UNIX work the same way, but we will use commands to manage files instead of mouse clicking or dragging.
As we have already seen the UNIX command touch
, which
creates new files. This command creates an empty file for each argument
given it. At your UNIX prompt, enter
unix> touch stuff
This creates the empty file named stuff
in your current
working directory.
Now let us analyze the anatomy of this command. The name of the
command is touch
; its purpose is to create an empty file.
Since you do not see a - sign, there are no options being used. The
argument is stuff
. This is the name of the file you
created. Create a few more empty files. Enter these commands
unix> touch foo unix> touch bar
You may create several files at once by making a space-separated list as we show here.
unix> touch aardvark buffalo cougar dingo elephant
Now you have eight new files in your cwd
. Next we will
see how to list the files. Enter this command at the UNIX prompt
unix> ls
The command ls
lists your files. Notice we had neither
options nor arguments. If you created the files using touch
as instructed, they should appear on your screen like this
unix> ls unix> aardvark bar buffalo cougar dingo elephant foo stuff
The command ls
has several options. One option is the
l
option; it list the files in long format. To invoke it,
type
unix> ls -l
You will see a listing like this
unix> ls -l -rw-rw-r-- 1 morrison morrison 0 Jun 9 10:50 aardvark -rw-rw-r-- 1 morrison morrison 0 Jun 9 10:50 bar -rw-rw-r-- 1 morrison morrison 0 Jun 9 10:50 buffalo -rw-rw-r-- 1 morrison morrison 0 Jun 9 10:50 cougar -rw-rw-r-- 1 morrison morrison 0 Jun 9 10:50 dingo -rw-rw-r-- 1 morrison morrison 0 Jun 9 10:50 elephant -rw-rw-r-- 1 morrison morrison 0 Jun 9 10:49 foo -rw-rw-r-- 1 morrison morrison 0 Jun 9 10:49 stuff
The first column reflects the permissions for the files. The sequence
-rw-rw-r--
indicates that you and your group have read/write permission and that others (“the world”) have read permission. We will discuss permissions in more detail when we discuss the management of directories.
You can see the name here is listed in two columns; on this machine
morrison
is in his own group. On another system, you may
live in group with several other people; if so you will see the name of
that group in one of these columns. The zero indicates the size of the
file; each file we created is empty. Then there is a date, a time and
the file name. This is the long format for file listing; it is seen by
using the -l
option in the ls
command.
Another option is the -a
option. This lists all files,
including “hidden” files. Hidden files have a dot (.) preceding their
name. To see them, enter
unix> ls -a
at the command line. One thing you are guaranteed to see, no matter
where you are are are the directories ..
(parent) and
.
(current). If you are in you home directory, You will see
the files you saw using ls
and several hidden files with
mysterious names like bash_profile
. Do not delete these;
they provide the configuration for your account and do things like
record preferences for applications you have used or installed. You can
also list all files including hidden files by entering
unix> ls --all
You can use more than one option at once. For example, entering
unix> ls -al
or
unix> ls -a -l
unix> ls --all -l
shows all of your files and hidden files in long format. Try this now on your machine.
Note to Mac Users
Mac users should precede verbose commands with a single
-
. So on a Mac, you type
unix> ls -all -l
and not
unix> ls --all -l
Otherwise, your Mac will respond with a cryptic error message.
Next we will show how to display a file to the screen. UNIX commands
that process files are called filters. Filters accept input
from a file, and place output in another file. The default action of any
UNIX filter is to get input from stdin
and send its output
to stdout
. Any errors resulting from using the filter go to
stderr
, which by default is just stdout
. If an
argument is offered to a UNIX filter, that argument is generally a
regular file, and it gets used in lieu of stdin
.
Let us peek inside your .bash_profile
file. Enter the
command
unix> cat .bash_profile
The command name is cat
, short for catalog (the file to
the screen). The cat
command is a filter that does not
filtering at all; it simply dumps the entire file to the screen all at
once. We are using no options, but the file name is an argument to
cat
. If a file is long and you want to see it one screenful
at a time, use the filter more
. The command
more
takes a file name as an argument and shows it on the
screen a screenful at a time. You can hit the space bar to see the next
screenful or use the down-arrow or enter key to scroll down one line at
a time. To exit more
at any time, type a q
and
more
quits. You can use several arguments in
cat
or more
and the indicated files will be
displayed in seriatum.
Programming Exercises
What does the
-r
option causels
to do?What does the
-t
option causels
to do? Try combining it with thte-l
option and it becomes clear.What does the
-n
option do tocat
?
6.1 Globbing
Make a directory called tossme
(you will toss this when
done) and enter it by doing the following
unix> mkdir tossme
unix> cd tossme
Now create some empty files with touch
like so.
MAC:Tue Jan 04:10:24:tossme> touch cat.html dog.html snake.css snake.py cow.c MAC:Tue Jan 04:10:26:tossme> touch Turtle.java Egret.java tapir.py goat.cpp MAC:Tue Jan 04:10:27:tossme> ls Egret.java cat.html dog.html snake.css tapir.py Turtle.java cow.c goat.cpp snake.py
The special character *
is a wildcard standing for any
sequence of zero or more characters. Using *
to create
wildcards is called “globbing” because it allows you to refer to a glob
of files. Let us use it to list all files with the extension
.py
.
MAC:Tue Jan 04:10:27:tossme> ls *.py snake.py tapir.py
Now let’s list all files that begin with the letter
s
.
unix> ls s* snake.css snake.py
How do we list all files whose extension begins with a
c
?
unix> ls *.c* cow.c goat.cpp snake.css
You can cat
a glob of files; for example.
cat *.py
will put all files to stdout
with a .py
extension.